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

How to Take whitespace in Input in C

I wanted to take character array from console and it also include white spaces, the only method i know in C is scanf, but it miss stop taking input once it hit with white space. What i should do? Here is what i am doing. char…
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
8
votes
3 answers

Use scanf with Regular Expressions

I've been trying to use regular expressions on scanf, in order to read a string of maximum n characters and discard anything else until the New Line Character. Any spaces should be treated as regular characters, thus included in the string to be…
someone
  • 361
  • 2
  • 3
  • 13
8
votes
2 answers

what is the %2d in scanf

I know the meaning of this statement scanf("%d",&x); But what does this statement do scanf("%2d",&x); I tried searching for this, but could not find an answer. I want to know what happens internally also.
Ashwin
  • 12,691
  • 31
  • 118
  • 190
8
votes
4 answers

Fscanf or Fgets? Reading a file line after line

i have to write a program in C to read a file containing several line of text, each line contains two variables: a number (%f) and a string: EX: file.txt ============ 24.0 Torino 26.0 Milano 27.2 Milano 26.0 Torino 28.0 Torino 29.4 Milano There is…
Lc0rE
  • 2,236
  • 8
  • 26
  • 34
8
votes
2 answers

warnings when activating the optimization options

I use scanf in a c program to read an int from STDIN: scanf("%d", &n); when I compile the c program with optimization enabled I get some warnings: gcc main.c -lm -lpthread -O2 -o main main.c: In function ‘main’: main.c:45: warning: ignoring return…
ob_dev
  • 2,808
  • 1
  • 20
  • 26
7
votes
6 answers

Programs with the scanf not working properly in NetBeans

I've installed NetBeans 7.0.1 today When I try to execute C program with "scanf" in it it's giving strange errors This is what I wrote: It keeps on running until I enter something in the output console. After entering its shows the printf…
Naresh
  • 657
  • 3
  • 16
  • 35
7
votes
4 answers

Faster than scanf?

I was doing massive parsing of positive integers using scanf("%d", &someint). As I wanted to see if scanf was a bottleneck, I implemented a naive integer parsing function using fread, just like: int result; char c; while (fread(&c, sizeof c, 1,…
Jo So
  • 25,005
  • 6
  • 42
  • 59
7
votes
1 answer

Consistency and behavior of %n operator in *scanf

I'm currently building a bit of HTTP handling into a C program (compiled using glibc on Linux), which will sit behind an nginx instance, and figured I should be safe deferring argument tokenization to sscanf in this scenario. I was very pleased to…
i336_
  • 1,813
  • 1
  • 20
  • 41
7
votes
2 answers

Reading and writing binary files in C

These are 2 separate applications. In the first one, I tried to store employee details like name, age and salary in the binary file named emp.bin. In the second application, I tried to view the contents of the file but in place of the name, only…
0x5961736972
  • 148
  • 1
  • 7
7
votes
5 answers

Escaping square bracket ] in sscanf

I want to scan lines like "[25, 28] => 34" I wrote a small program to test it out: #include #include int main() { char* line = "[25, 28] => 34"; char a1[100], a2[100]; int i; sscanf(line,…
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
7
votes
4 answers

Using scanf and fgets in the same program?

I need to do something like the following: int main(void) { char a,b,cstring; printf("please enter something"); scanf("%c %c",&a,&b); prinf("thanks, now some more"); fgets(cstring,35,stdin); } Problem is that whenever I enter the first vars from…
wesbos
  • 25,839
  • 30
  • 106
  • 143
7
votes
5 answers

How to input a string with unknown size

I'm a bit confused about strings in C. I understand that declaring buffer size is important since otherwise, it can cause buffer overflow. But I need to know how do I take a string input that I don't know the size of. For instance, if I wanted to…
Chase
  • 5,315
  • 2
  • 15
  • 41
7
votes
7 answers

Value of boolean type changed in C

I noticed that in C, my boolean variable somehow gets changed in a way I don't understand. #include #include int main(void) { bool x, y; printf("x: "); scanf("%d", &x); printf("x is %d\n", x); printf("y:…
7
votes
1 answer

Understanding Scanf in Go

I am not sure how to use Scanf function. Let's say I want to input number 10 on scan. By doing that, shouldn't the output be 0xA? Also, how do I use the function with two or more scan arguments (e.g. fmt.Scanf("%x", &e, &f, &g))? package…
dodo254
  • 519
  • 2
  • 7
  • 16
7
votes
4 answers

User typing null terminator in scanf

Can a user type null terminator in an input, for which scanf is used, so the length of the input would be 0? char msg[1000]; scanf("%1000s",msg); // the user would type nothing: '' or '\0'
user5654873