558 views
0 votes
0 votes

Consider the following C program:
int fun(int p, int*q)
{
int n;
*q+=5;
n=*q;
p=p+n;
return(p+n);
}
main()
{
int i=0, *p, y;
p=&i;
y=fun(i, p);
printf("%d", y);
}
The output of the above program is ________.

  1.   5
  2.   10
  3.   15

-----------------------------------------------------------------------------------------------------------------------------------------------

here i is sent by value and address of i is sent in p

in func()--

first statment ,*q+=5 makes *q =5 which implies that i has become 5 now.so does p also become 5 here?i dun think so,because p is the copy of 'i'.it should not be changed to 5,it should stay 0 only.

now next statement p=p+n means p=0 +5=5

now,return statement (p+n) returns (5+5) =10

am i correct or not??

1 Answer

Best answer
0 votes
0 votes
Yes you are correct.

Formal paramaters are local data for the function. It gets erased after function scope is over. But any changes made by function to non-local (global data or any other data whose scope is not over yet accessed by function) data are retained.

In your question p can not become 5 because its a copy of local data

to function.
selected by

Related questions

0 votes
0 votes
0 answers
1
Anirudh Kaushal asked Apr 6, 2022
198 views
#include<stdio.h struct marks { int p:3; int c:3; int m:2; }; void main() { struct marks s = {2, -6, 5}; printf("%d %d %d", s.p, s.c, s.m); }What does p:3 means here ?
0 votes
0 votes
1 answer
2
Parshu gate asked Nov 19, 2017
374 views
#include<stdio.h>int main(){ int a=2;if(a==2){ a=~a+2<<1; printf("%d",a);}}
2 votes
2 votes
2 answers
3
Parshu gate asked Nov 5, 2017
1,359 views
OPTIONSA)7,19 B) 10,1 C) 10,23 D)10,32
1 votes
1 votes
1 answer
4
Archies09 asked Apr 23, 2017
3,242 views
What will be the output of the program?#include<stdio.h int addmult(int ii, int jj) { int kk, ll; kk = ii + jj; ll = ii * jj; return (kk, ll); } int main() { int i=3, j=4...