Like Tree1Likes
  • 1 Post By bthomson900

white space characters and scanf

This is a discussion on white space characters and scanf within the C Programming forums, part of the General Programming Boards category; I am studying for an exam at the moment (my first one for eng computing) and I came across this ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Question white space characters and scanf

    I am studying for an exam at the moment (my first one for eng computing) and I came across this in my lecture notes:

    'Care must be taken when reading characters as such with
    scanf( ) as the 'white space' characters, viz. blank, tab (\t)
    and newline (\n) are valid characters for the %c conversion.'

    Correct me if I'm wrong but I understand that 'white space' is any multiple of blank, tab or new line spaces (so anything that is not occupied by characters basically i.e pressing spacebar, tab or enter whilst entering the input character stream).

    What confuses me is how could blank, tab and newline be "valid characters" for conversion? What exactly does this mean? Could someone please provide an example to explain this?

    I mucked around a bit and if I had two, character type variables declared and I entered "\n 5" into the input character stream, then the two variables are assigned \ and n respectively. I think I am interpreting this information the wrong way.
    kawaikx15 likes this.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by bthomson900
    Correct me if I'm wrong but I understand that 'white space' is any multiple of blank, tab or new line spaces (so anything that is not occupied by characters basically i.e pressing spacebar, tab or enter whilst entering the input character stream).
    Yes, so a white space character is one such character.

    Quote Originally Posted by bthomson900
    What confuses me is how could blank, tab and newline be "valid characters" for conversion? What exactly does this mean? Could someone please provide an example to explain this?
    The idea is that for most conversion specifiers, scanf will skip leading whitespace, but %c will not skip them, but rather match the character.

    Quote Originally Posted by bthomson900
    I mucked around a bit and if I had two, character type variables declared and I entered "\n 5" into the input character stream, then the two variables are assigned \ and n respectively. I think I am interpreting this information the wrong way.
    As you noted, whitespace characters can be entered by "pressing spacebar, tab or enter", so entering "\n" is wrong.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    The idea is that for most conversion specifiers, scanf will skip leading whitespace, but %c will not skip them, but rather match the character.
    Alright, I understand "skip leading whitespace", but what exactly do you mean by "%c will not skip them, but rather match the character". Could you provide a small example maybe?

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by bthomson900
    Alright, I understand "skip leading whitespace", but what exactly do you mean by "%c will not skip them, but rather match the character". Could you provide a small example maybe?
    Here is a simple example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char c;
        scanf("%c", &c);
        printf("You entered '%c'\n", c);
        return 0;
    }
    Eneter whatever input you want, with say, a space in front. Observe the output.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Talking

    Thanks mate. I think I've got it now.

    I also added a few more variables of character type to the scanf function.

    What I noticed was that any leading 'white space' (of any size) became was assigned to the first variable read. Furthermore, any subsequent white space between other characters entered into the input stream does not affect the assignment of any following characters.

    i.e.

    if I had three variables and entered: " 1 2"

    the output generated would be: "You entered ' ','1' and '2'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. Character literals in scanf
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 08-09-2008, 02:36 PM
  3. Read two words separated by a space with scanf at once
    By caduardo21 in forum C Programming
    Replies: 4
    Last Post: 06-23-2005, 04:17 PM
  4. Masking Characters In Scanf
    By coolshyam in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 07:55 AM
  5. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21