Scanf ("%d %c%f" , &a,&b,&g); AND . . Scanf ("%d%c%f" , &a,&b,&g);

While executing the second one with no space before %c , it just takes first and second value , but not the g . While in first one all three value are taken . Why?

28th Nov 2018, 12:47 PM
Jishnu - avatar
1 Answer
+ 4
Create a C project here and paste the whole code there, then attach it to your post for close inspection. Edit: From cppreference: "All conversion specifiers other than [, c, and n consume and discard all leading whitespace characters (determined as if by calling `isspace`) before attempting to parse the input. These consumed characters do not count towards the specified maximum field width." As a simple example let's assume we want to get one integer and one char like this scanf ("%d%c" , &a, &b); Without putting a space between them, since %c won't consume any whitespaces, after getting the integer and store it in `a` you hit the Enter key. The ASCII value of the linefeed (Enter) character (10) is still in the input buffer and it gets stored in `b` and the job is done. So, to avoid confusion it's better to seperate each specifier with a single space from each other like so: scanf ("%d %c %f" , &a, &b, &g); _____ https://en.cppreference.com/w/c/io/fscanf
28th Nov 2018, 1:44 PM
Babak - avatar