Thread: Simple C Question

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Simple C Question

    I have a simple question. Below I have typed a program that prompts one to input their name and the program then prints out their name. The program works fine if you enter just a first name. If you press space and enter your last name as well the program doesn't work. Why?

    Here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char *name[125];
        
        printf("Please enter your name: ");
        scanf("%s", &name);
        
        printf("Your name is %s ", name);
        
        getchar();
        getchar();
     }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    scanf() stops at a space. If you want to read an entire line from a user (spaces and all), then use fgets() instead of scanf().

    Also, your usage is incorrect. You declared an array of char pointers which is incorrect. You should have just declared an array of chars, and passed that to scanf(). The first paragraph regarding scanf() stopping at spaces still applies though.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    i do not think getchar(); is needed.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    you have to tell specifically to scanf that consider the new line character also \n

    check your code with one modified line

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char *name[125];
    
      printf("Please enter your name: ");
      scanf("%[^\n]s", &name);
    
      printf("Your name is %s ", name);
    
      getchar();
      getchar();
    }

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by bythos View Post
    i do not think getchar(); is needed.
    It's to create a pause at the end of the program.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The simple answer is that scanf() leaves the newline character in the input buffer from the first issuance, and the second time scanf() is issued, it sees the newline character in the input buffer and treats the input as empty.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Thank You For Everyone's Help

    Thank you to all! I know it was a simple question but I had to ask. Thank you to each of you for sharing your experience.

    LittleShell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM