Output of C Programs | Set 15
Predict the output of below C programs.
Question 1
#include<stdio.h> int main(void) { int a = 1; int b = 0; b = ++a + ++a; printf("%d %d",a,b); getchar(); return 0; } |
chevron_right
filter_none
Output: Undefined Behavior
See http://en.wikipedia.org/wiki/C_syntax#Undefined_behavior )
Question 2
#include<stdio.h> int main() { int a[] = {1, 2, 3, 4, 5, 6}; int *ptr = (int*)(&a;+1); printf("%d ", *(ptr-1) ); getchar(); return 0; } |
chevron_right
filter_none
Output: 6
&a is address of the whole array a[]. If we add 1 to &a, we get “base address of a[] + sizeof(a)”. And this value is typecasted to int *. So ptr – 1 points to last element of a[]
Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.
Recommended Posts:
- Output of C++ programs | Set 23
- Output of C++ programs | Set 24 (C++ vs C)
- Output of C programs | Set 52
- Output of C programs | Set 55
- Output of C++ programs | Set 45
- Output of C programs | Set 51
- Output of C++ programs | Set 42
- Output of C++ programs | Set 22
- Output of C++ programs | Set 44
- Output of C programs | Set 48
- Output of C programs | Set 34
- Output of C programs | Set 29
- Output of C programs | Set 65 (If-Else)
- Output of C programs | Set 32
- Output of C programs | Set 63



