Thread: scanf/printf char troubles

  1. #1
    Registered User XantuVolo's Avatar
    Join Date
    Apr 2011
    Location
    Minneapolis, Minnesota
    Posts
    2

    Question scanf/printf char troubles

    When I run this and enter my name I get a single character output.

    char cName = '\0';

    printf("\n\tGreetings\n");
    printf("\nPlease tell us your name: ");
    scanf("%c", &cName);
    printf("\nWelcome %c", cName);

    this is a challenge question from the Michael A. Vine "C-Programming for the absolute beginner." I believe Ive done everything right here as this same formula has worked for Integers. Do I have to change to this?

    printf("\nWelcome %.10c", cName);

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) Use code tags please... << !! Posting Code? Read this First !! >>

    2) You should usually post compilable code in case people want to test it.

    3) Read up on strings a little bit more...

    Code:
    char cName[10] = {0};
    
    printf("\nGreetings\n");
    printf("\nPlease tell us your name : ");
    scanf("%9s", cName);
    printf("\nWelcome %s", cName);
    It should come as no surprise that a char can only hold 1 character...
    In C strings are arrays of characters where the last used element of the array is tagged with a trailing 0.
    The library functions cooperate to treat these arrays like text.

  3. #3
    Registered User XantuVolo's Avatar
    Join Date
    Apr 2011
    Location
    Minneapolis, Minnesota
    Posts
    2
    Ok I have not gone over strings yet. I am self studying so I just follow the chapters in this book. Thanks for the information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with printf and scanf
    By Teiji in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 11:27 PM
  2. printf and scanf
    By silentintek in forum C Programming
    Replies: 1
    Last Post: 10-27-2008, 09:32 PM
  3. scanf and printf
    By gmanUK in forum C Programming
    Replies: 5
    Last Post: 11-25-2005, 03:03 PM
  4. printf and scanf
    By studentc in forum C Programming
    Replies: 3
    Last Post: 06-11-2004, 03:07 PM
  5. something about printf (or maybe scanf)
    By netboy in forum C Programming
    Replies: 4
    Last Post: 06-11-2002, 10:26 PM

Tags for this Thread