Thread: How to display full name in C Programming?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Question How to display full name in C Programming?

    I have tried the code below but it does not display the second name, only the first name. Everything else after the space is not being displayed.
    Code:
    #include<stdio.h>
    main()
    {
    char name[20];
    
    printf("name: ");
    scanf("%s", name);
    
    printf("\n%s", name);
    
    getch();
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you are not reading the full name to begin with: %s format specifier for input stops reading at whitespace. You could consider the use of fgets instead, which also makes it somewhat easier to limit the number of characters read so as to avoid buffer overflow.

    By the way, <conio.h> is non-standard (and you don't really need getch, so you can ditch it). I might have written instead:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        char name[20];
    
        printf("name: ");
        scanf("%s", name);
    
        printf("\n%s", name);
    
        return 0;
    }
    Notice the return type of main and the indentation.
    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
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You can take a look at How to get a line of text FAQ to see what Laser is saying about fgets().

    And of course the main declaration.
    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
    Jan 2009
    Posts
    13
    As laserlight mentioned you can use the fgets() function. You could use this code to output your full name

    Code:
    #include <stdio.h>
    #define MAX 20
    
    int main(void)
    {
        char name[MAX];
        char * ptr;
    
        printf("name?\n");
        ptr = fgets(name, MAX, stdin);
        printf("Your name is %s\n", ptr);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Display full sentence from a file
    By georgio17 in forum C Programming
    Replies: 2
    Last Post: 04-16-2011, 04:24 AM
  2. Replies: 3
    Last Post: 01-29-2011, 04:54 AM
  3. Full window display on Console Application
    By jcheng3305 in forum Windows Programming
    Replies: 3
    Last Post: 05-06-2010, 12:38 PM
  4. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  5. Full Programming Videos
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-18-2001, 12:49 AM