Thread: character representation in C

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    character representation in C

    good day to all,
    please i need your guide on how to represent numbers from 10 and above(10,20,30,40,50,----100)as a character.i am working on a circuit that takes in character data for its operation,so i have declared a character variable to store the data but i am having problem with numbers from 10 and above.below is a sample of what i am doing:
    Code:
    #include<stdio.h
                                
       
    int main()
    {
        char data;
             scanf("%c",&data);
             switch(data)
             {
             case '1':
                      printf("today is sunday \n");
                      break;
             case '9':
                      printf("today is monday \n");
                      break;           
                    
             case 10:
                      printf("today is tuesday \n");
                      break;         
                    
             case 20:
                      printf("today is wednesday \n");
                      break;     
                      
             case 100:
                      printf("today is friday \n");
                      break;
                      default:           
                      printf("you entered a wrong number");
                      break;
                           
             }
                    
                    
        getch();
        return 0;  
        
    }
    i do not have problem with character '1' to '9',i am getting the expected output,but i can not figure out how to represent numbers from 10 and above as character.your guide and support will be highly appreciated in this regards.
    best regards.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Thumbs up no closing '>' with your preprocessor directive

    hey i am new to c too but i believe you could you another loop appart for the switch case loop to process the character or you use the hex counting system where 10 is 0 and 11=a and continue that way cuz the case uses only a character

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It beats me why you would want more than 10 options to input a character that represents day of week. There are seven days in a week.

    Bear in mind that scanf() will skip whitespace, so not every character the user inputs can be read directly using scanf("%c" ...).

    However, there are the digits 0 to 9, lower case letters a through z, uppercase letters A through Z in every character set. What else is able to be input depends on the host system, but typical character sets have 32 (maybe more) printable characters. So, reading a single char, you'll have roughly 90 unique values to play with. Some character sets and keyboards will give you more: if you use one of those, your code will not be portable between compilers or systems.

    If you really want values greater than 10, the easier ways would be to use %d format specifier and read an int. The input "10" will give you the value 10 directly. And, nicely enough, an int is guaranteed to be able to represent at least 32000 distinct positive values.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    thank you very much mr nyah for the quick response.i really appreciate your sense of understand.
    i coud have used another approach but the input device to my circuit works with character that is more reason i have to program it this way .any further help will be highly appreciated.best regards.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    thank you very much mr grumpy for the quick response.i really appreciate your sense of understand.
    i used the days of the week to only demonstrate how my circuit works,they are not my actual output.the numbers 0 to 100 represent data from the user,the host system takes action base on what the user enters,but they have to be in character format because my input device works with charater.i do not intend to print the number on the screen.they are meant to be sent the host system in order for it to take the appropriate action.any further help will be highly appreciated.
    best regards.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You need to better understand what character input means... User is limited to typing in one character? Then you've got a value from 0 to 255 if you can bypass some special case trapping and handle ASCII above 127.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number representation
    By hanniball in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2010, 02:31 AM
  2. number representation
    By fsanchez in forum C Programming
    Replies: 6
    Last Post: 08-12-2010, 12:18 PM
  3. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  4. Character representation of integers
    By Maustrap in forum C Programming
    Replies: 2
    Last Post: 09-14-2006, 01:11 PM
  5. data representation
    By Absi in forum C++ Programming
    Replies: 13
    Last Post: 02-03-2003, 10:59 PM