Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Switch case in C help

Status
Not open for further replies.

em_begin

Newbie level 2
Joined
Feb 11, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,302
hii
i have some query about switch case in C

case 'C':
case 'c':
switch (*comm++)
{
case 'S':
case 's':
command = STATUS;
break;
}
break;

Tell me why it is necessry to use this switch case format like C,c,S,s?
is ther any concept behind this implimentation.
so just give me the some idea behind this things.
actually this code is related to serial command.
and i also want to do this things for my serial comm.

thnx
 

if 'C' condition is true
or 'c' condition is also true
then it will do some task

similarly in case of S,s
 

I assume that you are asking if this is a case sensitive statement,
everything is case sensitive in C, so if you use 'c' only then upper case 'C' will not be recognized,
this is why both upper and lower case letters are used in the code you have provided.

Alex
 
Last edited:

to save having multiple case statements you can convert the character to upper or lower case using functions from <ctype.h>
Code:
    switch(toupper(getchar()))
      {
        case 'A': printf(" is A or a"); break;
        case 'B': printf(" is B or b"); break;
        case 'C': printf(" is C or c"); break;
      }
 

I hope your are not confused with C,c,S,s. If yes then the answer is -- The switch case statement and and "if" condition statement both work identical. In your case let's say we have 2 options and 2 sub-options. The first main option say C executes some true condition and if this is true then it will execute the sub-option c option.

Switch C:
case c:
Switch S:
case s:
case s1:
etc etc
case c1:
etc. etc.

So there are many way to use switch statements like the "if" statements.

Hope you are understand what I mean to say.

Thankyou
 

The switch statement does not see 'C' or 'c' as characters, but merely as binary values.


Code:
switch(*comm++)
  {
  case 0x63:  /* ascii 'c' */
  case 0x43:  /* ascii 'C' */

                do something;
                break;
  }

This achieves the same result.
 
Last edited:

each letter has a unique code in computer
c and C r 2 different numbers for computer
and if u use case c: and case C:
without break statement then the next statement is executed in both cases acting as an OR operation
 

em_begin
C and c translate differently into ASCII formats. In serial communication, we use ASCII coding.

ASCII: American Standard Code for Information INterchange.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top