ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The redirectional opposite of getchar is clearly putchar.
Quote:
Originally Posted by GazL
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
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.
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.
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!
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:
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?
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?
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...
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.
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
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.
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:
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.
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!
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.
#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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.