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
18
votes
6 answers

How can I prevent scanf() to wait forever for an input character?

I want to fulfill the following things in a console application: If user inputs a character, the application will do the corresponding task. For example, if user inputs 1, the program will do task 1, if user inputs q, the program will quit; If…
user3208536
  • 191
  • 1
  • 1
  • 4
18
votes
10 answers

Scanf skips every other while loop in C

I'm trying to develop a simple text-based hangman game, and the main game loop starts with a prompt to enter a guess at each letter, then goes on to check if the letter is in the word and takes a life off if it isn't. However, when I run the game…
benwad
  • 6,414
  • 10
  • 59
  • 93
18
votes
3 answers

equivalent of Console.ReadLine() in c++

My teacher just gave me an assignment in c++ and I am trying to get a string with scanf but it only get the last characters typed. Can anyone help me please? I am looking for the equivalent of console.readline() in c++. edit : I must also be able…
ESD
  • 675
  • 2
  • 12
  • 35
17
votes
2 answers

Does sscanf require a null terminated string as input?

A recently discovered explanation for GTA lengthy load times(1) showed that many implementations of sscanf() call strlen() on their input string to set up a context object for an internal routine shared with other scanning functions (scanf(),…
chqrlie
  • 131,814
  • 10
  • 121
  • 189
17
votes
2 answers

Is scanf's "regex" support a standard?

Is scanf's "regex" support a standard? I can't find the answer anywhere. This code works in gcc but not in Visual Studio: scanf("%[^\n]",a); It is a Visual Studio fault or a gcc extension ? EDIT: Looks like VS works, but have to consider the…
bratao
  • 1,980
  • 3
  • 21
  • 38
17
votes
8 answers

Are there any practical applications for the format %n in printf/scanf family?

int x; printf("hello %n World\n", &x); printf("%d\n", x);
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
17
votes
5 answers

Width as a variable when using fscanf

I am trying to read in a certain portion of a file and that amount of data is different per line but I know how how many bytes of info I want. Like this: 5bytes.byte1byte2byte3byte4byte5CKSum //where # of bytes varies for each line (and there is…
Nick Sinas
  • 2,594
  • 11
  • 42
  • 51
17
votes
4 answers

How to skip a line when fscanning a text file?

I want to scan a file and skip a line of text before reading. I tried: fscanf(pointer,"\n",&(*struct).test[i][j]); But this syntax simply starts from the first line.
NLed
  • 1,845
  • 14
  • 39
  • 68
17
votes
6 answers

using scanf function with pointers to character

I have written the following piece of code: int main() { char arrays[12]; char *pointers; scanf("%s", arrays); scanf("%s", pointers); printf("%s", arrays); printf("%s", pointers); return 0; } Why does it give an error when I write…
Saurabh.V
  • 320
  • 1
  • 2
  • 9
17
votes
2 answers

Correct way to use scanf / printf (and family) with fixed size types?

Reading this SO question, I started wondering - what is the correct way to use scanf/printf (and family) with fixed size types? For example, if I have short int or int, I'd use %hd and %d respectively - fine. But what if I have int16_t? short int…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
16
votes
2 answers

ANSI C (ISO C90): Can scanf read/accept an unsigned char?

Simple question: Can scanf read/accept a "small integer" into an unsigned char in ANSI C? example code un_char.c: #include #include int main(void) { unsigned char character; scanf("%hhu", &character); return…
Tim
  • 161
  • 1
  • 3
16
votes
5 answers

Reading a single character in C

I'm trying to read a character from the console (inside a while loop). But it reads more than once. Input: a Output: char : a char : char : ' Code: while(..) { char in; scanf("%c",&in); } How can i read only 'a'?
g3d
  • 453
  • 2
  • 6
  • 14
15
votes
5 answers

scanf with const int* const works, but shouldn't

I have code equivalent to the following: const int* const n = new int; printf("input: "); scanf("%d", n); delete n; Now, since n is a pointer to a CONSTANT integer, this shouldn't work (I'm expecting a compiler error). However, this seems to work…
Crazycolorz5
  • 747
  • 6
  • 12
15
votes
5 answers

Using scanf in a while loop

Probably an extremely simple answer to this extremely simple question: I'm reading "C Primer Plus" by Pratta and he keeps using the example while (scanf("%d", &num) == 1)... Is the == 1 really necessary? It seems like one could just write: while…
Tyler Brock
  • 29,626
  • 15
  • 79
  • 79
15
votes
1 answer

sscanf for doubles

This is a simple problem, but I can't see it: char *s = "f 8.649292" ; double d ; sscanf( s, "f %f", &d ) ; printf( "d is %f\n", d ) ; Why is d not containing the double value 8.649292?
bobobobo
  • 64,917
  • 62
  • 258
  • 363