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.
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? Space, tab, line feed (newline), carriage return, form feed, and vertical tab characters are called "white-space characters" because they serve the same purpose as the spaces between words and lines on a printed page — they make reading easier.
White space is created by pressing the Return key, spacebar key, or the Tab key, and can also be created by setting the document's margins and inserting form feeds or tables. How to change the text line spacing.
What is whitespace? Whitespace is any string of text composed only of spaces, tabs or line breaks (to be precise, CRLF sequences, carriage returns or line feeds). These characters allow you to format your code in a way that will make it easily readable by yourself and other people.
Three Ways to Escape Spaces on WindowsBy enclosing the path (or parts of it) in double quotation marks ( ” ). By adding a caret character ( ^ ) before each space. (This only works in Command Prompt/CMD, and it doesn't seem to work with every command.) By adding a grave accent character ( ` ) before each space.
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
[...]
- Input white-space characters (as specified by the
isspace
function) are skipped, unless the specification includes a[
,c
, orn
specifier. 284)
How they are skipped?
Just read and discard them.
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.
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.
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.
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
[...]
- A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With