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 2 of 3.
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.
Sri said:
1 decade ago
When I compile this program in c compiler it is showing the error a "Disallowed system call: SYS_socketcall". But here it is said that it gives the output. How could this be possible?
Gaurav said:
1 decade ago
please someone explain the working of the following statements clearly
while(i++ < n)
p = &arr[i];
first i is incremented or it is compared with n.
while(i++ < n)
p = &arr[i];
first i is incremented or it is compared with n.
PRADEEP said:
1 decade ago
Here i increment to 4 p is assigned as it is post increment. p=&a[4],so it put outside the array limit, .In the main array it is not effected.
U.Jhansi sri said:
1 decade ago
Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function. So why this wrote in the program.plz tell me.
Subbu said:
1 decade ago
Please tell me the the difference between *p=0 and **p=0
Harsha kumar said:
1 decade ago
The code will compile witout any error but it will lead to out of bound condition at run time so the program crashes.
Yogesh said:
1 decade ago
Whenever an array is passed as argument to a function, the pointer to the first element is actually passed. Then why the function fun () has no affect on its output. Please help.
Karthi said:
1 decade ago
Void function doesnt return anything, if the function is int then what will be the output.
Rupinderjit said:
1 decade ago
Here is the Simple solution.The "i" in the function definition is local to that block.It's scope is only inside the curly braces of function definition.
And the "i" in main program doesn't knows the "i" in function definition.So function dentition's "i" has no effect on output,which implies fun(4,arr[]) has no effect on the output.
So the output is 1 2 3 4.
And the "i" in main program doesn't knows the "i" in function definition.So function dentition's "i" has no effect on output,which implies fun(4,arr[]) has no effect on the output.
So the output is 1 2 3 4.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers