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
15
votes
4 answers

scanf() variable length specifier

How can I use a variable to specify the max number of chars scanf() should read in? For example using printf() you can use the * like so #define MAXVAL 5 printf("Print at maximum MAXVAL chars: %.*s\n", MAXVAL, "myStringHere"); This will only print…
CS Student
  • 1,613
  • 6
  • 24
  • 40
15
votes
6 answers

Is there any difference in using %f, %e, %g, %E or %G with scanf?

In C, is there any difference in the format specifiers %f, %e, %g, %E and %G when used to read into a float variable with scanf? That is, will the behaviour of the code snippet float x; scanf("%", &x); ever depend on the…
lee77
  • 1,493
  • 3
  • 10
  • 14
14
votes
2 answers

getchar_unlocked( ) VS scanf() VS cin

What is the difference among these three input functions in programming language. Do they input in different ways from each other? 1.getchar_unlocked() #define getcx getchar_unlocked inline void inp( int &n ) { n=0; int ch=getcx();int…
Anil Arya
  • 3,100
  • 7
  • 43
  • 69
14
votes
3 answers

Why won't the second scanf() execute

I'm trying to execute this block of code. #include int main(void) { printf("Start from here\n"); int e, f, g, h; scanf("%d,%d", &e, &f); scanf("%d, %d", &g, &h); printf("%d %d %d %d", e, f, g,…
Nathu
  • 449
  • 1
  • 4
  • 11
14
votes
4 answers

scanf without additional arguments in C

Is it allowed to use scanf(" ") without additional arguments to ignore initial whitespaces? I'm using getchar() to read the chars of a word, and I want to ignore the whitespaces before the word (whitespaces after are used to check the end of the…
raxell
  • 677
  • 6
  • 19
14
votes
3 answers

Ignore 'E' when reading double with sscanf

I have input such as "(50.1003781N, 14.3925125E)" .These are latitude and longitude. I want to parse this with sscanf(string,"(%lf%c, %lf%c)",&a,&b,&c,&d); but when %lf sees E after the number, it consumes it and stores it as number in exponential…
lllook
  • 729
  • 2
  • 7
  • 20
14
votes
10 answers

What's the difference between gets and scanf?

If the code is scanf("%s\n",message) vs gets(message) what's the difference?It seems that both of them get input to message.
Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57
14
votes
3 answers

C++ alternative for parsing input with sscanf

Assuming my program expects arguments of the form [ 0.562 , 1.4e-2 ] (i.e. pairs of floats), how should I parse this input in C++ without regular expressions? I know there are many corner cases to consider when it comes to user input, but let's…
ph4nt0m
  • 948
  • 4
  • 10
  • 23
14
votes
3 answers

Why does Qt change behaviour of sscanf()?

I have noticed, Qt (4.8) changes the behaviour of sscanf(). Without Qt sscanf() works as usual, but with, it takes only localized strings. Here's a minimized example: Without Qt (plain C++) int main(int argc, char *argv[]) { float f; …
ollo
  • 24,797
  • 14
  • 106
  • 155
14
votes
3 answers

using scanf to read a string and an int separated by /

The input consists a string and an integer, which are separated by a '/', like this: hello/17 And I want to read the input into a string and an int, like this: char str[20]; int num; scanf("%s/%d", str, &num); // this how I tried to do it. I…
Alcott
  • 17,905
  • 32
  • 116
  • 173
14
votes
4 answers

whitespace in the format string (scanf)

Consider the following code: #include int main() { int i=3, j=4; scanf("%d c %d",&i,&j); printf("%d %d",i,j); return 0; } It works if I give 2c3 or 2 c 3 or 2c 3 as input if I have to change the value of variables. What…
Vaibhav Agarwal
  • 1,124
  • 4
  • 16
  • 25
14
votes
4 answers

sscanf doesn't move, scanning same integer everytime

I have a string that has ints and I'm trying to get all the ints into another array. When sscanf fails to find an int I want the loop to stop. So, I did the following: int i; int getout = 0; for (i = 0; i < bsize && !getout; i++) { if…
MinaHany
  • 1,015
  • 4
  • 16
  • 32
13
votes
5 answers

Program doesn't wait for user input with scanf("%c",&yn);

This is the basic code to a program I am writing to practise using files in C. I am trying to detect whether the output file already exists and if it does exist I want to ask the user if they would like to overwrite it or not. This is the reason…
user1083734
  • 4,815
  • 8
  • 24
  • 24
13
votes
3 answers

C - sscanf not working

I'm trying to extract a string and an integer out of a string using sscanf: #include int main() { char Command[20] = "command:3"; char Keyword[20]; int Context; sscanf(Command, "%s:%d", Keyword, &Context); …
kazinix
  • 28,987
  • 33
  • 107
  • 157
13
votes
8 answers

Why scanf must take the address of operator

As the title says, I always wonder why scanf must take the address of operator (&).
Wazery
  • 15,394
  • 19
  • 63
  • 95