How to display full name in C Programming?

This is a discussion on How to display full name in C Programming? within the C Programming forums, part of the General Programming Boards category; I have tried the code below but it does not display the second name, only the first name. Everything else ...

  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
    21,397
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,866
    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, 03: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, 03: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

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