string interrupted when there is a space

This is a discussion on string interrupted when there is a space within the C Programming forums, part of the General Programming Boards category; Hi. I study C. I wrote the following program: Code: main() { char yourname[20]; printf("Enter your name: "); scanf("%s", &yourname); ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    16

    string interrupted when there is a space

    Hi.

    I study C. I wrote the following program:

    Code:
    main()
    {
    char yourname[20];
    printf("Enter your name: ");
    scanf("%s", &yourname);
    printf("The name you entered is %s",yourname);
    getch();
    }

    But when I enter eg: 'Peter South', it only displays 'Peter'. I am used from Pascal, that the whole string is considered not to end when there is a space. How can I correct the program that it will display whole name user enters?

    I have also try to correct it, here is the result:

    Code:
    main()
    {
    char yourname[40];
    printf("Enter your name: ");
    scanf("%s", &yourname);
    int i;
    printf("The name you entered is:\n");
    for(i=0;i<40;i++){
    printf("%s",yourname[i]);
    }
    getch();
    }
    But this doesn't work. Could you please help?

    Thank you.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Use fgets(yourname,40,stdin) instead of scanf()

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    To further your reading on Tater's answer, read How to get a line of text - FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    You've received good advice, but if you want to know why that happens, here is the reason:

    Type character array. Inputs a sequence of non-whitespace characters (space, tab, carriage return, new line, vertical tab, or formfeed). The array must be large enough to hold the sequence plus a null character appended to the end.
    Source.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recvfrom getting : Interrupted system call
    By -EquinoX- in forum C Programming
    Replies: 15
    Last Post: 03-25-2009, 07:46 PM
  2. Interrupted System Call
    By Seiki in forum C Programming
    Replies: 2
    Last Post: 01-28-2009, 11:55 PM
  3. space in string
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2008, 12:54 AM
  4. Is there a way to check a string for a space
    By Raigne in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2005, 03:27 AM
  5. deleting a space from a string
    By niroopan in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2003, 09:43 PM

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