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
32
votes
9 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
  • 16,649
  • 8
  • 71
  • 100
32
votes
5 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…
Ashish Ahuja
  • 4,798
  • 9
  • 48
  • 63
32
votes
5 answers

Reading numbers from a text file into an array in C

I'm a programming noob so please bear with me. I'm trying to read numbers from a text file into an array. The text file, "somenumbers.txt" simply holds 16 numbers as so "5623125698541159". #include main() { FILE *myFile; myFile =…
Vonti
  • 335
  • 1
  • 3
  • 7
32
votes
3 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,351
  • 3
  • 19
  • 34
32
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
  • 40,717
  • 14
  • 119
  • 194
31
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
  • 605
  • 3
  • 9
  • 16
31
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
  • 428,875
  • 83
  • 813
  • 1,025
31
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
  • 381
  • 1
  • 5
  • 9
30
votes
4 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
  • 23,305
  • 30
  • 116
  • 165
29
votes
6 answers

scanf: "%[^\n]" skips the 2nd input but " %[^\n]" does not. why?

Consider the following code: #include int main (void) { char str1[128], str2[128], str3[128]; printf ("\nEnter str1: "); scanf ("%[^\n]", str1); printf ("\nstr1 = %s", str1); printf ("\nEnter str2: "); scanf ("%[^\n]",…
phoxis
  • 52,327
  • 12
  • 74
  • 110
29
votes
6 answers

What does the scanf function return?

I know that the signature of the scanf function is: int scanf(const char *format, ...) What is the int value returned from this function?
Rachit
  • 421
  • 1
  • 4
  • 4
28
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
  • 3,791
  • 1
  • 25
  • 41
28
votes
5 answers

What should I use instead of sscanf?

I have a problem that sscanf solves (extracting things from a string). I don't like sscanf though since it's not type-safe and is old and horrible. I want to be clever and use some more modern parts of the C++ standard library. What should I use…
Ben Hymers
  • 22,821
  • 15
  • 55
  • 79
27
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,064
  • 6
  • 23
  • 40
26
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
  • 269
  • 1
  • 3
  • 3