Home »
C programming language
Find output of C programs (Arrays)
Learn: In this article we enhance our knowledge regarding array in C by solving and finding output of some C programming questions from the topic array.
Submitted by Amit Shukla, on June 23, 2017
Here you will find C programs with output and explanations based on array.
1) What will happen if we assigned a value to an array element whose size of subscript is greater than the size of array in C programming?
- A compilation error occurs.
- The element will be set 0.
- The program crashes at run time.
- The array size increases automatically.
Output
C) The program crashes at run time.
Explanation
In C programming if we assign a value to array element whose size of subscript is greater than size of subscript of array then it crashes at run time because array have fixed size for data management. We cannot extend or reduce the size of array. Due to this drawback of array, linked list is formed. In linked list we can increase or decrease the size. But now modern gcc compilers will take care of this kind of errors.
Run program below program, it will crash in Turbo C compiler.
#include <stdio.h>
int main()
{
int arr[10];
arr[15]=200;
printf("%d",arr[15]);
return 0;
}
2) In C, if user passes array as an argument then, what actually get passes?
- Base address of the array will be passed.
- Value of first element is passed.
- Value of last element is passes.
- Address of last element is passed.
Output
A) Base address of the array will be passed.
Explanation
In this question statement A is correct, In C programming (turbo C) when we pass an array as an argument in any function then the base address of the array will be passed.
Example:
#include <stdio.h>
int main()
{
int arr[2][2]={ 1, 2, 3, 4 };
printf("%u\n", arr);
return 0;
}
Output
10485312
Hence, in above program the output is base address of arr.
3) What will be the output of the following program in turbo C?
#include <stdio.h>
int main()
{
int arr[8];
int n=0;
while(n<8)
{
arr[n]=++n;
}
for(n=0; n<8; n++)
{
printf("%d, ",arr[n]);
}
return 0;
}
- 1, 2, 3, 4, 5, 6, 7,
- 1, 2, 3, 4, 5, 6, 7, 8,
- 0, 1, 2, 3, 4, 5, 6, 7,
- Garbage value, 1, 2, 3, 4, 5, 6, 7,
Output
D) Garbage value, 1, 2, 3, 4, 5, 6, 7,
Explanation
In this question garbage value is assigned in first element arr[0] because due to ++n operator first value of variable n increased by 1 and then at a[1] the value 1 is assigned. Hence no value is assigned in a[0]. That’s why we got garbage value in it.
4) What will be the output of the following program in C?
#include <stdio.h>
int main()
{
char arr[]={ 'A', 'B', 'c', 'd', 'E', 'f' };
int size=sizeof(arr)/sizeof(arr[0]);
printf("%d\n", size);
return 0;
}
- 0
- 6
- 12
- 1
Output
B) 6
Explanation
The function sizes of returns the given variable.
Example:
char a='A';
sizeof(a) is 1 byte
Step 1: char arr[]={ 'A', 'B', 'c', 'd', 'E', 'f' };
The variablearr is declared as character array and it is initialized
with the values.
Step 2: int size=sizeof(arr)/sizeof(arr[0]);
The variablearr has 6 elements. The size of the char variable is 1 byte.
Hence 6 elements x 1 bytes = 6 bytes
sizeof(arr[0]) is 1 bytes
Hence 6/1 is 6 bytes
Hence the output of the program is '6'.
5) What will be the output of the following code?
#include <stdio.h>
int main()
{
int a[2][2] = { 1, 2, 3, 4 };
int *p;
p= &a[1][1];
printf("%d\n",*p);
return 0;
}
- 1
- 2
- 3
- 4
Output
D) 4
Explanation
Step 1 :-
int a[2][2] = { 1, 2, 3, 4 };
The variable a is declare as multi dimension integer array with size
2 rows and 2 columns.
Step 2 :-
int *p;
A integer pointer variable p is declared.
Step 3 :-
p= &a[1][1];
Then address of a[1][1] is stored in pointer variable p.
Step 4 :-
printf("%d\n",*p);
The value of at address p is printed.
And the value at address p is value of a[1][1].
Hence output result is '4'.