answersLogoWhite

0

prog1: #include<stdio.h>

int main()

{

switch(1)

{

int i=0;

case 1:printf("%d",i);

}

getchar();

return 0;

}

Output: garbage value.

prog2: #include<stdio.h>

int main()

{

switch(1)

{

printf("Inside Switch");

case 1:printf("Case 1\n");

}

printf("Outside Switch");

getchar();

return 0;

}

Output:

Case 1

Outside Switch.

The statements before a case labelled statement seem unreachable according to program 2 but then why don't i get an error for an undeclared variable i in the first program (only a warning).

Would be really helpful if someone could explain in detail that how the switch statement is treated internally.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What is break in c?

The break statement is used to exit a loop or switch-case.


What is the function of visual c plus plus switch condition?

The switch / case statement.


Why you use if and else statement in c language program?

There is no "elseif" statement in C. You can only use "else" and "if" separately. This is a good reason for switch/case/break.


Calling a function from switch in C?

Yes, you can call a function from within a switch statement in C. switch (i) { case 0: function1(i); break; case 1: function2(i); break; default: function3(i); break; } When the function returns, you will still be in the switch statement.


Which statement should you use if you have multiple conditions to check for the same variable?

SWITCHswitch( yourVar ){case 'A':foo++;case 'a':bar++;default :baz++;}The switch statement will only work with integer or character variables, however. If your variable is not of that simple type, then the switch statement will not work. In that case you need to use the standard if-then-else-if sequencing.


What are the similarities between if else and switch case in c program?

A switch-case statement is used to select between multiple values for a single variable. Like having a case for 1 2 and 3 for an integer. An If-else statement is used for evaluating an expression to either true or false.


When might you omit a break statement from the end of a case in a switchstatement?

You can omit the break statement at the end of a case whenever you want execution to flow into the next case, or when the case is the last case. For instance, if you wanted to test a character regardless of whether it was upper or lower case, you might use the following: void f(char c) { switch (c) { case 'a': // execution flows into next case... case 'A': // do something break; case 'b': // execution flows into next case... case 'B': // do something else } }


What is difference between select Case statement and nested loop in c programming in UNIX?

UNIX has no bearing on the C language; it is cross-platform. There is no select/case in C, you probably meant switch/case. However, a switch/case is a conditional jump while a nested loop is a loop within a loop. Besides the C language they have nothing in common with each other.


Specific case in which a statement is false?

A counterexample is a specific case in which a statement is false.


What is the purpose of Continue and break statement in C?

The break statement is frequently used to terminate the processing of a particular case within a switch statement. Lack of an enclosing iterative or switch statement generates an error.Within nested statements, the break statement terminates only the do, for, switch, or whilestatement that immediately encloses it. You can use a returnor goto statement to transfer control elsewhere out of the nested structure.This example illustrates the break statement:#include int main() { char c; for(;;) { printf_s( "\nPress any key, Q to quit: " ); // Convert to character value scanf_s("%c", &c); if (c == 'Q') break; } } // Loop exits only when 'Q' is pressed


What language introduced the case statement?

The case statement was first introduced by ALGOL-W.


Rudy wrote ideas for his report on preparing a supply kit in case of storms and other natural disasters. Which of these statements would BEST begin a paragraph about preparing a supply kit A)statement 1 B)statement 2 C)statement 3 D)statement 4?

Statement 4