![]() |
VOOZH | about |
Predict the output of the below programs.
Question 1
Output:
Compiler dependent
The evaluation order of parameters is not defined by the C standard and is dependent on compiler implementation. It is never safe to depend on the order of parameter evaluation. For example, a function call like above may very well behave differently from one compiler to another.
References:
https://gcc.gnu.org/onlinedocs/gcc/Non_002dbugs.html
Question 2
Output:
Not Same
&arr is an alias for &arr[0] and returns the address of the first element in the array, but &p returns the address of pointer p.
Now try the below program
Question 3
Output:
4 3
sizeof(arr) returns the amount of memory used by all elements in array
and sizeof(p) returns the amount of memory used by the pointer variable itself.
Question 4
Output:
0
In C, variables are always statically (or lexically) scoped. The binding of x inside f() to global variable x is defined at compile time and not dependent on who is calling it. Hence, the output for the above program will be 0.
On a side note, Perl supports both dynamic and static scoping. Perl's keyword "my" defines a statically scoped local variable, while the keyword "local" defines a dynamically scoped local variable. So in Perl, a similar (see below) program will print 1.
Reference:
https://en.wikipedia.org/wiki/Scope_%28programming%29
Please write comments if you find any of the above answers/explanations incorrect.