Char and switch output

This is a discussion on Char and switch output within the C Programming forums, part of the General Programming Boards category; Hi, could anybody explain to me why the code below outputs "5050"? I'm having huuuge problems understanding the whole deal ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    4

    Exclamation Char and switch output

    Hi, could anybody explain to me why the code below outputs "5050"? I'm having huuuge problems understanding the whole deal with char. I just don't get it at all.

    The way I see it, "c = '1' + 1;" means c is currently 1, but not the ASCII code, but the actual character 1, because of ' '. Then you add 1 to it, which now makes it character 2.

    After that it's a blank. How does it suddenly become 5050?

    Thanx!

    Code:
    #include <stdio.h>
    int main() {
        char c;
        c = '1' + 1;
        switch (c) {
        case '1': printf ("%d", c);
        case '2': printf ("%d", c);
        case '3': printf ("%d", c);break;
        default: printf ("default");
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    In the ASCII character set, '1' has the numerical value of 49, and '2' has the numerical value of 50. %d prints the numerical value (which is what you are seeing). %c will print the character '2'.

    If you do a test, you will find that ('1' == 49) will yield true (again, assuming compatibility with the ASCII character set).

    You might want to look at this link. Note that the value '0' has decimal value 48 in the ASCII character set, and other digits have values that follow from there sequentially.

    '2' + 1 should be the value '3' (51 in ASCII). If you are not getting that, you are doing something wrong. '2' + '1', however, will have the value 99, which corresponds to the letter 'c' in ASCII (and something else, if your compiler or host system work with a different character set).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Thanx for the answer, grumpy, I think I get it, but let me review, just to be sure.

    "c = '1' + 1;" means you add 1 to the character '1' so it becomes '2', and numerical value of '2' --> 50

    Then, because "c = '1' + 1;" equals 2, case 2 and case 3 get executed, they output the numerical value of the character '2' because of %d --> 5050

    and at the end of the case 3 is a break, so the switch ends. Is that it?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Yes,case 2 and case 3 will be executed If you remove break default will also be shown at your screen
    Last edited by std10093; 07-09-2012 at 07:31 AM.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Great! Thanx to both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  2. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  3. Char in a Switch()
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2003, 02:25 PM
  4. Replies: 3
    Last Post: 02-19-2003, 07:34 PM
  5. problems getting output with int and switch
    By Lyanette in forum C++ Programming
    Replies: 5
    Last Post: 02-07-2003, 05:40 AM

Tags for this Thread


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