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
4 answers

How to scanf full sentence in C

How to scanf() full sentence in C without using fgets() or gets() I want to scan a command line from the user and based on the first word recognize the command. For example: mkdir so I need to recognize that the user wants to create a new dir…
Dkova
  • 1,087
  • 4
  • 16
  • 28
7
votes
4 answers

reading lines using fscanf

hi i have a file which contains following lines: wwa weweweof ewewe wdw: 1 11 ms <1 ms <1 ms 174.78.134.1 2 11 ms <1 ms <1 ms 174.78.134.1 3 5 ms <1 ms <1 ms x58trxd00.abcd.edu.com [143.71.290.42]…
KAKAK
  • 879
  • 7
  • 16
  • 32
7
votes
5 answers

Ignoring separating character using scanf

The problem: I am attempting to use scanf to read a sentence with fields seperate by | ,so naturally i use the scanf's natural features to ignore this symbol but it then also ignores everything that has a | in it. The code, simplified: int main(){ …
Thongurf
  • 105
  • 1
  • 1
  • 6
7
votes
2 answers

How to use a scanf width specifier of 0?

How to use a scanf width specifier of 0? 1) unrestricted width (as seen with cywin gcc version 4.5.3) 2) UB 3) something else? My application (not shown) dynamically forms the width specifier as part of a larger format string for scanf(). Rarely…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
7
votes
2 answers

do ... while loop isn't working in main

I wrote a simple program with a function that calculates the area of a circle. The program also asks to the user if he wants to calculate it again and if the input is 'N', the program is supposed to stop. Here's the narrowed down test…
Alberto Rossi
  • 1,800
  • 4
  • 36
  • 58
7
votes
2 answers

Scanf unsigned char in hex

I am trying to change data in array, this is part of my code: u_char paket[100]; //here i put some data into array and then trying to change it by user scanf("%hhx.%hhx.%hhx.%hhx.%hhx.%hhx", &paket[0], &paket[1], &paket[2], &paket[3], &paket[4],…
user2306381
  • 71
  • 1
  • 3
7
votes
3 answers

Why 2nd scanf doesn't work in my program?

scanf("%d %c",&size,&chara); works but separate scanf for character input does not work. I show these inside the code. Why is that? void squareCustomFill(int size, char chara); int main(void) { int size,i,k; char chara; printf("Enter size of…
Lyrk
  • 1,936
  • 4
  • 26
  • 48
7
votes
2 answers

Inputting Non ASCII characters to scanf("%s")

Is there a way one can issue non ascii hex characters to a scanf that uses %s ? I'm trying to insert hexadecimal chars like \x08\xDE\xAD and so on (to demonstrate buffer overflow). The input is not to a command line parameter, but to a scanf inside…
asudhak
  • 2,929
  • 4
  • 22
  • 27
7
votes
4 answers

How to get newline character from scanf even if it's the only input

I'm doing homework that asks me to read an integer n representing the size of a loop and then read a line of characters n times and print it right after the user's input. So I used scanf and then I print it with printf. The problem is that if the…
user2018675
  • 657
  • 2
  • 5
  • 15
7
votes
4 answers

Clearest way to read and print .txt file lines in C

There are a bunch of ways describing how to use various methods to print out lines of a text file on this site: Posix-style, reading IP addresses, Fixed line length. They all seem to be tailored to a specific example. It would be great to have the…
Dlinet
  • 1,193
  • 3
  • 15
  • 22
7
votes
1 answer

Sscanf delimiters for parsing?

I am trying to parse the following string with sscanf: query=testword&diskimg=simple.img How can I use sscanf to parse out "testword" and "simple.img"? The delimiter arguments for sscanf really confuse me :/ Thank you!
Jason Block
  • 155
  • 1
  • 3
  • 9
7
votes
4 answers

How can I scan strings with spaces in them using scanf()?

I want to write a sub-program in which a user can input their comment. I use scanf("%s", X) and let them input a comment, but it can only store the word before a space bar in the string. How can I solve this problem in order to store a whole…
Wai Hung Tong
  • 71
  • 1
  • 1
  • 2
7
votes
5 answers

Reading multiple lines of input with scanf()

Relevant code snippet: char input [1024]; printf("Enter text. Press enter on blank line to exit.\n"); scanf("%[^\n]", input); That will read the whole line up until the user hits [enter], preventing the user from entering a second line (if they…
user688604
  • 71
  • 1
  • 1
  • 2
7
votes
1 answer

When does scanf start and stop scanning?

It seems scanf begins scanning the input when the Enter key is pressed, and I want to verify this with the code below (I eliminated error checking and handling for simplicity). #include int main(int argc, char **argv) { /* disable…
Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83
6
votes
3 answers

scanf not working. need to read double from console

I'm not sure what I'm doing wrong, but I'm not able to read a double from the console. Reading an it works fine for some reason. I'm using Xcode. double n1; // get input from the user printf("Enter first number: "); scanf("%f", &n1); printf("%f",…
user445338