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
24
votes
2 answers

error: unknown conversion type character 'l' in format - scanning long long

I'm trying to get long long from the console using standard IO function scanf. I started with %lld: scanf("%lld", &rule); That throws: error: unknown conversion type character 'l' in format [-Werror=format=] I've found more workarounds, but they…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
24
votes
3 answers

Compiler message "warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’"

I am trying to run a simple C program, but I am getting this error: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’ I am running Mac OS X v10.8 (Mountain Lion) and am compiling in the terminal using GCC…
l00kitsjake
  • 955
  • 3
  • 11
  • 24
24
votes
2 answers

Using scanf for reading an unsigned char

I'm trying to use this code to read values between 0 to 255 (unsigned char). #include int main(void) { unsigned char value; /* To read the numbers between 0 to 255 */ printf("Please enter a number between 0 and 255 \n"); …
user1293997
  • 855
  • 5
  • 12
  • 20
23
votes
8 answers

Reading a line using scanf() not good?

scanf(" %[^\n]",line); A friend of mine suggested that using fgets() to read a line as input would be a much better idea than using scanf() as in the statement above. Is he justified?
amulous
  • 704
  • 2
  • 6
  • 15
23
votes
7 answers

Why does scanf require &?

I want to read a number from stdin. I don't understand why scanf requires the use of & before the name of my variable: int i; scanf("%d", &i); Why does scanf need the address of the variable?
Nick
  • 10,309
  • 21
  • 97
  • 201
22
votes
7 answers

fgets doesn't work after scanf

#include #include #include void delspace(char *str); int main() { int i, loops; char s1[101], s2[101]; scanf("%d", &loops); while (loops--) { fgets(s1, 101, stdin); fgets(s2, 101,…
Vayn
  • 2,507
  • 4
  • 27
  • 33
22
votes
3 answers

read comma-separated input with `scanf()`

I have the following input: AG23,VU,Blablublablu,8 IE22,VU,FooBlaFooBlaFoo,3 and so on... I want it to "parse" with scanf() using some code like this: char sem[5]; char type[5]; char title[80]; int value; while(scanf("%s,%s,%s,%d", sem, type,…
Moonlit
  • 5,171
  • 14
  • 57
  • 95
21
votes
6 answers

Is sscanf considered safe to use?

I have vague memories of suggestions that sscanf was bad. I know it won't overflow buffers if I use the field width specifier, so is my memory just playing tricks with me?
nmichaels
  • 49,466
  • 12
  • 107
  • 135
21
votes
6 answers

What does `scanf("%*[^\n]%*c")` mean?

I want to make a loop in C that, when the program asks for an integer and the user types a non-digit character, the program asks again for an integer. I just found the below code. but I don't understand what this means scanf("%*[^\n]%*c"). What does…
21
votes
3 answers

Specifying the maximum string length to scanf dynamically in C (like "%*s" in printf)

I can specify the maximum amount of characters for scanf to read to a buffer using this technique: char buffer[64]; /* Read one line of text to buffer. */ scanf("%63[^\n]", buffer); But what if we do not know the buffer length when we write the…
wefwefa3
  • 3,872
  • 2
  • 29
  • 51
20
votes
6 answers

What is the simplest way of getting user input in C?

There seem to be a LOT of ways you can get user input in C. What is the easiest way that requires little code? Basically I need to display this: Enter a file name: apple.text Basically I need to ask the user for a file name. So I need something…
antonpug
  • 13,724
  • 28
  • 88
  • 129
19
votes
4 answers

Reading file using fscanf() in C

I need to read and print data from a file. I wrote the program like below, #include #include int main(void) { char item[9], status; FILE *fp; if( (fp = fopen("D:\\Sample\\database.txt", "r+")) == NULL) { printf("No such…
Guru
  • 207
  • 2
  • 4
  • 8
19
votes
3 answers

When should I use ampersand with scanf()

What are the rules for using ampersand in c while using scanf()? struct Student { char name[20]; int id; }; int main(void) { struct Student std1; printf("enter name and id of std1\n"); scanf("%s %d", std1.name, &(std1.id)); } Why for…
Dkova
  • 1,087
  • 4
  • 16
  • 28
18
votes
4 answers

Difference between fgets and fscanf?

I have a question concerning fgets and fscanf in C. What exactly is the difference between these two? For example: char str[10]; while(fgets(str,10,ptr)) { counter++; ... and the second example: char…
Chris
  • 6,093
  • 11
  • 42
  • 55
18
votes
5 answers

Reading long int using scanf

To read an int using scanf we use: scanf("%d", &i); What if i is a long not int?? Note: when using %d with long it gives me an irritating warning..
Betamoo
  • 14,964
  • 25
  • 75
  • 109