LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2020, 07:05 AM   #16
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931

Quote:
Originally Posted by AnanthaP View Post
The redirectional opposite of getchar is clearly putchar.
Quote:
Originally Posted by GazL View Post
I agree with AP. Been a while, but from what I remember, the early chapters of the K&R book were all about throwing characters around.
Yep, makes sense. Seems the OP got something working here, which I missed (phone screen laziness to not scroll), but there's a follow-up:
Quote:
Originally Posted by Arct1c_f0x View Post
Thanks guys! Now it does what it's supposed to.
I cut out all the extra nonsense I had put into the program because it simple wasn't necessary.
Code:
...
While I have you guys here could you briefly explain what the difference between single and double quotes is in C? I've only ever learned Python and JavaScript before and as you guys probably know, the two are, at least to my knowledge, completely interchangable. But it seems that I've run into issues with using '' and "" as if they were the same. Is it that one set interprets what's inside them literally or numerically and the other doesn't?
In C, single quotes are typically use to surround one character, and double quotes are typically used to surround a string. Not to say that you can't confuse yourself with a string of one apparent character, for instance "a" is still a string and in memory it would be two bytes, the letter 'a', or 0x61 in ASCII HEX code, but then followed by a 0x00, or NULL string terminator. And there are just so many ways to declare things that you can cross yourself up. No trying to complicate, just trying to answer the question. You see in printf() and scanf() the double quotes are used to surround the print or scan format string, because it is a string, of characters. Meanwhile, for one character, the syntax is to use the single quote. I'm not recalling any other situations in the C language where those two are used for anything else, except these purposes.

EDIT: Beat me to it michaelk
 
1 members found this post helpful.
Old 08-28-2020, 07:12 AM   #17
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,814

Rep: Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960


I wish I knew where my copy of K&R is hiding...
 
Old 08-28-2020, 07:33 AM   #18
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,886
Blog Entries: 1

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
Literal '@' (or '\x20' or '\177' etc) is an 'int', it's value is between 0..255 or -128..127 depending on default char-type is unsigned or signed. On the other hand, getchar (getc, fgetc, etc) always returns an int between 0..255, or -1 for EOF/error.
 
1 members found this post helpful.
Old 08-28-2020, 03:53 PM   #19
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by rtmistler View Post
Excuse me?

Is this not as simple as grabbing the argument list and outputting those arguments?

I.e. Non-compiled/tested, coded by seat of pants:
Code:
int main(int argc, char **argv)
{
    if(argc > 1) {
        for(int i = 1; i < argc; i++) {
            printf("%s\n", argv[i]);
        }
    }
}

I just started learning C and I knew that you could use the main() int function with such arguments but I'm trying to work with the working 'code' concept that Brian and Dennis have furnished me with at this point in the book (almost done with the first chapter).

Thanks man! Some of the syntax above I don't exactly understand but I know that I'll get there with time!
 
Old 08-28-2020, 04:00 PM   #20
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by dugan View Post
I assume you're supposed to get the "input" with scanf or something similar.
No actually by this point in the book the authors have only taught you about getchar() so I've been using ((getchar()) != EOF) as the conditional for a while loop. It seems to work although I also know how to use scanf to achieve the same result.

Funny thing though. I'm writing code in VS code and compiling and running the executables in the bash shell and whenever I want to use a prompt(printf) with input (scanf) I have to write it like this:

Code:
printf("Enter int: ", ) && scanf("%f", &exampleint);
And if I don't write it both like this separated by an AND statement, it will skip immediately past the scanf and move to the next lines without waiting for input. Weird right? Why does it do that do you think?
 
Old 08-28-2020, 04:08 PM   #21
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by michaelk View Post
To answer your second question, a single quote is used to identify a single character and double quotes are used for string literals. A string literal “x” is a string, containing the character ‘x’ and a null terminator ‘\0’. So “x” is two-character array in this case.
Thanks! I'm read that since but I'm still trying to work out what that literally means. so is the null terminator an escape character kind of like '\n' or '\t'? What is the null terminator exactly? I've read about the null character but for some reason it still doesn't seem to make sense to me. Why is it needed?



I know that an int is a 4byte value that is 32 bits when store in memory. Does the classification have something to do with how it's stored in memory?
 
Old 08-28-2020, 06:49 PM   #22
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,814

Rep: Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960
By definition.
Quote:
A string literal in C is a sequence of chars, terminated by a literal zero.
NULL in the ASCII table is the value of zero. You are correct \xx are ASCII escape sequences using numbers instead of a code i.e \n which is usually an octal value as compared to hex or decimal.

