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
30
votes
1 answer

How to use sscanf in loops?

Is there a good way to loop over a string with sscanf? Let's say I have a string that looks like this: char line[] = "100 185 400 11 1000"; and I'd like to print the sum. What I'd really like to write is this: int n, sum = 0; while (1 ==…
Andrew H. Hunter
  • 561
  • 1
  • 4
  • 8
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
  • 60,131
  • 14
  • 81
  • 117
29
votes
9 answers

How to scanf only integer?

I want the code to run until the user enters an integer value. The code works for char and char arrays. I have done the following: #include int main() { int n; printf("Please enter an integer: "); while(scanf("%d",&n) != 1) …
ani627
  • 5,578
  • 8
  • 39
  • 45
28
votes
7 answers

How to read from input until newline is found using scanf()?

I was asked to do a work in C when I'm supposed to read from input until there's a space and then until the user presses enter. If I do this: scanf("%2000s %2000s", a, b); It will follow the 1st rule but not the 2nd. If I write: I am smart What I…
brunoais
  • 6,258
  • 8
  • 39
  • 59
28
votes
1 answer

How to use int16_t or int32_t with functions like scanf

The way that I understand int16_t or int32_t in C is that they are typedefed to be 16 and 32 bit numbers respectively on your computer. I believe you would use these when you need to guarentee a number is 16 or 32 bits because different systems do…
guribe94
  • 1,551
  • 3
  • 15
  • 29
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
  • 25,586
  • 16
  • 59
  • 84
26
votes
7 answers

C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf

I have this block of code (functions omitted as the logic is part of a homework assignment): #include int main() { char c = 'q'; int size; printf("\nShape (l/s/t):"); scanf("%c",&c); printf("Length:"); …
Snow_Mac
  • 5,727
  • 17
  • 54
  • 80
26
votes
5 answers

Simple C scanf does not work?

If I try something such as: int anint; char achar; printf("\nEnter any integer:"); scanf("%d", &anint); printf("\nEnter any character:"); scanf("%c", &achar); printf("\nHello\n"); printf("\nThe integer entered is %d\n", anint); printf("\nThe char…
John
  • 1,110
  • 3
  • 14
  • 28
26
votes
7 answers

Why does scanf ask twice for input when there's a newline at the end of the format string?

#include #include #include char *method1(void) { static char a[4]; scanf("%s\n", a); return a; } int main(void) { char *h = method1(); printf("%s\n", h); return 0; } When I run the code…
joy
  • 721
  • 2
  • 11
  • 18
26
votes
3 answers

How to find EOF through fscanf?

I am reading matrix through file with the help of fscanf(). How can i find EOF? Even if i try to find EOF after every string caught in arr[] then also i am not able to find it. with the help of count i am reading the input file -12 23 3 1 2 4 int…
karthik
  • 351
  • 1
  • 3
  • 9
25
votes
4 answers

scanf() skip variable

In C, using scanf() with the parameters, scanf("%d %*d", &a, &b) acts differently. It enters value for just one variable not two! Please explain this! scanf("%d %*d", &a, &b);
ross
  • 388
  • 2
  • 4
  • 10
25
votes
2 answers

Using "\n" in scanf() in C

I mistakenly used scanf("%d\n",&val); in one of my programmes, I could not understand the behavior, the function showed. int main(){ int val; scanf("%d\n", &val); printf("%d\n", val); return 0; } Now the program required 2 integer…
nishchay2192
  • 353
  • 1
  • 3
  • 6
25
votes
3 answers

Format specifier in scanf for bool datatype in C

I am using bool datatype in C std99 whose definitions are defined in . Now I want the user to give me input. What format specifier I must use in scanf to input the boolean value of 1 byte from the user and then manipulate it afterwards in…
pranavk
  • 1,774
  • 3
  • 17
  • 25
24
votes
3 answers

Safer but easy-to-use and flexible C++ alternative to sscanf()

When I need to scan in values from a bunch of strings, I often find myself falling back to C's sscanf() strictly because of its simplicity and ease of use. For example, I can very succinctly pull a couple double values out of a string with: string…
Jason R
  • 11,159
  • 6
  • 50
  • 81
24
votes
2 answers

getc Vs getchar Vs Scanf for reading a character from stdin

Of the below three functions: getc getchar & scanf which is the best one for reading a character from stdin and why? Are there any known disadvantages or limitations for any of these functions which makes one better than the other?
Jay
  • 24,173
  • 25
  • 93
  • 141