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

fscanf input with floats

I'm reading from a text file which contains: Mary 55334422 24.90 56.6 45.68 and am reading it in: ....char name[20]; int num; double worked; double rate; double total;.... fscanf(fp, "%s %d %f %f %f\n", name, &num, &worked, &rate, &total); I'm…
rach
  • 701
  • 6
  • 16
  • 20
8
votes
1 answer

What is the range of the "number of characters read" in fscanf?

fscanf() specifies the "%n" directive as a means to write "the number of characters read from the input stream so far by this call to the fscanf function" C11dr §7.21.6.2 12. Let us call this number: ncount. The "%n" directive may be preceded by…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
8
votes
2 answers

Are scanf arguments allowed to alias?

I'm using scanf to extract very regular tabular data. I need to go over it in two passes and can safely ignore some of the arguments some of the time. I want to allocate space for only the strings I care about and a single "discard" string for the…
porglezomp
  • 1,333
  • 12
  • 24
8
votes
1 answer

scanf a string in a char pointer

I want to scan a string and point a char pointer to this scanned string. int main(int argc, char *argv[]) { char *string; scanf("%s",&string); printf("%s\n",string); } But gives a warning saying warning: format '%s' expects argument…
BangOperator
  • 4,377
  • 2
  • 24
  • 38
8
votes
2 answers

In C, How to use scanf to scan in 1,000,000 ignoring the commas

In C, what is the best way to ignore the commas when using scanf on a number like 1,000,000?
Dizzzyp
  • 137
  • 1
  • 9
8
votes
3 answers

Loop is running more than specified in C? Why?

If I enter the number 5, this loop has to run 5 times but it is running 6 times. What is the problem? int main(){ int i, *arr, size; printf("Please enter the Number: "); scanf("%d ",&size); arr = (int*) malloc(size…
Murali krishna
  • 823
  • 1
  • 8
  • 23
8
votes
5 answers

Char arrays and scanf function in C

I expected to get errors in following code, but I did not. I did not use & sign. Also I am editing array of chars. #include int main() { char name[10] ="yasser"; printf("%s\n",name); // there is no error , // trying…
BratBart
  • 369
  • 2
  • 3
  • 13
8
votes
4 answers

Using sscanf to extract an int from a string in C++

My function must process strings that look like say hello y(5) or data |x(3)|, and I need to be able to extract the integer shown and store it into a separate int variable called address. However, some strings passing through will not have any…
J. Shephard
  • 93
  • 1
  • 1
  • 6
8
votes
2 answers

Why is adding a leading space in a scanf format string recommended?

I am currently reading this book "C11 Programming for beginners" and in the scanf() chapter it says: "Always add a leading space before the first control string character to ensure accurate character input." As in: scanf(" %s", whatever); The…
Stanley
  • 69
  • 1
  • 3
8
votes
2 answers

Is scanf guaranteed to not change the value on failure?

If a scanf family function fails to match the current specifier, is it permitted to write to the storage where it would have stored the value on success? On my system the following outputs 213 twice but is that guaranteed? The language in the…
M.M
  • 138,810
  • 21
  • 208
  • 365
8
votes
3 answers

sscanf wrapping function to advance string pointer in C

I have a function that makes a series of calls to sscanf() and then, after each, updates the string pointer to point to the first character not consumed by sscanf() like so: if(sscanf(str, "%d%n", &fooInt, &length) != 1) { // error…
Dusty
  • 2,283
  • 2
  • 20
  • 24
8
votes
2 answers

C++ scanf %la returns 0

Win7 64-bit gcc 4.8.2 g++ -Wall I tried to format my C++ sscanf as defined in Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"? but the return is x == 0. The program is given below. I can't figure out what I did wrong…
lostbits
  • 928
  • 1
  • 9
  • 23
8
votes
4 answers

Parsing a txt file

I am trying to parse a txt file which contains names in the format: "MARY","PATRICIA","LINDA","BARBARA","ELIZABETH",... This is the code I wrote: #include // Names scores int problem22() { FILE *f = fopen("names.txt", "r"); char…
xylon97
  • 153
  • 5
8
votes
2 answers

Reading values from CSV file into variables

I am trying to write a simple piece of code to read values from a CSV file with a max of 100 entries into an array of structs. Example of a line of the CSV file: 1,Mr,James,Quigley,Director,200000,0 I use the following code to read in the values,…
Dawson
  • 457
  • 2
  • 9
  • 21
8
votes
3 answers

Does ampersand in front of char's array affect scanf? Is it legit?

When we usually input the string, we do this: #include int main() { char str[256]; scanf("%s",str); //Other Operation } But, today, in programming class, one of my friends wrote scanf line like this: scanf("%s",&str); and it…
Sarun Intaralawan
  • 1,092
  • 3
  • 13
  • 30