What would be the output of the following program? main() { static int a[20]; int i = 0; a[i] = i++; printf ("\n%d%d%d", a[0], a[1], i); }
Correct Answer
0 0 1 That's what some of the compilers would give But some other compiler may give a different answer The reason is, when a single expression causes the same object to be modified and then inspected the behaviour is undefined
Programming problems
Search Results
1. Point out the error, if any, in the following program. main() { int i = 4, j = 2; switch(i) { case 1 : printf (''\n To err is human, to forgive is against company policy."); break; case j : printf (''\n if you have nothing to do, don't do it here."); break; } }
Correct Answer: Here we are initalising the function pointer p to the address of the function fun() But during this initialisation the function has not been defined Hence an error To eliminate this error add the prototype of the fun() before declaration of p, as shown below: extern int fun(); or simply int fun();
4. What is the difference between the following declarations? extern int fun(); int fun();
Correct Answer: The order may vary from compiler to compiler Here the multiplication will happen before the addition , but in which order the functions would be called is undefined In an arithmetic expression the parentheses tell the compiler which operands go with which operators but do not force the compiler to evaluate everything within the parentheses first
8. Which error are you likely to get when you run the following program? main() { struct emp { char name[20]; float sal; }; struct emp e[10]; int i; for ( i = 0 ; i <= 9; i++) scanf ( "%s %f" , e[i].name, &e[i].sal ); }