C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 3)
3.
What will be the output of the program ?
#include<stdio.h>
int main()
{
void fun(int, int[]);
int arr[] = {1, 2, 3, 4};
int i;
fun(4, arr);
for(i=0; i<4; i++)
printf("%d,", arr[i]);
return 0;
}
void fun(int n, int arr[])
{
int *p=0;
int i=0;
while(i++ < n)
p = &arr[i];
*p=0;
}
Answer: Option
Explanation:
Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.
Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4
Step 3: int i; The variable i is declared as an integer type.
Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.
Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.
Hence the output of the program is 1,2,3,4
Discussion:
25 comments Page 1 of 3.
Vignesh J said:
3 years ago
fun(4,arr) does not have an effect on the code because at the termination of the while loop.
while(i++ < n)
p = &arr[i];
n will be 4 and p = &arr[4] (which is outside the size of the array)
So, when *p = 0;, value at arr[4] will be 0 which is outside the limit of the array so it function call will not have any effect.
while(i++ < n)
p = &arr[i];
n will be 4 and p = &arr[4] (which is outside the size of the array)
So, when *p = 0;, value at arr[4] will be 0 which is outside the limit of the array so it function call will not have any effect.
(2)
Anon said:
3 years ago
fun(4,arr) can have an effect on the code.
It doesn't in this case due to the way the while loop is defined, which changes arr[4] (which doesn't concern us).
Replace i++ with ++i to see if the while loop has an effect on the values of the array.
It doesn't in this case due to the way the while loop is defined, which changes arr[4] (which doesn't concern us).
Replace i++ with ++i to see if the while loop has an effect on the values of the array.
Aamir said:
5 years ago
In fun function, only changes are made to p and *p, arr is unaffected. It is not being used on the left-hand side of any statement. So the line fun(4, arr) has no effect on the code.
Preeti said:
6 years ago
In fun(4, arr) function , there is no printf statement. So it does not affect output.
Lingam said:
8 years ago
Please explain the while loop condition not used please explain.
Keerthi said:
10 years ago
Thanks @Sanny after reading all this comments you only cleared my doubt thanks a lot.
Sanny said:
1 decade ago
Because *p=0 is not in while loop and after finishing the while loop is is giving the value 0 for the *p variable. So there is no effect on the value of array elements.
Jagpal singh said:
1 decade ago
In function calling base address of array is passing and in function prototype it accept an array as argument.
Why this code won't give any error? please answer.
Why this code won't give any error? please answer.
Atuljain said:
1 decade ago
this output like 1, 2, 3, 4 because finally pointer p.
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]='\0'
Finally p point to null's address and above we print only 4 times if you i<=4 then you get arr[4]=0;
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]='\0'
Finally p point to null's address and above we print only 4 times if you i<=4 then you get arr[4]=0;
Nivin said:
1 decade ago
No it's because post increment, increments only after end of statement. So here (i++<n) p gets address of arr[4]. And we don't get any error because we are not accessing it and only assignment is done.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers