Questions tagged [scanf]

Questions related to the scanf() family of functions in the C runtime library, which read and convert formatted data. (Includes scanf(), sscanf(), fscanf(), and their variadic equivalents.)

The scanf() function reads data with specified format from stdin. Originating from the C runtime library, scanf() is present in many other programming languages.

The scanf() function, in the C runtime library, reads input text for numbers and other data types from standard input. It returns the total number of items successfully matched, which can be less than the number requested. If the input stream is exhausted or reading from it otherwise fails before any items are matched, EOF is returned. The C prototype for scanf() is as follows:

int scanf(const char *format, ...);

The specification of conversion identifiers in format is a rich treasure of solutions—and occasional slight incompatibilities. The core functionality goes back to the beginnings of C. All of the ... parameters must be a pointer—note that an array name is a pointer type—to matching types of data.

Though popular for simple input in instructional programs, scanf() does not handle errors or complicated situations well, and many programmers recommend using alternative input techniques.

The tag is used for questions related to the use of the scanf(), sscanf(), and fscanf() functions and their derivatives. sscanf() reads from a string buffer; fscanf() reads from a FILE *; vscanf(), vsscanf(), vfscanf do—respectively—the same using a va_list instead of an explicit list of variables.

See also:

scanf documentation.

scanf() is the converse operation to and shares many of the same format specifiers.

7386 questions
228
votes
4 answers

What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)

What is the difference between %d and %i when used as format specifiers in printf and scanf?
Ayoub M.
  • 4,690
  • 10
  • 42
  • 52
194
votes
5 answers

Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float? Example code: double d; scanf("%lf", &d); printf("%f", d);
raldi
  • 21,344
  • 33
  • 76
  • 86
178
votes
11 answers

How do you allow spaces to be entered using scanf?

Using the following code: char *name = malloc(sizeof(char) + 256); printf("What is your name? "); scanf("%s", name); printf("Hello %s. Nice to meet you.\n", name); A user can enter their name but when they enter a name with a space like Lucas…
Kredns
  • 36,461
  • 52
  • 152
  • 203
169
votes
2 answers

Why does reading into a string buffer with scanf work both with and without the ampersand (&)?

I'm a little bit confused about something. I was under the impression that the correct way of reading a C string with scanf() went along the lines of (never mind the possible buffer overflow, it's just a simple example) char string[256]; scanf( "%s"…
abeln
  • 3,749
  • 2
  • 22
  • 31
160
votes
5 answers

What is the format specifier for unsigned short int?

I have the following program #include int main(void) { unsigned short int length = 10; printf("Enter length : "); scanf("%u", &length); printf("value is %u \n", length); return 0; } Which when compiled using gcc…
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
151
votes
9 answers

What can I use for input conversion instead of scanf?

I have very frequently seen people discouraging others from using scanf and saying that there are better alternatives. However, all I end up seeing is either "don't use scanf" or "here's a correct format string", and never any examples of the…
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
138
votes
11 answers

How to do scanf for single char in C

In C: I'm trying to get char from the user with scanf and when I run it the program don't wait for the user to type anything... This is the code: char ch; printf("Enter one char"); scanf("%c", &ch); printf("%c\n",ch); Why is not working?
Yuval
  • 1,721
  • 4
  • 16
  • 15
130
votes
7 answers

scanf() leaves the newline character in the buffer

I have the following program: int main(int argc, char *argv[]) { int a, b; char c1, c2; printf("Enter something: "); scanf("%d", &a); // line 1 printf("Enter other something: "); scanf("%d", &b); // line 2 printf("Enter…
ipkiss
  • 13,311
  • 33
  • 88
  • 123
112
votes
13 answers

Reading string from input with space character?

I'm using Ubuntu and I'm also using Geany and CodeBlock as my IDE. What I'm trying to do is reading a string (like "Barack Obama") and put it in a variable: #include int main(void) { char name[100]; printf("Enter your name: "); …
Hieu Nguyen
  • 1,497
  • 3
  • 12
  • 15
98
votes
6 answers

How to prevent scanf causing a buffer overflow in C?

I use this code: while ( scanf("%s", buf) == 1 ){ What would be the best way to prevent possible buffer overflow so that it can be passed strings of random lengths? I know I can limit the input string by calling for example: while ( scanf("%20s",…
goe
  • 5,207
  • 14
  • 45
  • 49
92
votes
11 answers

How can I read an input string of unknown length?

If I don't know how long the word is, I cannot write char m[6];, The length of the word is maybe ten or twenty long. How can I use scanf to get input from the keyboard? #include int main(void) { char m[6]; printf("please input a…
showkey
  • 482
  • 42
  • 140
  • 295
89
votes
9 answers

Disadvantages of scanf

I want to know the disadvantages of scanf(). In many sites, I have read that using scanf might cause buffer overflows. What is the reason for this? Are there any other drawbacks with scanf?
karthi_ms
  • 5,568
  • 11
  • 38
  • 39
81
votes
9 answers

sscanf in Python

I'm looking for an equivalent to sscanf() in Python. I want to parse /proc/net/* files, in C I could do something like this: int matches = sscanf( buffer, "%*d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %*X %*X:%*X %*X:%*X %*X %*d %*d %ld…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
71
votes
4 answers

Going through a text file line by line in C

I have been working on a small exercise for my CIS class and am very confused by the methods C uses to read from a file. All that I really need to do is read through a file line by line and use the information gathered from each line to do a few…
Dan Bradbury
  • 2,071
  • 2
  • 21
  • 27
70
votes
6 answers

How to read string from keyboard using C?

I want to read a string entered by the user. I don't know the length of the string. As there are no strings in C I declared a pointer: char * word; and used scanf to read input from the keyboard: scanf("%s" , word) ; but I got a segmentation…
mainajaved
  • 7,905
  • 10
  • 36
  • 44
1
2 3
99 100