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

C: why no & for strings in scanf() function?

Possible Duplicate: Why doesn't scanf need an ampersand for strings and also works fine in printf (in C)? In C when we use the scanf() function to get user input, we always use the & sign before the variable. For example: scanf("%d",…
tarashish
  • 1,945
  • 21
  • 32
6
votes
6 answers

Why does sscanf not work properly with a bool type

The output of this code: const char *buff = "*_2D 1"; char field[10]; int flag; sscanf(buff, "%s %d", field, &flag); printf("field:%s flag:%i\n", field, flag); is field:*_2D flag:1 However by changing the int to bool results in strange…
Salgar
  • 7,687
  • 1
  • 25
  • 39
6
votes
3 answers

scanf %d segfault at large input

So I ran some static code analyzer over some c code and one thing that surprised me was a warning about: int val; scanf("%d", &val); which said that for large enough input this may result in a segfault. And surely enough this can actually happen.…
Voo
  • 29,040
  • 11
  • 82
  • 156
6
votes
1 answer

Is scanf("%4s%4s", b, b); well-defined?

#include int main(void) { char b[5]; scanf("%4s%4s", b, b); //My input: "qwersgsh" printf("%s", b); //Output: sgsh } C99: Between the previous and next sequence point an object shall have its stored…
user13469230
6
votes
3 answers

Parsing: library functions, FSM, explode() or lex/yacc?

When I have to parse text (e.g. config files or other rather simple/descriptive languages), there are several solutions that come to my mind: using library functions, e.g. strtok(), sscanf() a finite state machine which processes one char at a…
Philip
  • 5,795
  • 3
  • 33
  • 68
6
votes
2 answers

Scanning Multiple inputs from one line using scanf

I'm trying to scan in a single line input and storing it in a struct. I'm not sure if I am storing it wrong or I am printing it wrong. I'm not sure on how to use scanf with for loops since I never done it before not to mention C likes to overwrite…
Thao Nguyen
  • 901
  • 7
  • 22
  • 42
6
votes
1 answer

Is there difference between scanf("%c",&x) and x=getchar()?

I'm writing to understand as much as I can of the following program that I tried to compute, because I thought it had some possible application for everyday life. The program goes like this (it's a cypher): #include void cifrario(int k)…
jacopoburelli
  • 257
  • 2
  • 9
6
votes
4 answers

How do I use a #defined constant as the max field width in fscanf?

All of this is in C89, not C99. I have a constant. #define MAX_NAME_LEN 256 I want to use it as the max field width in fscanf, sort of like this. fscanf(input, "%256s", name); But I want to use the MAX_NAME_LEN instead of the literal 256 for sake…
SonicN
  • 163
  • 7
6
votes
2 answers

parse integer without appending char in C

I want to parse an integer but my following code also accepts Strings like "3b" which start as a number but have appended chars. How do I reject such Strings? #include #include int main(int argc, char *argv[]) { int n; …
Gastmon
  • 83
  • 6
6
votes
2 answers

%[^\n]s in scanf does not wait for input and gets skipped

In the loop in the code below, scanf("%[^\n]s",array) is not working. It does not wait for input and gets skipped. But a space before % fixes the issue. Why? Here is the wrong program: #include #include int main() { …
ammasum
  • 118
  • 1
  • 9
6
votes
2 answers

How fork() and scanf() work together?

I tried to see what happens if I read something from keyboard while I have multiple processes with fork() (in my case there are two children and a parent) and I discovered the following problem: I need to tell the parent to wait for children's…
IonutC
  • 75
  • 4
6
votes
4 answers

Use scanf to read lines or break on special character

Is it possible to read lines of text with scanf() - excluding \n and break on special(chosen) character, but include that character This is my current expression: while(scanf("%49[^:\n]%*c", x)==1) but this one excludes :. Is it possible to break…
Iluvatar
  • 642
  • 7
  • 19
6
votes
2 answers

matching trailing text in sscanf?

This is related to sscanf usage - how to verify a completed scan vs an aborted scan but it's an edge case not covered by that question. char entry[] = "V2X_3_accepted"; int d1,d2,ret1,ret2; ret1 = sscanf(entry,"V2X_%d_expected",&d1); ret2 =…
SF.
  • 13,549
  • 14
  • 71
  • 107
6
votes
3 answers

Reading in a string with spaces in C

I am trying to read in a string that may or may not include spaces ex. "hello world". By doing the following with a number select menu that is inputted by the user. This is just a small replica of what I am trying to do. #include #include…
TheBoxOkay
  • 81
  • 1
  • 8
6
votes
4 answers

Use of [] in fscanf()

I've a text file with following contents: "abc","def","ghi" The following works to read the file contents properly: int main() { char name[1024] = {0}; FILE *file = fopen("file.txt", "r"); while(1) { if (fscanf(file, "…
Donotalo
  • 12,748
  • 25
  • 83
  • 121