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

How to scanf a float followed immediately by the letter 'e' in c?

I'm trying to use fscanf to read in data, and part of the input is a float followed by the letter 'e', for example, 41.72elapsed. When writing the strng for fscanf, I attempted to use "%felapsed", but this doesn't work, as %fe is its own format…
user2093696
  • 131
  • 5
13
votes
1 answer

c - reading a specific column in data file

So I created a data file like so: for(size_t n = ...;...;...){ //do some stuff double mean_value = ... double min_value = ... double max_value = ... FILE *fp = fopen(OUTPUT_FILE,"a+"); fprintf(fp,"%d %lf %lf…
User1291
  • 7,664
  • 8
  • 51
  • 108
13
votes
3 answers

What is the need of hh and h format specifiers?

In the code below mac_str is char pointer and mac is a uint8_t array: sscanf(mac_str,"%x:%x:%x:%x:%x:%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]); When I try the above code it gives me a warning: warning: format ‘%x’ expects argument of…
Siva Kannan
  • 2,237
  • 4
  • 27
  • 39
13
votes
11 answers

How do I parse out the fields in a comma separated string using sscanf while supporting empty fields?

I have a comma separated string which might contain empty fields. For example: 1,2,,4 Using a basic sscanf(string,"%[^,],%[^,],%[^,],%[^,],%[^,]", &val1, &val2, &val3, &val4); I get all the values prior to the empty field, and unexpected results…
Belrog
  • 950
  • 1
  • 11
  • 19
12
votes
6 answers

What does space in scanf mean?

#include int main(int argc, char* argv[]) { char c; scanf(" %c", &c); printf("%c\n", c); return 0; } [root@test]# ./scanf a a [root@test]# ./scanf h h It seems always matching whether space exists,why?
Je Rog
  • 5,675
  • 8
  • 39
  • 47
12
votes
3 answers

C getchar vs scanf

I am confused by a piece of code found in a function I am studying: char GetCommand( void ) { char command; do { printf( "Enter command (q=quit, n=new, l=list): " ); scanf( "%c", &command ); Flush(); } while…
startuprob
  • 1,917
  • 5
  • 30
  • 45
12
votes
5 answers

Validate max integer in scanf

I want to read a int from stdin but I want to validate if the user exceeds the int max value. How can I do it? int n; scanf("%d", &n); scanf reads the decimal input and stores in the int, causing overflow. How can I check and avoid this?
António Silva
  • 121
  • 1
  • 1
  • 3
12
votes
2 answers

scanf regex - C

I needed to read a string until the following sequence is written: \nx\n : (.....)\n x\n \n is the new line character and (.....) can be any characters that may include other \n characters. scanf allows regular expressions as far as I know, but i…
pasadinhas
  • 353
  • 2
  • 6
  • 22
12
votes
5 answers

fscanf return value

What does fscanf return when it reads data in the file. For example, int number1, number2, number3, number4, c; c = fscanf (spFile, "%d", &number1); //c will be 1 in this case. c = fscanf (spFile, "%d %d %d %d", &number1, &number1, &number3,…
slow
  • 805
  • 3
  • 13
  • 27
11
votes
2 answers

scanf("%[^\n]s",a) vs gets(a)

I have been told that scanf should not be used when user inputs a string. Instead, go for gets() by most of the experts and also the users on StackOverflow. I never asked it on StackOverflow why one should not use scanf over gets for strings. This…
niko
  • 9,285
  • 27
  • 84
  • 131
11
votes
2 answers

Can `std::istream::operator>>()` accept integer radix prefixes like stdio's %i format specifier?

When using scanf() and its variants, the format specifier %i will accept data as hex (prefixed "0x"), octal (prefixed "0"), or decimal (no prefix), so for example the strings "0x10", "020", and "16" are all converted to an integer with decimal value…
Clifford
  • 88,407
  • 13
  • 85
  • 165
11
votes
3 answers

How to portably convert a string into an uncommon integer type?

Some background: If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t, I’d use SCNu16 from , like this: #include #include uint16_t x; char *xs =…
Teddy
  • 6,013
  • 3
  • 26
  • 38
11
votes
1 answer

What happens if I forget to close a scanset?

Suppose I forgot to close the right square bracket ] of a scanset. What will happen then? Does it invoke Undefined Behavior? Example: char str[] = "Hello! One Two Three"; char s1[50] = {0}, s2[50] = {0}; sscanf(str, "%s %[^h", s1, s2); /* UB?…
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
11
votes
3 answers

How can printf issue a compiler warning?

I was wondering how can a function issue a compile-time warning? This came to my mind because when we supply wrong format specifier in the first argument of printf (scanf) for the variable matched with that type specifier and compile with gcc with…
user1635881
  • 239
  • 3
  • 11
11
votes
8 answers

Using scanf with NSStrings

I want the user to input a string and then assign the input to an NSString. Right now my code looks like this: NSString *word; scanf("%s", &word);
Omar
  • 2,155
  • 4
  • 24
  • 38