Since the string ends with a zero it is called a null terminated string...

https://en.wikipedia.org/wiki/Null_character
 
Old 08-29-2020, 04:00 AM   #23
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
By the way, there was an official "C answer book" published by Prentice-Hall.

AP
 
Old 08-29-2020, 04:10 AM   #24
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Thanks! I'm read that since but I'm still trying to work out what that literally means. so is the null terminator an escape character kind of like '\n' or '\t'? What is the null terminator exactly? I've read about the null character but for some reason it still doesn't seem to make sense to me. Why is it needed?
Again from K&R edition 1.
Quote:
The compiler places a null byte \0 at the end of each string so that programs which scan the string can find it's end.
I suppose that it is analogous to <ctrl+d> to signify the end of plain unmarked up text file.

Last edited by AnanthaP; 08-29-2020 at 04:12 AM.
 
Old 08-29-2020, 06:54 AM   #25
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by Arct1c_f0x View Post
Thanks! I'm read that since but I'm still trying to work out what that literally means. so is the null terminator an escape character kind of like '\n' or '\t'? What is the null terminator exactly? I've read about the null character but for some reason it still doesn't seem to make sense to me. Why is it needed?
A good deal of this can be answered and affirmed by using the debugger, GDB to look at the memory for a string.
Quote:
Originally Posted by Arct1c_f0x View Post
I know that an int is a 4byte value that is 32 bits when store in memory. Does the classification have something to do with how it's stored in memory?
The variable type has to do with how many bytes the variable is. It is not guaranteed the it is 32 bits.

You can print the size of variables in a test program, in bytes:
Code:
printf("Size of an int is: ", sizeof(int));
Coding is a lot of self learning, not to say people won't answer you, but if you try and see for yourself, you'll learn far more completely.
 
Old 08-29-2020, 08:20 AM   #26
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by Arct1c_f0x View Post
No actually by this point in the book the authors have only taught you about getchar() so I've been using ((getchar()) != EOF) as the conditional for a while loop. It seems to work although I also know how to use scanf to achieve the same result.

Funny thing though. I'm writing code in VS code and compiling and running the executables in the bash shell and whenever I want to use a prompt(printf) with input (scanf) I have to write it like this:

Code:
printf("Enter int: ", ) && scanf("%f", &exampleint);
And if I don't write it both like this separated by an AND statement, it will skip immediately past the scanf and move to the next lines without waiting for input. Weird right? Why does it do that do you think?
The code you've posted shouldn't compile:

Code:
#include <stdio.h>

int main () {
	int exampleint;

	printf("Enter int: ",) && scanf("%f", &exampleint);

	return 0;
}
Code:
$ gcc -Wall -o scan_test scan_test.c
scan_test.c: In function ‘main’:
scan_test.c:6:24: error: expected expression before ‘)’ token
  printf("Enter int: ", ) && scanf("%f", &exampleint);
                        ^
scan_test.c:4:6: warning: unused variable ‘exampleint’ [-Wunused-variable]
  int exampleint;
Corrected to:
Code:
#include <stdio.h>

int main () {
	int exampleint;

	printf("Enter int: ",) && scanf("%f", &exampleint);

	return 0;
}
Code:
$ gcc -Wall -o scan_test scan_test.c
scan_test.c: In f#include <stdio.h>

int main () {
	int exampleint;

	printf("Enter int: ",) && scanf("%f", &exampleint);

	return 0;
}unction ‘main’:
scan_test.c:6:33: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘int *’ [-Wformat=]
  printf("Enter int: ") && scanf("%f", &exampleint);
                                 ^
scan_test.c:6:24: warning: value computed is not used [-Wunused-value]
  printf("Enter int: ") && scanf("%f", &exampleint);
Finally coreected to (and without the && which hould be unneccesary and a bit nonsensical).

Code:
#include <stdio.h>

int main () {
	int exampleint;

	printf("Enter int: "); 
	
	scanf("%d", &exampleint);

	return 0;
}
it works as expected.

Perhaps there is something else in your code causing the problem?

Last edited by SoftSprocket; 08-29-2020 at 08:21 AM.
 
Old 08-29-2020, 09:38 AM   #27
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,923

Rep: Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040
To be on the safe side, a fflush(stdout); between the printf() and the scanf() wouldn't hurt either.
 
Old 08-29-2020, 03:03 PM   #28
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by AnanthaP View Post
Again from K&R edition 1.

