Output of C Programs | Set 4
Predict the output of below programs
Note the difference between structure/union declaration and variable declaration. When you declare a structure, you actually declare a new data type suitable for your purpose. So you cannot initialize values as it is not a variable declaration but a data type declaration.
We have created a 3D array that should have 2*3*3 (= 18) elements, but we are initializing only 13 of them. In C when we initialize less no of elements in an array all uninitialized elements become ‘\0’ in case of char and 0 in case of integers.
Initially ptr1 points to ‘k’ and ptr2 points to ‘\n’ in “geeks\nforgeeks”. In print statement value at str is incremented by 1 and value at ptr1 is decremented by 1. So string becomes “heejs\nforgeeks” .
First print statement becomes
printf(“%c”, ‘h’ – ‘j’ + ‘n’ + 2)
‘h’ – ‘j’ + ‘\n’ + 2 = -2 + ‘\n’ + 2 = ‘\n’
First print statements newline character. and next print statement prints “heejs\nforgeeks”.
Explanation: fun(n) calculates sum of first n integers or we can say it returns n(n+1)/2.
Output: Exception – Divide by zero
Explanation: There is a bug in the above program. It goes inside the do-while loop for c = 0 also. Be careful when you are using do-while loop like this!!