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

Parsing input with scanf in C

I've been having a lot of problems trying to figure out how to use scanf(). It seems to work fine with integers, being fairly straight forward scanf("%d", &i). Where I am running into issues is using scanf() in loops trying to read input. For…
zxcv
  • 7,391
  • 8
  • 34
  • 30
10
votes
2 answers

Why doesn't scanf need an ampersand for strings and also works fine in printf (in C)?

I am learning about strings in C now. How come to use scanf to get a string you can do scanf("%s",str1); and for printf you can do printf("The string is %s\n", str1); I understand that for scanf it is because the string is just a character array…
Adam
  • 2,606
  • 4
  • 19
  • 14
10
votes
3 answers

Difference between scanf("%c", &c) and scanf(" %c", &c)

Consider the following C code snippet: #include int main() { int a; char c; scanf("%d",&a); scanf("%c",&c); printf("int=%d\n",a); printf("char=%c\n",c); } I'm able to input only the integer and not the…
passmaster10
  • 127
  • 1
  • 2
  • 7
10
votes
4 answers

sscanf until it reaches a comma

I'm trying to scanf words and numbers from a string looks like: "hello, world, I, 287876, 6.0" <-- this string is stored in a char array (string) What I need to do is to split things up and assign them to different variables so it would be like …
Faisal Al-shawi
  • 111
  • 1
  • 1
  • 4
10
votes
3 answers

How to parse numbers like "3.14" with scanf when locale expects "3,14"

Let's say I have to read a file, containing a bunch of floating-point numbers. The numbers can be like 1e+10, 5, -0.15 etc., i.e., any generic floating-point number, using decimal points (this is fixed!). However, my code is a plugin for another…
anrieff
  • 619
  • 1
  • 6
  • 15
10
votes
4 answers

How does scanf() work inside the OS?

I've been wondering how scanf()/printf() actually works in the hardware and OS levels. Where does the data flow and what exactly is the OS doing around these times? What calls does the OS make? And so on...
jetru
  • 1,964
  • 4
  • 16
  • 24
10
votes
4 answers

%*c in scanf() - what does it mean?

I tried to run this program in Turbo C but couldn't decipher the output. What does this %*c mean? Any help would be appreciated. int dd,mm,yy; printf("\n\tEnter day,month and year"); scanf("%d %*c %d %*c %d",&dd,&mm,&yy); // what does %*c mean…
Piyp791
  • 649
  • 1
  • 13
  • 28
10
votes
3 answers

sscanf & newlines

I need to parse a response from a server like this: risposta: 200\n Len 1040\n Expire 30\n \n 1111111111111111111111111\n 1111111111111111111111111\n 1111111111111111111111111\n I'm trying with sscanf: sscanf(risposta, "%d\nLen %d\nExpire…
Gorgo
  • 456
  • 1
  • 7
  • 19
9
votes
5 answers

How to use sscanf correctly and safely

First of all, other questions about usage of sscanf do not answer my question because the common answer is to not use sscanf at all and use fgets or getch instead, which is impossible in my case. The problem is my C professor wants me to use scanf…
evodevo
  • 479
  • 1
  • 7
  • 14
9
votes
2 answers

how use EOF stdin in C

I need to input coordinates into an array until EOF is encountered, but something is wrong in my code. I used ctrl+Z, ctrl+D int main() { int x[1000],y[1000]; int n=0,nr=0,a,b,i; printf("Enter the coordinates:\n"); while(scanf ( "%d…
Trung Nguyen
  • 91
  • 1
  • 1
  • 2
9
votes
4 answers

What is the purpose of using the [^ notation in scanf?

I have run into some code and was wondering what the original developer was up to. Below is a simplified program using this pattern: #include int main() { char title[80] = "mytitle"; char title2[80] =…
ojblass
  • 21,146
  • 22
  • 83
  • 132
9
votes
3 answers

How to prevent fscanf from interpreting 0x2 as hex?

I have a file which stores values like 2.32x7. I read the floating-point part using: fscanf(file, "%lf", &value); It works perfectly... except for when the file stores something like 0x2. In that case, it reads the entire string as a hexadecimal…
Off Kilter
  • 201
  • 1
  • 4
9
votes
3 answers

Why can a null character be embedded in a conversion specifier for scanf?

Perhaps I'm misinterpreting my results, but: #include int main(void) { char buf[32] = ""; int x; x = scanf("%31[^\0]", buf); printf("x = %d, buf=%s", x, buf); } $ printf 'foo\n\0bar' | ./a.out x = 1, buf=foo Since the…
William Pursell
  • 204,365
  • 48
  • 270
  • 300
9
votes
3 answers

scanf() curious behaviour!

I recently stumbled upon a curious case(atleast for me, since I hadn't encountered this before)..Consider the simple code below:- int x; scanf("%d",&x); printf("%d",x); The above code takes a normal integer input and displays the result as…
sachin11
  • 1,087
  • 3
  • 13
  • 17
9
votes
6 answers

NULL arg allowed to sscanf?

Is a NULL pointer allowed as the string to store result in in a call to sscanf? I don't find anything about it in any documentation but it seems to be working fine. Same thing with scanf. Example: int main(int arc, char* argv[]) { char* s = NULL; …
Lii
  • 11,553
  • 8
  • 64
  • 88