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

What is the result of `strtod("3ex", &end)` supposed to be? What about `sscanf`?

In my experiments this expression double d = strtod("3ex", &end); initializes d with 3.0 and places end pointer at 'e' character in the input string. This is exactly as I would expect it to behave. The 'e' character might look as a beginning of…
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
11
votes
3 answers

What is the modern equivalent (C++) style for the older (C-like) fscanf method?

What is the best option if I want to "upgrade" old C-code to newer C++ when reading a file with a semicolon delimiter: /* reading in from file C-like: */ fscanf(tFile, "%d", &mypost.nr); /*delimiter ; */ fscanf(tFile, " ;%[^;];",…
Chris_45
  • 8,769
  • 16
  • 62
  • 73
11
votes
8 answers

Input in C. Scanf before gets. Problem

I'm pretty new to C, and I have a problem with inputing data to the program. My code: #include #include #include int main(void) { int a; char b[20]; printf("Input your ID: "); scanf("%d", &a); …
Dmitri
  • 2,451
  • 6
  • 35
  • 55
11
votes
9 answers

How to ignore floating number in scanf("%d")?

If user enters floating number for an integer variable I want to print invalid input. is that possible? int a; scanf("%d",&a); // if user enters 4.35 print invalid input I have tried for characters like this if(scanf("%d",&a)==1); else…
niko
  • 9,285
  • 27
  • 84
  • 131
11
votes
4 answers

Why scanf("%d", [...]) does not consume '\n'? while scanf("%c") does?

Here, I saw this statement in the accepted answer: Most of the conversion specifiers skip leading whitespace including newlines but %c does not. For me it is not clear the rationale under this different behaviors, I would have expected a uniform…
Alessandro S.
  • 875
  • 12
  • 24
11
votes
1 answer

Format specifier for scanning long unsigned int

I am trying to take two variables as input using this code snippet:- unsigned int i; unsigned long int j; scanf("%u",i); scanf("%lu",j); But this give rise to the following warnings :- warning: format ‘%u’ expects argument of type ‘unsigned int…
user1391362
11
votes
2 answers

How do we test the return values from the scanf() function?

I want to confirm if a value returned from the scanf() function is a floating number or not. How can I do that? My code is not running as it should if the wrong data types are supplied to the scanf() function. Similarly, how would I confirm if a…
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
10
votes
5 answers

read char from console

I write console application which performs several scanf for int And after it ,I performs getchar : int x,y; char c; printf("x:\n"); scanf("%d",&x); printf("y:\n"); scanf("%d",&y); c = getchar(); as a result of this I get c = '\n',despite the …
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
10
votes
3 answers

While-loop ignores scanf the second time

#include int main () { char loop='y'; while(loop != 'n') { printf("loop? "); scanf("%c", &loop); if(loop != 'y') { loop='n'; } } return 0; } If I type in 'y' he restart the…
user1069968
  • 267
  • 3
  • 5
  • 14
10
votes
3 answers

Is there an inverse function of *SysUtils.Format* in Delphi

Has anyone written an 'UnFormat' routine for Delphi? What I'm imagining is the inverse of SysUtils.Format and looks something like this UnFormat('a number %n and another %n',[float1, float2]); So you could unpack a string into a series of…
Andy W
  • 103
  • 1
  • 5
10
votes
2 answers

Using ifstream as fscanf

Assume that I have an input as follows: N (X_1,Y_1) (X_2,Y_2) .... (X_N, Y_N) where N, X_i and Y_i are integers. An example: 2 (55,1) (521,7) To read this, I can do something like this(assume all variables are defined, etc.): fscanf(fin,"%d…
kolistivra
  • 4,229
  • 9
  • 45
  • 58
10
votes
3 answers

While scanf!=EOF or scanf==1?

Ceteris paribus (well formed data, good buffering practices and what not), is there a reason why I prefer to loop while the return of scanf is 1, rather than !EOF? I may have read this somewhere, or whatever, but I may have it wrong as well. What do…
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
10
votes
2 answers

scanf %u negative number?

I have tried scanf("%u",&number) and I have entered negative number the problem is when I printf("%d",number) I get the negative number. I thought this will prevent me from reading negative number. Are scanf("%d",&number) and scanf("%u",&number) the…
rondino
  • 395
  • 2
  • 3
  • 10
10
votes
5 answers

Is there any function to get an unlimited input string from standard input

The condition is: I want to input a line from standard input, and I don't know the size of it, maybe very long. method like scanf, getsneed to know the max length you may input, so that your input size is less than your buffer size. So Is there any…
Daizy
  • 331
  • 2
  • 5
  • 12
10
votes
2 answers

scanf field width string overflow

Which one of the following is safe regarding buffer overflow? char buf[10] = {0}; scanf("%10s", buf); or char buf[10] = {0}; scanf("%9s", buf); From what I've read I'm going for the second (sizeof minus one), but the matter is quite subtle and…
jimis
  • 794
  • 1
  • 9
  • 14