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
40
votes
8 answers

Getting multiple values with scanf()

I am using scanf() to get a set of ints from the user. But I would like the user to supply all 4 ints at once instead of 4 different promps. I know I can get one value by doing: scanf( "%i", &minx); But I would like the user to be able to do…
Josh Curren
  • 10,171
  • 17
  • 62
  • 73
38
votes
4 answers

What is the effect of trailing white space in a scanf() format string?

What is difference between scanf("%d") and scanf("%d ") in this code, where the difference is the trailing blank in the format string? #include int main(void) { int i, j; printf("enter a value for j "); scanf("%d ",&j); …
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40
38
votes
2 answers

C/C++ printf() before scanf() issue

I'm using Eclipse to code in C/C++ and I'm struggling with what might be something pretty easy. In my code below I use printf() and after scanf(). Althougth printf is written before scanf() the output differs. I was able to find out something about…
quapka
  • 2,799
  • 4
  • 21
  • 35
37
votes
1 answer

C program yielding different results on different machines despite using the same fixed length data types

Simple program I made when I was experimenting with inttypes.h: #include #include #include bool get_bit(uint32_t x, uint8_t n) { x >>= n; return x & 1; } int main() { uint32_t x; uint8_t n; …
Clyde B.
  • 399
  • 2
  • 4
37
votes
4 answers

What is the difference between %f and %lf in C?

I have seen these two parameters in a C example in a C book, but the author didn't elaborate what the difference between the two is. I know that %f specifies that a float should take its place. I have tried looking this up but have had a hard time…
committedandroider
  • 8,711
  • 14
  • 71
  • 126
37
votes
5 answers

What does the scanf function return?

What is the value returned by scanf when: int g; int p = scanf("%d", &g); // Originally: int p = scanf("%d", g); I know that the signature of the scanf function is: int scanf(const char *format, ...) What is the int value returned from this…
Rachit
  • 501
  • 1
  • 4
  • 4
36
votes
2 answers

Get number of characters read by sscanf?

I'm parsing a string (a char*) and I'm using sscanf to parse numbers from the string into doubles, like so: // char* expression; double value = 0; sscanf(expression, "%lf", &value); This works great, but I would then like to continue parsing the…
Alexis King
  • 43,109
  • 15
  • 131
  • 205
35
votes
10 answers

Looking for C# equivalent of scanf

I used to code in C language in the past and I found the scanf function very useful. Unfortunately, there is no equivalent in C#. I am using using it to parse semi-structured text files. I found an interresting example of scanf implementation here.…
Larry
  • 17,605
  • 9
  • 77
  • 106
35
votes
2 answers

Read int values from a text file in C

I have a text file that contains the following three lines: 12 5 6 4 2 7 9 I can use the fscanf function to read the first 3 values and store them in 3 variables. But I can't read the rest. I tried using the fseek function, but it works only on…
elh mehdi
  • 359
  • 1
  • 3
  • 3
35
votes
3 answers

Difference between scanf and scanf_s

What is the difference between scanf and scanf_s? In the university I have been taught and I am using scanf, but at my personal computer Visual Studio keeps sending this warning. error C4996: 'scanf': This function or variable may be unsafe.…
Tony Andreev
  • 421
  • 1
  • 6
  • 9
34
votes
6 answers

What will happen if '&' is not put in a 'scanf' statement?

I had gone to an interview in which I was asked the question: What do you think about the following? int i; scanf ("%d", i); printf ("i: %d\n", i); I responded: The program will compile successfully. It will print the number incorrectly but it…
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
34
votes
2 answers

Why is glibc's sscanf vastly slower than fscanf on Linux?

I am using GCC 4.8 and glibc 2.19 on an x86_64 Linux. While playing with different input methods for a different question, I compared fscanf and sscanf. Specifically, I would either use fscanf on the standard input directly: char s[128]; int…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
33
votes
5 answers

How to read numbers separated by space using scanf

I want to read numbers(integer type) separated by spaces using scanf() function. I have read the following: C, reading multiple numbers from single input line (scanf?) how to read scanf with spaces It doesn't help me much. How can I read numbers…
Jainendra
  • 24,713
  • 30
  • 122
  • 169
32
votes
7 answers

How do you read scanf until EOF in C?

I have this but once it reaches the supposed EOF it just repeats the loop and scanf again. int main(void) { char words[16]; while(scanf("%15s", words) == 1) printf("%s\n", words); return 0; }
JJRhythm
  • 633
  • 3
  • 10
  • 16
31
votes
3 answers

What does an asterisk in a scanf format specifier mean?

So I stumbled across this code and I haven't been able to figure out what the purpose of it is, or how it works: int word_count; scanf("%d%*c", &word_count); My first thought was that %*d was referencing a char pointer or disallowing word_count…
Joel
  • 5,732
  • 4
  • 37
  • 65