When you are trying to switch a case, the switching generally depends on the value which is assigned to the variable (in your case the variable is num
whose value is 3). The value of this variable can even change. There is no rule that a variable whose value is getting switched (num
) should be a constant integer. Switch cases are not meant for hard coded values. Depending upon the value of the variable that particular switch case will be executed and the value can even change (in you code it does'nt, because you have hard coded it as 3) Please try this code
#include <stdio.h>
int main()
{
//int const var = 3;
int num=3;
switch(num)
{
case 3: //If the value of num is "3", then execute this case.
printf("constant var");
break;
case 4: //If the value of num is "4", then execute this case.
printf("Test case");
break;
default: //If the value of num is anything other than "3" and "4", then execute this case
printf("Default case");
break;
}
return 0; //This should be included as you are returning an integer
}
Inside the switch case, you are supposed to mention the value (of num
) depending on which the case will be executed. Also, please note that when you have declared the return type of your main()
as integer, it is better you include a return 0
in the end. After you run the code with num=3
, please try the same code with num=4
and num=5
.