Help with a name in scanf

This is a discussion on Help with a name in scanf within the C Programming forums, part of the General Programming Boards category; Hi all, Could someone please help, I have just started learning C and going through some end of chapter exercises ...

  1. #1
    Registered User taffy's Avatar
    Join Date
    Mar 2011
    Location
    South Wales, UK
    Posts
    2

    Help with a name in scanf

    Hi all,

    Could someone please help, I have just started learning C and going through some
    end of chapter exercises which already have me stumped.
    The exercise reads like this:-

    "Create a program that prompts a user for her name. Store the users name using the
    scanf() function and return a greeting back to the user using her name."

    I am creating programs like this with numbers, characters and floating point numbers no problem at all. But I cant get it to work with a name.

    I know it's a very basic thing but I cant move on to the next chapter untill i've got it worked
    out. I would really appreciate some help.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Code:
    char name[50]
    ...
    printf("What is your name? ");
    scanf("%s", name);
    printf("Hello there, %s\n", name);

  3. #3
    Registered User taffy's Avatar
    Join Date
    Mar 2011
    Location
    South Wales, UK
    Posts
    2

    Thank you

    Thank you so much nonoob.

    I was missing the [50] from the char data type
    (no mention of it in the chapter I just did).

    I also didn't know you could have the %s conversion specifier with a char data type
    (no mention of that in the chapter either).

    I can move onto "CONDITIONS" now.

    Again thank you very much.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Code:
    char name[50]
    ...
    printf("What is your name? ");
    scanf("%s", name);
    printf("Hello there, %s\n", name);
    One small improvement to prevent buffer overflows...

    Code:
    char name[50];
    ...
    printf("What is your name? ");
    scanf("%49s", name);           // limit number of input characters 
    printf("Hello there, %s\n", name);
    I know it's a bit ahead of the OPs skills but there's no time like the present for forming good programming habits.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Excellent idea, 'tater. I never used it like that. You sure it shouldn't be %.49s because number to the left of decimal is output field size, number to the right is hard limit?? At least for output. I'll have to play.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Excellent idea, 'tater. I never used it like that. You sure it shouldn't be %.49s because number to the left of decimal is output field size, number to the right is hard limit?? At least for output. I'll have to play.
    In scanf it's a hard limit... "%49s" will stop reading at 49 characters.... The user can type pages and pages and only the first 49 characters get loaded into the variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. some questions about scanf
    By winggx in forum C Programming
    Replies: 1
    Last Post: 03-28-2010, 06:16 PM
  3. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 07:35 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

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