Using the Switch statement with Characters?

This is a discussion on Using the Switch statement with Characters? within the C Programming forums, part of the General Programming Boards category; To Whomever Can help Me; Is it possible to use the Switch structure for characters? For example: switch(char) { case ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Unhappy Using the Switch statement with Characters?

    To Whomever Can help Me;

    Is it possible to use the Switch structure for characters? For example:

    switch(char)
    {
    case 1:char = 'b'
    .
    .
    .
    I'm trying to do it, but having trouble. Is this even possible? Any help would be great! Thanks!

    Dave

  2. #2
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    It is possible:
    Code:
    char choice;
    printf("Enter a letter...:");
    scanf("%c", choice);
    switch (choice) {
      case 'a': {
      ...
      }
      case 'b': {
      ...
      }
    }
    try that
    Last edited by Lau; 11-18-2002 at 12:14 PM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,439
    Yes, chars and ints can be used in switches. But don't name a variable 'char' it's a keyword y'know? Also, the label cannot be a variable name, see below:

    Code:
    char a = 'a';
    char b = 'a';
    
    switch(a){
     case b: a = 'x'; break; // <-- illegal.
     case 't': a = 'z'; break;
     case 12 : a = 'r'; break;
     default: a = 'p';
    }
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  4. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM
  5. Equivalent of less than in a switch statement?
    By Diamonds in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 07:14 PM

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