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

gcc/ld - create a new libc.so with __isoc99_sscanf@@GLIBC_2.7 symbol from glibc.2.6

I have an application, which does a error when I try to run it: /lib/libc.so.6: version `GLIBC_2.7' not found But the only symbol it needs from glibc 2.7 is __isoc99_sscanf@@GLIBC_2.7 I want to write a small single function "library" with this…
osgx
  • 90,338
  • 53
  • 357
  • 513
6
votes
3 answers

C produce error if no argument is given in command line

In C, how can I produce an error if no arguments are given on the command line? I'm not using int main(int argc , * char[] argv). my main has no input so I'm getting my variable using scanf("%d", input)
Nicky Mirfallah
  • 1,004
  • 4
  • 16
  • 38
6
votes
3 answers

Why won't this scanf format-string work? "%[^\n]\n"

I've seen a few examples where people give scanf a "%[^\n]\n" format string to read a whole line of user input. If my understanding is correct, this will read every character until a newline character is reached, and then the newline is consumed by…
ivan
  • 6,032
  • 9
  • 42
  • 65
6
votes
2 answers

sscanf get the value of the remaining string

I have a String which looks like this: "HELLO 200 Now some random text\n now more text\t\t" I try to get the HELLO, the 200, and the remaining string. Unfortunately the string may contain \n's and \t's so i cannot use %[^\n\t]. I tried the…
plainerman
  • 435
  • 8
  • 18
6
votes
2 answers

How to use fgets properly in a structure?

I can't work out what's the problem with my code. Here's my code: #include #include #define N 20 typedef struct _dog { char dogName[N],ownerName[N]; int dogAge; } Dog; int main() { //Dynamic array int…
2b1c
  • 75
  • 8
6
votes
2 answers

Why is there no dscanf()?

Why is there no dscanf function for reading from file descriptors? We have fprintf, sprintf and dprintf for printing but for scanning there is only fscanf and sscanf.
6
votes
2 answers

scanf in c and relation input buffer

I try the understand the relation between scanf and the input buffer. I use scanf with the following format string: int z1,z2; scanf("%d %d", &z1,&z2); And try to understand why I can enter as many as possible whitespace (Enter, Blanks, Tabs) after…
knowledge
  • 941
  • 1
  • 11
  • 26
6
votes
5 answers

scanf / field lengths : using a variable / macro, C/C++

How can I use a variable to specify the field length when using scanf. For example: char word[20+1]; scanf(file, "%20s", word); Also, is it correct to use 20+1 (since it needs to add a \0 at the end?). Instead, I'd like to have something…
user51231
  • 63
  • 1
  • 3
6
votes
9 answers

scanf() (C Language ) confused me

When do I need to insert/don't insert & for scanf() in C? Thank you. int main() { char s1[81], s2[81], s3[81]; scanf("%s%s%s", s1, s2, s3); // If replace scanf() with the expression below, it works too. // scanf("%s%s%s", &s1, &s2,…
Nano HE
  • 9,109
  • 31
  • 97
  • 137
6
votes
1 answer

What is really happening on removing and adding white space characters?

I'm new in programming and learning basics of C programming. I'm learning about how scanf() works but I think now I'm very much confused and really don't know how and what to ask. But I will try my best to put my question clear. Questions I'm…
Yogesh Tripathi
  • 703
  • 5
  • 16
6
votes
3 answers

How does scanf determine whether to block?

When I redirect a file to stdin using MyProgram < cl.txt command from the command line, scanfs doesn't wait me to press Enter. But when I use scanf in my program without doing so, it does block until enter key is pressed. How exactly does it…
Millo Varantz
  • 173
  • 2
  • 8
6
votes
2 answers

What does a # sign after a % sign in a scanf() function mean?

What does the following code mean,in C scanf("%d%#d%d",&a,&b,&c); if given values 1 2 3 it gives output as 1 0 0 P.S- I know it is used with printf() statement but here in scanf() statement it gives random behaviour.
FossArduino
  • 161
  • 2
  • 2
  • 11
6
votes
5 answers

if one complains about gets(), why not do the same with scanf("%s",...)?

From man gets: Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is …
dbarbosa
  • 2,969
  • 5
  • 25
  • 29
6
votes
2 answers

C scanf behaving unexpectedly with %i to scan dates

For some reason, when I run my program it is not giving me the option to type numbers for the statements associated with day1 and year2. Why is it doing this? #include int main(void ) { int day1, day2, month1, month2, year1, year2; …
Frank Tocci
  • 616
  • 2
  • 8
  • 20
6
votes
2 answers

fscanf() reading string with spaces in formatted lines

Using this structure: typedef struct sProduct{ int code; char description[40]; int price; }; I want to read a txt file with this format: 1,Vino Malbec,12 where the format is: code,description,price. But I'm having problems to read the…
Mati Tucci
  • 2,826
  • 5
  • 28
  • 40