C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Point Out Errors (Q.No. 2)
2.
Point out the error in the program?
#include<stdio.h>

int main()
{
    char ch;
    int i;
    scanf("%c", &i);
    scanf("%d", &ch);
    printf("%c %d", ch, i);
    return 0;
}
Error: suspicious char to in conversion in scanf()
Error: we may not get input for second scanf() statement
No error
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
23 comments Page 3 of 3.

Khushi said:   1 decade ago
No its not the case.

It will take input but the values that will get stored will be unexpected and it may lead to segmentation fault as well.

NITESH KHATRI said:   1 decade ago
Because this is the peculiarity of scanf(). After supplying data for the int i variable we would hit the enter key. What scanf() does it assigns the value what we gave to the variable i and keeps the enter key unread in the keyboard buffer (standard i/o buffer) , so when its time to supply another value the 2nd scanf() statement will read the enter key from the buffer thinking that user has entered the enter key. To AVOID this problem. WE USE THE FUNCTION flush (stdin). It is designed to remove or 'flush out' any data remaining in the buffer.

Pratham said:   1 decade ago
You will not get a chance to supply a character for 2nd scanf().


Post your comments here:

Your comments will be displayed after verification.