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
9
votes
2 answers

Why is scanf not allowing any more input?

#include #include int main() { char a[100], b[100]; char *ret; scanf("%[^\n]s", a); scanf("%[^\n]s", b); ret = strstr(a, b); if (ret != NULL) printf("its a substring"); else …
Karan Singh
  • 1,114
  • 1
  • 13
  • 30
9
votes
1 answer

difference between %ms and %s scanf

Reading the scanf manual I encounter this line: An optional 'm' character. This is used with string conversions (%s, %c, %[), Can someone explain it with simple example stating the difference and the need of such an option in some cases?
rondino
  • 395
  • 2
  • 3
  • 10
9
votes
5 answers

How do I read white space using scanf in c?

Problem: I need to be able identify when two whitespaces occur consecutively. I have read the following questions: how to read a string from a \n delimited file how to read scanf with spaces And I am aware of scanf problems:…
MykC
  • 188
  • 1
  • 3
  • 13
9
votes
1 answer

How to sscanf to make sure the buffer is exactly what I wanted?

I would like to sscanf a line and make sure that that there is nothing more then what I wanted in it. The code looks like this: void parse_init_command(char *buffer, command *new_command) { if (sscanf(buffer, "%s %d %d %d %d %d %d…
madasionka
  • 812
  • 2
  • 10
  • 29
9
votes
3 answers

Read a string as an input using scanf

I am new to C language and I am trying read a character and a string (a sentence; max-length 25) from a user. Not sure what I am doing wrong in the following lines of code, its giving me an error "Segment Fault". #include int main(){ …
Hafiz Temuri
  • 3,882
  • 6
  • 41
  • 66
9
votes
4 answers

How to only accept a certain precision (so many decimals places) in scanf?

In scanf() function I want to take only upto decimal values.Can we achieve it? For example for displaying upto two decimal places we use printf("%.2f",A) How can we do it for scanf() function.
mani teja varma
  • 183
  • 1
  • 3
  • 12
9
votes
5 answers

C, reading multiple numbers from single input line (scanf?)

I have written an app in C which expects two lines at input. First input tells how big an array of int will be and the second input contains values separated by space. For example, the following input 5 1 2 3 4 99 should create an array containing…
migajek
  • 8,524
  • 15
  • 77
  • 116
9
votes
4 answers

Dynamic String Input - using scanf("%as")

I am trying to read input using scanf and storing into char * dynamically as specified by GCC manual, But it is giving a compile time error. char *string; if (scanf ("%as",&string) != 1){ //some code } else{ printf("%s\n", *string); …
N 1.1
  • 12,418
  • 6
  • 43
  • 61
9
votes
1 answer

Conversion specifier %ju

In the following page, I found the code like CERT INT15-C Use intmax_t or uintmax_t for formatted IO on programmer-defined integer types uintmax_t temp; if(scanf("%ju", &temp) != 1) { ... I am not familiar with the "%ju" specifier. And I am not…
sevenOfNine
  • 1,509
  • 16
  • 37
9
votes
3 answers

sscanf behaviour / return value

I'm a novice learning C and trying to understand the following code from an online lecture. It scans a string for an integer; if characters are encountered, the sscanf fails. int n; char c; if (sscanf(string, " %d %c", &n, &c) == 1) //return the…
cortexlock
  • 1,446
  • 2
  • 13
  • 26
9
votes
2 answers

Strange character after an array of characters

I am a real beginner to C, but I am learning! I've stumbled upon this problem before and decided to ask what the reason for it is. And please explain your answers so I can learn. I have made a program which allows you to input 5 characters and then…
user2166357
  • 127
  • 1
  • 1
  • 7
9
votes
1 answer

scanf Getting Skipped

I am trying to make a simple C program for a class and one of the requirements is that I'm required to use scanf/printf for all input and output. My question is why is it that my scanf after the for loop in the main gets skipped and the program just…
Austin Davis
  • 3,516
  • 10
  • 27
  • 47
9
votes
1 answer

don't care in scanf

Imagine the following: you read in a string with scanf() but you only need a few of the datapoints in the string. Is there an easy way to throw away the extraneous information, without losing the ability to check if the appropriate data is there so…
NomeN
  • 17,140
  • 7
  • 32
  • 33
8
votes
4 answers

When to use printf/scanf vs cout/cin?

I'm testing some snippets I found off the web using g++ from MinGW. This is the C++ compiler...why then does it correctly compile C....why do people intertwine C and C++. The concrete question is: Is it O.K. to use both C and C++ and compile…
user656925
8
votes
3 answers

scanf Cppcheck warning

Cppcheck shows the following warning for scanf: Message: scanf without field width limits can crash with huge input data. To fix this error message add a field width specifier: %s => %20s %i => %3i Sample program that can crash: #include…
Alex F
  • 42,307
  • 41
  • 144
  • 212