This is well explained in the C FAQ. See also: explanation. The proposed solutions:
- Quit using scanf. Use
fgets
and thesscanf
Use this to eat the newline
while((c = getchar()) != '\n' && c != EOF) /* discard the character */;
The fact that flushing stdin works on some implementations is wrong.
Some vendors do implement fflush so
that fflush(stdin) discards unread
characters, although portable programs
cannot depend on this.
Related Contents:
- Split string with delimiters in C
- What is the difference between char * const and const char *?
- Dividing 1/n always returns 0.0 [duplicate]
- Observing stale instruction fetching on x86 with self-modifying code
- Should I explicitly cast malloc()’s return value? [duplicate]
- Undefined reference to `pow’ and `floor’
- Find the highest order bit in C [duplicate]
- Is there a standard function in C that would return the length of an array?
- How to format a number using comma as thousands separator in C?
- ?: ternary conditional operator behaviour when leaving one expression empty
- Why is fseek or fflush always required between reading and writing in the update modes?
- What will happen if ‘&’ is not put in a ‘scanf’ statement?
- call printf using va_list
- Read no more than size of string with scanf()
- When the main thread exits, do other threads also exit?
- how to print __uint128_t number using gcc?
- How to use EOF to run through a text file in C?
- Is modification of string literals undefined behaviour according to the C89 standard?
- Error: Address already in use while binding socket with address but the port number is shown free by `netstat`
- How to format a function pointer?
- How to implement a timeout in read() function call?
- Conversion specifier of long double in C
- What is a designated initializer in C?
- variably modified array at file scope in C
- Is the output of printf (“%d %d”, c++, c); also undefined?
- Is it OK to call pthread_exit from main?
- How can I fix warnings like: “comparison between signed and unsigned”?
- Why can’t C compilers rearrange struct members to eliminate alignment padding? [duplicate]
- Casting one struct pointer to another – C
- Unsigned hexadecimal constant in C?
- What does “control reaches end of non-void function” mean?
- Modify a string with pointer [duplicate]
- faster equivalent of gettimeofday
- Can an equality comparison of unrelated pointers evaluate to true?
- Pointer to 2D arrays in C
- What is the purpose of the div() library function?
- Pointer/Address difference [duplicate]
- Pass integer value through pthread_create
- C’s strtok() and read only string literals
- What are the major differences between ANSI C and K&R C?
- How does sig_atomic_t actually work?
- What happens with an extern inline function?
- Why does “typdef struct { struct S *s; } S;” containing a pointer to same type compile?
- sizeof single struct member in C
- memcpy vs assignment in C
- How do *nix pseudo-terminals work ? What’s the master/slave channel?
- Which C datatype can represent a 40-bit binary number?
- read file backwards (last line first)
- Swap nodes in a singly-linked list
- Assign multiple values to array in C