Thread: reading off characters

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    8

    reading off characters

    hi i want to ask how to read off characters into display. here is my code:

    Code:
    #include <stdio.h>
    
    int main()
    {
         char name;
    
         printf("Please enter your name: ")
         scanf("%c", &name);
    
         printf("%c loves lenovo thinkpads.", name);
    
    return 0;
    }
    everytime i press run the followong result appears:

    Code:
    Please ente your name: amie
    a loves lenovo thinkpads.
    i don't know why the first character only appears.
    Last edited by amie001; 06-07-2011 at 12:37 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A single char can only hold 1 char.

    You need an array of them to store a whole word.

    And change your %c formats to %s (read about them).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Hey Salem, you know what would be cool? If someone would sit down and write a tutorial about this and, wait brainstorm coming, I know make it available on this site!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in single characters
    By pallum in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2010, 12:55 AM
  2. problem with reading characters
    By csvraju in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:59 AM
  3. Reading characters from a string?
    By ladysniper in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 11:45 PM
  4. reading characters
    By sara.stanley in forum C Programming
    Replies: 9
    Last Post: 02-12-2006, 06:07 PM
  5. Reading Characters.
    By Red Army in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2002, 11:36 AM