What is really happening on removing and adding white space characters?
I'm new in programming and learning basics of C programming. I'm learning about how scanf()
works but I think now I'm very much confused and really don't know how and what to ask. But I will try my best to put my question clear.
Questions
scanf
and when they are not and biggest question: How they are skipped? Along with the whitespace concept I'm not able to understand the working of scanf
function also? I had read about it in many books and websites and also in this site but it confuse me more since each person has their own way of telling any concept and it vary from one to another.
Have a look at this short program:
#include<stdio.h>
int main()
{
int num;
char ch;
printf("enter the value of num and ch:n");
scanf("%d",&num);
scanf("%c",&ch);
printf("num = %d and ch = %c",num,ch);
return 0;
}
I know that in this program user will be allowed to enter the value of num
only, because of the new line character that stays back in the input buffer and next time scanf
will input that new line character but can be solved if we add extra space before %c
in the second scanf function.
But when I replace the char ch
variable with int ch
, scanf
skips the new line. Why?
Why scanf do not skip non-white space character just like whitespace For example - a, b, c, d, @) # etc?
space
and newline
character in scanf
? I mean there will some exceptions right? First Question
I mean when they are skip by the scanf and when they are not
White-space characters are skipped unless the format specifier is %c
, %n
or %[
. Relevant quote from the C11 standard:
7.21.6.2 The fscanf function
[...]
isspace
function) are skipped, unless the specification includes a [
, c
, or n
specifier. 284) How they are skipped?
Just read and discard them.
Second Question
I'm not able to understand the working of scanf function also?
scanf
is a variadic function meaning that it can take any number of arguments with a minimum of one. scanf
parses the first argument which is a string literal and accordingly, takes input.
Third Question
But when I replace the char ch variable with int ch, scanf skips the new line. Why?
First part of the first answer explains it. %d
will skip whitespace characters.
Fourth Question
Why scanf do not skip non-white space character just like whitespace?
For some conversion specifiers like %c
, non-whitespace characters are valid inputs. It doesn't make sense why they should skip them. For other like %d
, characters ( not numbers ) are invalid inputs. scanf
stops scanning and returns when it sees invalid input. It is designed this way.
Fifth Question
What is the difference between space and newline character in scanf?
There is no difference when any of them are placed in the format string in scanf
. Both of them are considered as whitespace characters, although they are different characters. They skip any number of whitespace characters, including none, until the first non-whitespace character when they are used in the format string of scanf
. Relevant quote from the C11 standard:
7.21.6.2 The fscanf function
[...]
上一篇: 计算流的元素
下一篇: 删除和添加空格字符真的发生了什么?