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

What is the difference between sscanf or atoi to convert a string to an integer?

gcc 4.4.4 c89 What is better to convert a string to an integer value. I have tried 2 different methods atoi and sscanf. Both work as expected. char digits[3] = "34"; int device_num = 0; if(sscanf(digits, "%d", &device_num) == EOF) { …
ant2009
  • 27,094
  • 154
  • 411
  • 609
68
votes
11 answers

Why do I have to specify data type each time in C to printf() and scanf()?

As you can see from the code snippet below, I have declared one char variable and one int variable. When the code gets compiled, it must identify the data types of variables str and i. Why do I need to tell again during scanning my variable that…
Akki619
  • 2,386
  • 6
  • 26
  • 56
65
votes
7 answers

Reading in double values with scanf in c

I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code: double a,b; printf("--------\n"); //seperate lines scanf("%ld",&a); printf("--------\n");…
user1880009
  • 663
  • 1
  • 5
  • 5
61
votes
7 answers

Format specifiers for uint8_t, uint16_t, ...?

If I have an integer variable I can use sscanf as shown below by using the format specifier %d. sscanf (line, "Value of integer: %d\n", &my_integer); Where can I find format specifiers for uint8_t, uint16_t, uint32_t and uint64_t? uint64_t has…
Uthman
  • 9,251
  • 18
  • 74
  • 104
57
votes
4 answers

Read no more than size of string with scanf()

Edit: for my class I have to use scanf. So recommending other ways of input is not the solution I am looking for (if there is one that involves scanf). If I am reading in user input for a small project (for example, a game). Lets say I ask would…
MrHappyAsthma
  • 6,332
  • 9
  • 48
  • 78
55
votes
16 answers

Why is scanf() causing infinite loop in this code?

I've a small C-program which just reads numbers from stdin, one at each loop cycle. If the user inputs some NaN, an error should be printed to the console and the input prompt should return again. On input of "0", the loop should end and the number…
user208785
50
votes
1 answer

Does sscanf("123456789123456789123456789", "%d", &n) have defined behavior?

When sscanf() or another function from the scanf family is given a sequence of digits whose converted value exceeds the maximum value of the target integer type, should the conversion be considered to have failed? is the behavior defined at…
chqrlie
  • 131,814
  • 10
  • 121
  • 189
49
votes
5 answers

What are scanf("%*s") and scanf("%*d") format identifiers?

What is the practical use of the formats "%*" in scanf(). If this format exists, there has to be some purpose behind it. The following program gives weird output. #include int main() { int i; char str[1024]; …
manav m-n
  • 11,136
  • 23
  • 74
  • 97
48
votes
7 answers

C - scanf() vs gets() vs fgets()

I've been doing a fairly easy program of converting a string of Characters (assuming numbers are entered) to an Integer. After I was done, I noticed some very peculiar "bugs" that I can't answer, mostly because of my limited knowledge of how the…
Marko
  • 491
  • 1
  • 5
  • 6
43
votes
5 answers

Reading a string with spaces with sscanf

For a project I'm trying to read an int and a string from a string. The only problem is sscanf() appears to break reading an %s when it sees a space. Is there anyway to get around this limitation? Here's an example of what I'm trying to do: #include…
SDLFunTimes
  • 745
  • 2
  • 7
  • 13
43
votes
7 answers

printf not printing on console

I’m getting started in the C language. I am using eclipse (juno) as my IDE and installed CDT plugin. I have also unpacked mingw64 (GCC Compiler). I wrote a very simple program to see if it works. This is my code: #include int main() { …
Mr T.
  • 4,278
  • 9
  • 44
  • 61
41
votes
6 answers

string format for intptr_t and uintptr_t

What is the string format for intptr_t and uintptr_t which is valid for both the 32 and 64 bit architecture . EDIT warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has type "AAA" This is the warning i am getting in 64 bit but not…
thetna
  • 6,903
  • 26
  • 79
  • 113
41
votes
6 answers

Difference between scanf() and fgets()

I want to know what is the difference between fgets() and scanf(). I am using C as my platform.
Biswajyoti Das
  • 7,881
  • 11
  • 34
  • 26
40
votes
9 answers

what is the Java equivalent of sscanf for parsing values from a string using a known pattern?

So I come from a C background (originally originally, though I haven't used that language for almost 5 years) and I'm trying to parse some values from a string in Java. In C I would use sscanf. In Java people have told me "use Scanner, or…
Adam Burley
  • 5,551
  • 4
  • 51
  • 72
40
votes
6 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
  • 415
  • 1
  • 4
  • 7