scanf/printf char troubles
This is a discussion on scanf/printf char troubles within the C Programming forums, part of the General Programming Boards category; When I run this and enter my name I get a single character output.
char cName = '
';
printf("\n\tGreetings\n");
printf("\nPlease ...
-
Registered User
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);
-
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.
-
Registered User
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
Similar Threads
-
By Teiji in forum C Programming
Replies: 2
Last Post: 03-12-2009, 11:27 PM
-
By silentintek in forum C Programming
Replies: 1
Last Post: 10-27-2008, 09:32 PM
-
By gmanUK in forum C Programming
Replies: 5
Last Post: 11-25-2005, 02:03 PM
-
By studentc in forum C Programming
Replies: 3
Last Post: 06-11-2004, 03:07 PM
-
By netboy in forum C Programming
Replies: 4
Last Post: 06-11-2002, 10:26 PM
Tags for this Thread