Thread: Reading string with white space in C

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Reading string with white space in C

    I know how to do this easily in C++ however i can't figure it out in C.

    I want to do

    char fullname[20];

    printf("Enter full name : ");

    let's say the user enters "John Doe Duval"

    scanf("%s", fullname) --> only reads John.

    How do i read "John Doe Duval" into a string??

    Thx
    GEP

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can either use the format string "%[^\n]" for scanf - that means "read anything up until a newline" as a string.

    The other option is to use fgets().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    THX Mats !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. string input...with white spaces?
    By Pureghetto in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 01:52 PM