The %c
conversion specifier won’t automatically skip any leading whitespace, so if there’s a stray newline in the input stream (from a previous entry, for example) the scanf
call will consume it immediately.
One way around the problem is to put a blank space before the conversion specifier in the format string:
scanf(" %c", &c);
The blank in the format string tells scanf
to skip leading whitespace, and the first non-whitespace character will be read with the %c
conversion specifier.
Related Posts:
- Convert char array to a int number in C
- Returning an array using C
- How to copy a char array in C?
- How to empty a char array?
- How do you allow spaces to be entered using scanf?
- Difference between fgets and fscanf?
- Reading in double values with scanf in c
- C: warning: excess elements in array initializer; near initialization for ‘xxx’ ; expects ‘char *’, but has type ‘int’
- How do I check if a string contains a certain character?
- Convert Char to String in C
- What is an unsigned char?
- Get a substring of a char* [duplicate]
- char *array and char array[]
- Expression must be a modifiable L-value
- What is the difference between char s[] and char *s?
- Char Comparison in C
- Reading a string with scanf
- Going through a text file line by line in C
- waitpid, wnohang, wuntraced. How do I use these
- Break statement not within loop or switch in C
- How to properly malloc for array of struct in C
- C: error: expected ‘)’ before ‘;’ token
- Where is the C auto keyword used?
- execv vs execvp, why just one of them require the exact file’s path?
- difference between
and - Bind failed: Address already in use
- Warning/error “function declaration isn’t a prototype”
- What is the difference between %f and %lf in C?
- What is stdin in C language?
- How to prevent multiple definitions in C?
- How to pass 2D array (matrix) in a function in C?
- How can you print multiple variables inside a string using printf?
- What are 0x01 and 0x80 representative of in C bitwise operations?
- How to make parent wait for all child processes to finish?
- Use of cudamalloc(). Why the double pointer?
- C dynamically growing array
- Why should we check WIFEXITED after wait in order to kill child processes in Linux system call?
- Mapping a numeric range onto another
- Why are hexadecimal numbers prefixed with 0x?
- OpenGL — GL_LINE_LOOP —
- too many arguments for format [-Wformat-extra-args]
- Valgrind complains with “Invalid write of size 8”
- Scanning Multiple inputs from one line using scanf
- Expression must be a pointer to a complete object type using simple pointer arithmetic
- cast to pointer from integer of different size, pthread code
- Invalid type argument of unary ‘*’ (have ‘int’) Error in C
- Why do I get “cast from pointer to integer of different size” error?
- C subscripted value is neither array nor pointer nor vector when assigning an array element value
- error: too few arguments to function `printDay’ (C language)
- Difference between sizeof(char) and sizeof(char *)
- Where to find the complete definition of off_t type?
- struct has no member named
- printf not printing to screen
- Initializing array of structures
- Assembly x86 – “leave” Instruction
- Undefined reference to main – collect2: ld returned 1 exit status
- Is there a way to have printf() properly print out an array (of floats, say)?
- Convert Little Endian to Big Endian
- Excess elements of scalar initializer for pointer to array of ints
- How does one represent the empty char?
- How to write to a file using open() and printf()?
- Can I define a function inside a C structure?
- Where is the
header file on Linux? Why can’t I find ? - What exactly is meant by “de-referencing a NULL pointer”?
- What is the difference between %g and %f in C?
- Why is the sizeof(int) == sizeof(long)?
- getc() vs fgetc() – What are the major differences?
- Structure padding and packing
- Allocating char array using malloc
- Tokenizing strings in C
- How to allocate array of pointers for strings by malloc in C?
- The difference between char * and char[] [duplicate]
- Reading float using scanf in c
- How to format strings using printf() to get equal length in the output
- “-bash: gcc: command not found” using cygwin when compiling c?
- How to format strings using printf() to get equal length in the output
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- lseek/write suddenly returns -1 with errno = 9 (Bad file descriptor)
- Should I use printf(“\n”) or putchar(‘\n’) to print a newline in C?
- What do \t and \b do?
- Why and when to use static structures in C programming?
- error: struct has no member named X
- Where does linux store my syslog?
- Example of waitpid() in use?
- Constructor for structs in C
- Can a function return two values?
- write() to stdout and printf output not interleaved?
- Split string with multiple delimiters using strtok in C
- warning: missing terminating ” character [enabled by default]
- FFT in a single C-file
- Implicit function declarations in C
- Convert long long to string in C?
- Using pointer to char array, values in that array can be accessed?
- How can I create a dynamically sized array of structs?
- malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))
- how to stop a loop arduino
- How to normalize a mantissa
- Level vs Edge Trigger Network Event Mechanisms
- How to use shared memory with Linux in C
- When is it ok to use a global variable in C?