I have written this C code in which I'm declaring a variable inside switch body with initialializing it to 10.But when I'm running this code it is printing some garbage value.

#include <stdio.h>

int main()
    switch(2) {
              int x=10;
              case 1: printf("Case 1: %d\n",x);
              case 2: printf("Case 2: %d\n",x);

    return 0;

Is their anyone who can explain even if the variable x is declared why is it not initialized to 10 and some garbage value is printed.

Recommended Answers

All 8 Replies

When I compile this as C++, I get a compiler error that says:
error C2360: initialization of 'x' is skipped by 'case' label

It means that line of code might not be executed, so the initialization can fail.

The initialization of identifier can be skipped in a switch statement. You cannot jump past a declaration with an initializer unless the declaration is enclosed in a block. (Unless it is declared within a block, the variable is within scope until the end of the switch statement.)

Thanks thines01 for reply.I run this code in gcc-3.4.2 in windows . I'm getting warning "warning: unreachable code at beginning of switch statement".But compilation is complete and when I'm running this code it is printing some garbage value.
If that line of code is never executed how x is declared ?When accessed it is giving garbage value in switch.

switch (2) {
   int x = 10;
   case 2: printf("Case 2: %d\n",x); 

Is the same as...

goto 2;
int x = 10;
printf("Case 2: %d\n",x);

Which is the same as...

printf("Case 2: %d\n",x);

And since x is not initialised with any value, it will print garbage.

Thank you N1GHTS but my problem is not how to run this code but to know why this code is running and giving garbage value ?

That was not a tutorial on how to run the code. I was trying to explain why the code is giving garbage value.

A "switch" is a more elegant "goto." A "goto" is a command that jumps over code. Your switch is jumping over x=10. It never gets to be initialised, thus, it has no useful information in it, thus it is garbage.

hmm , but as the code to initialize and declare both are in the same statement why one is executing (delaration) but other never happens (initialization) .

Ok, thats a different question. When you compile your code in C, it gets converted to machine code (assembly). That code is not a direct representation of C.

When you type this:

int a;

That is not compiled. That's because its only useful for the compiler. The computer itself does not care if "a" is an integer, only the compiler cares.

This code does get compiled:

int a = 10;

It is putting the value of 10 to some memory space, probably a register.

Code in C generally executes one line at a time, and if you use a "switch", the program will jump to wherever you are "switching" to.

switch (2) {  <--- Starts Here
   int a = 10;  <-- ignored
   case 1:      <-- ignored
      break;    <-- ignored
   case 2:    <---- Jumps to here
      break;  <---- Exits the switch
   case 3:      <-- ignored
      break;    <-- ignored
}             <---- Jumps to here

It will only pay attention to "case 2"

hmm , That's the logical explanation. Thanks N1GHTS .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.