Thread: Whitespaces in Strings

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    India
    Posts
    2

    Whitespaces in Strings

    Why aren't whitespaces in a strings not displayed? Does the scanf() function stop accepting values which lie after a whitespace?

    For example:

    Code:
    int name[100]; 
    
    printf("Whats your name?");
    scanf("%s",&name);
    My input:
    Code:
    Harry Potter
    Output:
    Code:
    Harry

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Most likely another one of scanf()'s many bugs. Use fgets() instead, and read Things to Avoid in C/C++ -- scanf, Part 5 - GIDNetwork

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    India
    Posts
    2
    fgets() is creating more prob then scanf() did....
    Errors:
    Code:
    Type mismatch in parameter '__n' in call to 'fgets'
    Too few parameters in call to 'fgets'
    Replacing scanf() by fgets(), is enough to get a correct output...or gonna need to get more things change in the code?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Take a look at this, I think it will help you.
    HomePort : A C Web Service API for heterogeneous home automation systems

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Quote Originally Posted by memcpy View Post
    Most likely another one of scanf()'s many bugs. Use fgets() instead, and read Things to Avoid in C/C++ -- scanf, Part 5 - GIDNetwork
    Reading up to the next whitespace is standard documented behaviour for scanf. For %s for example: "This will read subsequent characters until a whitespace is found". I agree scanf is bad for other reasons, but this is not a bug, this is normal behaviour.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trim whitespaces
    By Pingoo in forum C Programming
    Replies: 13
    Last Post: 06-27-2010, 02:20 PM
  2. whitespaces
    By happyclown in forum C Programming
    Replies: 2
    Last Post: 01-06-2009, 10:33 PM
  3. sscanf & whitespaces
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 05-10-2006, 09:06 AM
  4. scanf and whitespaces
    By rkooij in forum C Programming
    Replies: 25
    Last Post: 03-08-2006, 03:55 AM
  5. Extracting Whitespaces
    By TechWins in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2002, 07:28 PM