What really happens when deleting and adding space characters?

I am new to programming and learning the basics of C programming. I will learn how scanf() works, but I think now I am very confused and really do not know how and what to ask. But I will try my best to raise the question.

Questions

  • I really can't understand the whole concept of spaces. I mean when they skip scanf , and when this is not the biggest question: How are they skipped?
  • Along with the concept of spaces, I also can not understand the function scanf ? I read about it in many books and websites, as well as on this site, but I am more confused about this, because each person has his own way of talking about a concept and varying from one to another.

  • Take 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 the user will be allowed to enter num only because of the new line character that remains in the input buffer, and next time scanf will enter this new line character, but maybe if we add extra space to %c in second scanf function.

    But when I replace the variable char ch with int ch , scanf skips the new line. Why?

  • Why does scanf not skip the white space character just like spaces. For example - a, b, c, d, @) #, etc.

  • What is the difference between space and newline characters in scanf ? I mean there will be some exceptions?
+7
c whitespace scanf
source share
1 answer

First question

I mean when they skip scanf, and when they don't

Space characters are skipped if the format specifier is %c , %n or %[ . Corresponding quote from standard C11:

7.21.6.2 fscanf function

[...]

  1. Entering space characters (as indicated by isspace ) is skipped if the specification does not include the [ , c or n specifier. 284)

How are they skipped?

Just read and discard them.

Second question

I also can not understand the function scanf?

scanf is a variational function , which means that it can take any number of arguments with at least one. scanf parses the first argument, which is a string literal and, accordingly, accepts input.

Third question

But when I replace the variable char ch with int ch, scanf skips the new line. Why?

The first part of the first answer explains this. %d skip whitespace.

Fourth question

Why does scanf not skip non-white spaces like spaces?

For some conversion specifiers, such as %c , characters without spaces are valid entries. It makes no sense why they should skip them. For others, such as %d , characters (not numbers) are invalid entries. scanf stops scanning and returns when it sees an invalid input. It is designed that way.

Fifth question

What is the difference between space and newline in scanf?

It makes no difference if any of them fit in a format string in scanf . Both are considered space characters , although they are different. They skip any number of whitespace characters, including none, to the first character without a space when they are used in a scanf format string. Corresponding quote from standard C11:

7.21.6.2 fscanf function

[...]

  1. A directive consisting of white space character (s) is executed by reading the input up to the first character of a non-white space (which remains unread) or until more characters are read. The directive will never work.
+4
source share

All Articles