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
2
votes
1 answer

MPI fscanf() return false value

My file called "a.txt" contains a number (47). If I run the program on a compiler without MPI using fscanf(a,"%f",&num) it gives a true value nnum = 47. If I run the program using MPI ssh it does not return the correct value. it will return num =…
2
votes
1 answer

scanf getting stuck

Im a beginner in C. Im using scanf function in my c program and its getting stuck and the program moves ahead only when i type exit. The part of the code in which Im facing problem is: while(fread(&emr,recsize_emr,sizeof(emr)/recsize_emr,fp)==1) { …
Abhishek
  • 33
  • 7
2
votes
1 answer

Failed to read integer using fscanf in c

FILE *fin = fopen("figura.in", "r"); if(fscanf(fin, "%d %d %d %d", &int[0], &int[1], &int[2], &int[3]) == 1) { printf("%d\t%d\t%d\t%d\n", int[0], int[1], int[2], int[3]); } else { printf("failed to read integer.\n"); } I…
eksponente
  • 49
  • 2
  • 9
2
votes
4 answers

C: scanf with ==

I saw this piece of code. I am quite new in C, so pardon me. The while loop below will keep looping if i < SIZE && scanf("%f",&Arr[i]) == 1. I get the i < SIZE part but what does scanf("%f",&Arr[i]) == 1 mean? I know it is taking in the value from…
Lawrence Wong
  • 1,129
  • 4
  • 24
  • 40
2
votes
3 answers

C Trying to check for invalid input

I'm using scanf("%s", u); so I take a string. I can take the characters q, c, -, +, /, *, %, ^, =, and integers, but for everything else I want my program to display an error message. How would I know if it's any other character, because if they put…
Man Person
  • 1,122
  • 8
  • 20
  • 34
2
votes
1 answer

Working sscanf for Python (preferrably py3k)?

I have seen this question, but I am having problems with the top solution. For example: >>> scanf.sscanf("\"test\"","\"%s\"") Traceback (most recent call last): File "", line 1, in File "scanf.py", line 393, in sscanf return…
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
2
votes
3 answers

Force fscanf to Consume Possible Whitespace

I have a multiline TSV file with the following format: Type\tBasic Name\tAttribute\tA Long Description\n As you can see, the Basic Name and the Description can both contain some number of spaces. I am trying to read each line in and extract the…
Tanaki
  • 2,575
  • 6
  • 30
  • 41
2
votes
3 answers

Reading integers from input redirection in C

At the Unix command line, I need to use input redirection to specify the input file: ./a.out < filename Here is my source code: int main(int argc, char *argv[]) { char fileName[30]; fgets(fileName, sizeof fileName,…
David Kaczynski
  • 1,246
  • 2
  • 20
  • 36
2
votes
2 answers

Can we set a process as the master/root process in MPI?

MPI seems to automatically designate 0 as the master process. But I want to be able to designate another process (e.g. process with rank 10) to be the master process. Also, the function scanf only works in the master process: other processes simply…
Madhu
  • 253
  • 1
  • 8
  • 21
2
votes
2 answers

sscanf with less arguments than specified?

So I'm wondering how sscanf functions when faced with a line like this: sscanf(input_string, "%s %s %s", cmd1, cmd2, cmd3); But say the input_string only contains 1 string token. What values are assigned to cmd2 and cmd3? Is there an error…
Ethan
  • 1,206
  • 3
  • 21
  • 39
2
votes
3 answers

Why Does This Scanf Result in Infinite Loop?

So I've just started learning C for a class of mine, and I have been working on this assignment, but this part has me perplexed. I have some code that essentially looks like: #include #include int main(void) { int one = 0; …
This 0ne Pr0grammer
  • 2,632
  • 14
  • 57
  • 81
2
votes
1 answer

c sscanf with character delimiter

I have an input string of the form char *s = "one.two three" and I want to separate it into 3 string variables. I'm doing sscanf(s, "%s.%s %s", one, two, three); but it's reading in "one.two" as the string for variable one. How do I handle the "."…
user1190650
  • 3,207
  • 6
  • 27
  • 34
2
votes
1 answer

Xcode 4.5 character not show in console box , with scanf() in C language

I have got this problem since updated to Xcode 4.5. I typed 4555 in console box but it show only 4 in console box. I tried different input parameters such as %d, %f, %c and %s, all have the same problem. I don't get it. How to fix it? My source code…
2
votes
2 answers

fscanf fixed string size using macro

I want to parse an ip from file using fscanf (C code using gcc). so, I want to do: char myip[INET_ADDRSTRLEN]; fscanf(file, "%16s", myip); but, I don't want to hardcode the number 16, so I'm trying to use macro, but it doesn't work. #define…
ramone
  • 266
  • 1
  • 2
  • 10
2
votes
2 answers

pattern matching using scanf in C

I have been stuck up with this issue. The requirement of the problem is very simple. All I want to do is check user input and allow the user to enter only lower case letters. The other constraint is I want to do it using only scanf(). I know I can…
qre0ct
  • 5,680
  • 10
  • 50
  • 86
1 2 3
99
100