Thread: white space characters and scanf

  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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08: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