I suppose that it is analogous to <ctrl+d> to signify the end of plain unmarked up text file.
I totally get it. So The compiler places a byte of all zeros 00000000 at the end of of the string (which is technically an array) so it can be easily determined where the array ends. I don't remember reading that in the second edition. Thanks for explaining it so clearly!
 
Old 08-29-2020, 03:15 PM   #29
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by rtmistler View Post
A good deal of this can be answered and affirmed by using the debugger, GDB to look at the memory for a string.The variable type has to do with how many bytes the variable is. It is not guaranteed the it is 32 bits.

You can print the size of variables in a test program, in bytes:
Code:
printf("Size of an int is: ", sizeof(int));
Coding is a lot of self learning, not to say people won't answer you, but if you try and see for yourself, you'll learn far more completely.
Sizeof() is a very tool thank you.

I know that coding is a lot of self learning.. That's what I've been doing but when I reach a point where I can't seem to get the level of clarification I need in order to understand to topic in the way that I feel that I should, I don't shy away from looking outwardly for assistance. I take no offense at your mention that coding 'is a lot self-learning', assuming otherwise on my part would be a sure sign of naiivety; rest assured that I'm quite aware of this fact, and that in addition I'm quite thankful for your input.
 
Old 08-29-2020, 03:22 PM   #30
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by SoftSprocket View Post
The code you've posted shouldn't compile:

Code:
#include <stdio.h>

int main () {
	int exampleint;

	printf("Enter int: ",) && scanf("%f", &exampleint);

	return 0;
}
Code:
$ gcc -Wall -o scan_test scan_test.c
scan_test.c: In function ‘main’:
scan_test.c:6:24: error: expected expression before ‘)’ token
  printf("Enter int: ", ) && scanf("%f", &exampleint);
                        ^
scan_test.c:4:6: warning: unused variable ‘exampleint’ [-Wunused-variable]
  int exampleint;
Corrected to:
Code:
#include <stdio.h>

int main () {
	int exampleint;

	printf("Enter int: ",) && scanf("%f", &exampleint);

	return 0;
}
Code:
$ gcc -Wall -o scan_test scan_test.c
scan_test.c: In f#include <stdio.h>

int main () {
	int exampleint;

	printf("Enter int: ",) && scanf("%f", &exampleint);

	return 0;
}unction ‘main’:
scan_test.c:6:33: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘int *’ [-Wformat=]
  printf("Enter int: ") && scanf("%f", &exampleint);
                                 ^
scan_test.c:6:24: warning: value computed is not used [-Wunused-value]
  printf("Enter int: ") && scanf("%f", &exampleint);
Finally coreected to (and without the && which hould be unneccesary and a bit nonsensical).

Code:
#include <stdio.h>

int main () {
	int exampleint;

	printf("Enter int: "); 
	
	scanf("%d", &exampleint);

	return 0;
}
it works as expected.

Perhaps there is something else in your code causing the problem?
Yeah you're completely right and I retyped a incorrect replacation of the code that I wrote. This is the real code and I actually found the && statement to be necessary for the scanf function to work properly. I'm not yet experienced enough yet to completely understand why but I believe that It's because for the the 'for loop' to continue to iterate the scanf HAS to take input because both the printf and scanf have to run properly. Correct me if I'm wrong. Here's the code:

Code:
// This is just a C file to practice with using arrays in C programming

#include <stdio.h>
#include <stdlib.h>

int main() {
    
    int selectIndex = 3;
    int i;


   
    int a[10] = {0};
    int b[20] = {1};
    int c[30] = {2};
    int d[40] = {3};

    for (i = 0; i < 10 ; i++)
        printf("enter a number: \n") && scanf("%d", &a[i]);
        // At first I typed printf and scanf statements on two separate lines. It didn't wait for scanf to take input but...
        // Then I fixed this by putting an AND statement between the printf and scanf which fixed the problem.
for (i=0;i<10;i++) {
    printf("%d\n", a[i]);
}

    return 0;

}
Output:
enter a number:
1
enter a number:
2
enter a number:
3
enter a number:
4
enter a number:
5
enter a number:
6
enter a number:
7
enter a number:
8
enter a number:
9
enter a number:
0
1
2
3
4
5
6
7
8
9
0


Works exactly as it should. Now if you understand why it won't work without those two functions being separated without an &&; I'd certainly be interested to know.

Last edited by Arct1c_f0x; 08-29-2020 at 03:23 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tar - write to stdout & create log files ( from stdout and stderr ) [solved] paziulek Linux - General 2 02-23-2014 12:26 PM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration