What does space mean in scanf?

#include <stdio.h> int main(int argc, char* argv[]) { char c; scanf(" %c", &c); printf("%c\n", c); return 0; } [ root@test ]# ./scanf a a [ root@test ]# ./scanf h h 

It always seems to coincide, does space exist, why?

+7
source share
6 answers

White space (for example, spaces, tabs, or newlines) in a format string matches any number of spaces, including none , in the input.

http://www.manpagez.com/man/3/scanf/

+9
source

A space in scanf format means skip all spaces from the current position. Since most scanf format specifiers already skip all spaces before trying to read anything, space is not used in scanf format most of the time. However, when you use a format specifier that does not ignore spaces, including format space when necessary (to make it skip), it might make sense.

Qualifiers that do not ignore spaces are c [ and n . Thus, defining a space in front of one of these qualifiers matters. Otherwise, it does not matter. In your specific example, spaces are ignored specifically because you used a space in scanf format (since you are using %c ). Try to remove the space and see what happens.

+4
source

The space character in the format string will ignore 0 or more space characters from input to the first character without spaces.

+2
source

The documentation for scanf says:

A directive consisting of one or more space characters must be executed by entering the input until a more valid input is read, or until the first byte, which is not a space character, that remains unread.

And

Entering space characters (as indicated in isspace) should be omitted if the conversion specification does not include the conversion specifier [, c, C or n.

This means that space is required in your formatted string. However, if you are going to read an integer, space would be superfluous:

 /* equivalent statements */ scanf(" %d %d %d", &v1, &v2, &v3); scanf("%d%d%d", &v1, &v2, &v3); 
+1
source

Space simply means that space is accepted as input. You might want to read this related thread about the dangers of using scanf, not fgets.

How can you enter spaces with scanf?

0
source

% c will not skip char spaces, as numeric format specifiers do. Therefore, if you use:

 #include<stdio.h> int main(int argc, char* argv[]){ char c; scanf("%c", &c); printf("%c\n", c); scanf("%c", &c); // Try running with and without space printf("%c\n", c); return 0; } 

It is very likely that the previous space character in the input buffer will be made in the second scanf, and you will not get a chance to type. A space up to% c will cause scanf to skip any space character in the input buffer so that you can correctly enter your input. Sometimes, to get the same effect, people write:

 fflush(stdin); scanf("%c" &c); 

But this is considered very poor programming, as C Standard defines the behavior of fflush (stdin) undefined. Therefore, always use a space in the format string unless you have a specific reason to capture spaces.

0
source

All Articles