Introduction To Programming

Why programs ? Programming is nothing but making the computers think the same way a human thinks. Programs are made for obtaining a specific solution which can even be solved by humans which when solved by us may consume time..

Sunday, March 14, 2010

56

In the following program which one will run without any errors?:





#define a 2
int main()
{
int b=2;
switch(b)
{
case a:
printf("%d",b);
break;
}
return 0;
}

int main()
{
int a=2,b=2;
switch(b)
{
case a:
printf("%d",b);
break;
}
return 0;
}


Answer


The first program.
we can use only constants in case.
in first program a is defined using #define so it's constant.
in second program a is defined using integer datatype(obviously it's variable not constant).