"Switch", "int" and "char"!

This is a discussion on "Switch", "int" and "char"! within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> void main(){ unsigned char c=280; switch(c){ printf("Start\t"); case 280:printf("David Beckham\t"); case 24: printf("Ronaldinho\t"); default: printf("Ronaldo\t"); printf("End"); } } ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    11

    "Switch", "int" and "char"!

    Code:
    #include<stdio.h>
    void main(){
        unsigned char c=280;
        switch(c){
            printf("Start\t");
            case 280:printf("David Beckham\t");
            case 24: printf("Ronaldinho\t");
            default:  printf("Ronaldo\t");
            printf("End");
        }
        
    }
    When I run that code, the output is: Ronaldinho Ronaldo End

    But I don't know how 280 --> 24?? in switch expression.

    I hope some one will answer me, thanks you!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Take a "break" else the case statements fall through.
    Code:
    case 280: printf("David Beckham\t"); break;

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    You're missing the break after each case.
    Code:
        switch(c){
            printf("Start\t");
            case 280:printf("David Beckham\t");
                            break;
            case 24: printf("Ronaldinho\t");
                          break;
            default:  printf("Ronaldo\t");
                          break;
            printf("End");
        }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Or that 280 % 256 is 24

    Limited range of data type and all that - did you even get a warning when you compiled this code?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Quote Originally Posted by Salem View Post
    Or that 280 % 256 is 24

    Limited range of data type and all that - did you even get a warning when you compiled this code?
    Thanks Salem, I code in VS 2008 and no warning appeared.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    char is one byte, so it cannot have the value of 280. The compiler should have complained with a warning.

    Anyway, it goes to 24, because 280 is 100011000 now truncating this to one byte we have 00011000 which is 24.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Project->settings->compiler->Warning level

    Set it to maximum


    And change void main to int main while you're at it.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie help with "if" and "char"
    By morgog in forum C Programming
    Replies: 14
    Last Post: 02-23-2009, 01:52 PM
  2. Pronunciation of "char"
    By brewbuck in forum C Programming
    Replies: 10
    Last Post: 06-15-2007, 11:53 AM
  3. Difference between "char* buf "and "char *buf"
    By smo59 in forum C Programming
    Replies: 2
    Last Post: 09-02-2004, 04:44 AM
  4. How can I copy a "char *" to a "char"...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-31-2002, 11:52 AM
  5. Converting a "Char" string to an Integer Array
    By Jazz in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2001, 01:50 PM

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