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
7
votes
5 answers

Get scanf to quit when it reads a newline?

If I input 5 5 at the terminal, press enter, and press enter again, I want to exit out of the loop. int readCoefficents(double complex *c){ int i = 0; double real; double img; while(scanf("%f %f", &real, &img) == 2) c[i++] =…
Matt
  • 73
  • 1
  • 1
  • 3
7
votes
1 answer

C - scanf unsigned char

I am trying to use scanf() for reading an unsigned char value with %hhu, but the compiler returned me: error: unknown conversion type character 'h' in format [-Werror=format]| // line 3 error: too many arguments for format …
user3813238
7
votes
2 answers

when scanf returns 0 in c and just doesn't work

'when there is no successful assignments' i know that scanf returns 0 to indicate it, but is that the only thing that it does? this is my code: #include int main(void) { int val,x; x=scanf("%d",&val); if(x==1) …
Nous Sa Smily
  • 73
  • 1
  • 7
7
votes
5 answers

scanf("%c") call seems to be skipped

I tried this code below, but it seems scanf("%c") is skipped. It only asks me to enter name and age and skips the lines below that. It just print the text in the printf above the if statements. Can anyone help? #include int main() { …
Dezhou Zhang
  • 169
  • 1
  • 2
  • 7
7
votes
2 answers

C: scanf for char not working as expected

I was recently running a c program in my PC. It have a for loop in which some char d is scanned. The for loop runs for 3 times. During each running it prints the the count of running and then scans the value of char d. The program is as…
Vivek Sasidharan
  • 1,265
  • 3
  • 17
  • 34
7
votes
5 answers

Convert string to GUID with sscanf

I'm trying to convert a string to GUID with sscanf: GUID guid; sscanf( "11111111-2222-3333-4455-667788995511", "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", &guid.Data1, &guid.Data2, &guid.Data3, &guid.Data4[0], &guid.Data4[1],…
Andy Li
  • 5,894
  • 6
  • 37
  • 47
7
votes
2 answers

Is getchar() equivalent to scanf("%c") and putchar() equivalent to printf("%c")?

Is a = getchar() equivalent to scanf("%c",&a);? Is putchar(a) equivalent to printf("%c",a); where a is a char variable?
Gautam Kumar
  • 1,162
  • 3
  • 14
  • 29
7
votes
4 answers

How to get multiple inputs in one line in C?

I know that I can use scanf("%d %d %d",&a,&b,&c): But what if the user first determines how many input there'd be in the line?
Soham
  • 203
  • 1
  • 2
  • 6
7
votes
7 answers

String input using C scanf_s

I've been trying to look for answer myself, but I can't find one. I want to insert a part of the programming that reads in a string like "Hello" and stores and can display it when I want, so that printf("%s", blah); produces Hello. Here's the code…
user3587529
  • 81
  • 1
  • 1
  • 3
7
votes
5 answers

Skipping expected characters like scanf() with cin

How to achieve scanf("%d # %d",&a,&b);sort of effect with cin in C++ ?
eldos
  • 3,132
  • 3
  • 29
  • 32
7
votes
3 answers

how to ignore whitespaces in fscanf()

I need to use fscanf to ignore all the white spaces and to not keep it. I tried to use something like the combination between (*) and [^\n] as: fscanf(file," %*[^\n]s",); Of course it crashed, is there any way to do it only with fscanf? code: int…
olsynt
  • 93
  • 1
  • 2
  • 9
7
votes
7 answers

How does the scanf function work in C?

Why do you require ampersand (&) in the scanf function. What will the output or type of error (compile or runtime) be in the following C code? #include void main() { int a; printf("enter integer:"); scanf("%d", a); }
Amol Aggarwal
  • 2,674
  • 3
  • 24
  • 32
7
votes
4 answers

Traverse FILE line by line using fscanf

Ok so i have a text file database.txt. Each line is a user with the format below "John Smith"| 4| 80.00| "123 Lollipop Lane"| "New Jersey"| "08080" When I try do the following: database = fopen(fileName,"r"); while(fscanf(database,"%[^\n]",…
Brandon Amir
  • 380
  • 1
  • 6
  • 16
7
votes
4 answers

Using the scanf function in while loop

I am attempting to format a space-delimited user input for a programming assignment. Essentially, the input consists of an arbitrary number of expressions L integer integer integer integer and C integer integer integer. For example: L 1 1 5 7 C 4 5…
Julian Laval
  • 1,210
  • 4
  • 17
  • 34
7
votes
3 answers

Ignoring/skipping tokens using std::cin

With scanf one is allowed to skip matched tokens, simply adding * to the pattern, as in: int first, second; scanf("%d %*s %d", &first, &second); Is there any equivalent approach with std::cin? Something like (of course, sparing the usage of…
Rubens
  • 14,478
  • 11
  • 63
  • 92