Hi all, I am having a rather interesting problem. I was doing a program and I was trying to read a string in a subfunction after reading an integer in main function. Now a problem occured, I couldnt read the string. The reading of string does not work. It worked properly when the subfunction was called without reading the integer. The problem showed up when I tried to call the fucntion after reading the integer. This is a small program which depicts the bug. Code: #include<stdio.h> void test() { char x; printf("testt"); scanf("%c",&x); } main() { int c; scanf("%d", &c); test(); } I would love to know the reason for this strange problem. I tried flushing the stdin also. It didnt work as well. Thanks
Also if anyone wants a look at the original program where the bug was found, please find the page below. I cannot post the link because it is not allowed I guess. Github -> sajinmp -> nonweb-blog I would love every suggestion and feedback from you to improve it. It has just started and thats when i met with this bug. Thanks
char is a single character, not a string, and %c is the format string for reading a single character. Try defining x as an array of char instead (make sure it is large enough for the string you want to read) and use %s. "Does not work" is not a very helpful description of the problem. What exactly did you input, and what exactly happened? There is no display so how do you know x doesn't contain what you put into it?
Sorry about that. I expected to give a simple title and an example hoping that i could make it clear to you. I will give an expanded question then. Suppose I have a code Code: #include<stdio.h> void test { char a[10]; printf("Read a string : "); gets(a); } main() { int x; printf("Enter a number : "); scanf("%d",&x); test(); } Suppose we execute the above code. Running it Code: smp@debian$: gcc <filename> -o output smp@debian$: ./output Now I should be able to read an integer as well as a string. The output of executing the program Code: Enter a number : 10 #Number could be read. Read a string : #String could not be read. It just went past without waiting for an input. This is the problem. I hope you can understand it now. And sorry about the title. I thought of posting the problem but then it would turn out too big and when I thought of editing it, i couldnt edit a thread. Sorry about that Thanks for your help
This is down to the way scanf works. When you enter a number you press 1, 0, Enter. scanf reads the 1 and 0, then sees that the next character is Enter, which isn't part of a number, so it stops reading. It converts 1 and 0 to 10. The gets then looks at the input stream, which contains an Enter, so in fact it has read your string, but not the string you thought it was reading. If you want "10<Enter>" converting to a number, your best bet is to ditch scanf and use gets instead (reading into a temporary string), then use atoi to convert the input string to a number.
ok, so its the <enter> that makes the problem. Thanks boss. I got it figured out now. The <Enter> is basically a newline character. So this newline character is still in the input stream. To get rid of it I just need to call a getchar() function. It automatically reads this '\n' and moves on to the next line. So the simple solution is to call a getchar() function after every scanf functions. Thanks xpi0t0s.