Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  1. I'm really not able to understand the whole concept of whitespace. I mean when they are skip by the scanf and when they are not and biggest question: How they are skipped?
  2. 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.

  3. 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?

  4. Why scanf do not skip non-white space character just like whitespace For example - a, b, c, d, @) # etc?

  5. What is the difference between space and newline character in scanf? I mean there will some exceptions right?
like image 590
Yogesh Tripathi Avatar asked May 26 '15 07:05

Yogesh Tripathi


People also ask

What is the purpose of whitespace characters?

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.

Which character are used to insert a white space?

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 does white space mean in code?

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.

How do you escape a white space?

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.


1 Answers

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

[...]

  1. Input white-space characters (as specified by the 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

[...]

  1. 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.
like image 105
Spikatrix Avatar answered Oct 01 '22 00:10

Spikatrix