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.

6516 questions
211
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,425
  • 8
  • 38
  • 50
183
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
  • 19,496
  • 29
  • 73
  • 85
155
votes
2 answers

Reading a string with scanf

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,456
  • 2
  • 19
  • 28
148
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
  • 34,183
  • 49
  • 147
  • 200
135
votes
4 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
  • 14,309
  • 20
  • 66
  • 95
135
votes
8 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
  • 13,819
  • 7
  • 31
  • 62
116
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,491
  • 3
  • 14
  • 15
108
votes
14 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,457
  • 3
  • 12
  • 15
104
votes
5 answers

scanf() leaves the new line char 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 a char: "); …
ipkiss
  • 12,171
  • 26
  • 81
  • 118
86
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
  • 4,661
  • 13
  • 41
  • 48
73
votes
10 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
  • 449
  • 30
  • 101
  • 235
72
votes
13 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
  • 100,604
  • 94
  • 332
  • 495
69
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
  • 4,838
  • 10
  • 35
  • 36
67
votes
6 answers

What is the difference between sscanf or atoi to convert a string to an integer?

gcc 4.4.4 c89 What is better to convert a string to an integer value. I have tried 2 different methods atoi and sscanf. Both work as expected. char digits[3] = "34"; int device_num = 0; if(sscanf(digits, "%d", &device_num) == EOF) { …
ant2009
  • 30,351
  • 141
  • 365
  • 559
67
votes
11 answers

Why do I have to specify data type each time in C to printf() and scanf()?

As you can see from the code snippet below, I have declared one char variable and one int variable. When the code gets compiled, it must identify the data types of variables str and i. Why do I need to tell again during scanning my variable that…
Akki619
  • 2,224
  • 2
  • 22
  • 45
1
2 3
99 100