Predict the output of following C programs.
Question 1
C
Output: 0
C (or C++) allows arrays of size 0. When an array is declared with empty initialization list, size of the array becomes 0.
Question 2
C
Output:
C
#include <stdio.h>
int main()
{
int arr[] = {};
printf("%d", sizeof(arr));
return 0;
}
#include<stdio.h>
int main()
{
int i, j;
int arr[4][4] = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
printf("%d ", j[i[arr]] );
printf("\n");
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
printf("%d ", i[j[arr]] );
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16Array elements are accessed using pointer arithmetic. So the meaning of arr[i][j] and j[i[arr]] is same. They both mean (arr + 4*i + j). Similarly, the meaning of arr[j][i] and i[j[arr]] is same. Question 3
#include<stdio.h>
int main()
{
int a[2][3] = {2,1,3,2,3,4};
printf("Using pointer notations:\n");
printf("%d %d %d\n", *(*(a+0)+0), *(*(a+0)+1), *(*(a+0)+2));
printf("Using mixed notations:\n");
printf("%d %d %d\n", *(a[1]+0), *(a[1]+1), *(a[1]+2));
return 0;
}
Output:
Using pointer notations: 2 1 3 Using mixed notations: 2 3 4