C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 8)
8.
What will be the output of the program ?
#include<stdio.h>
int *check(static int, static int);
int main()
{
int *c;
c = check(10, 20);
printf("%d\n", c);
return 0;
}
int *check(static int i, static int j)
{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}
Discussion:
50 comments Page 1 of 5.
Yash said:
7 months ago
#include<stdio.h>
int *check( int, int);
int main()
{
int *c;
c = check(10, 20);
printf("%d\n", *c);
return 0;
}
int *check( int i, int j)
{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}
Here we get the output is 20.
int *check( int, int);
int main()
{
int *c;
c = check(10, 20);
printf("%d\n", *c);
return 0;
}
int *check( int i, int j)
{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}
Here we get the output is 20.
Dharmesh said:
2 years ago
Static variables compiler stores in static/global (data section) as per storage class.
Here we are having pointers present in the code section pointing to static variables which are present in other storage sections giving error.
Also, we cannot use static variables as function parameters in C.
Here we are having pointers present in the code section pointing to static variables which are present in other storage sections giving error.
Also, we cannot use static variables as function parameters in C.
(2)
Omar said:
5 years ago
@Avinash.
* means that's this function return pointer to integer value.
* means that's this function return pointer to integer value.
(2)
Amit said:
7 years ago
Static is not allowed.
Avinash said:
8 years ago
What kind of function is this?
int *check();
I mean what does the * sign denote here?
int *check();
I mean what does the * sign denote here?
Harikrishnan V said:
8 years ago
I would like to add one more thing.
#include<stdio.h>
int *check( int, int);
int main()
{
int *c;
c = check(10, 20);
printf("%d\n", c);
return 0;
}
int *check(int i, int j)
{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}
This removes the 'static' keyword, then also will not give the correct output as the pointer p is local to the function and on the return of control from the function it gets destroyed - returning a garbage value.
#include<stdio.h>
int *check( int, int);
int main()
{
int *c;
c = check(10, 20);
printf("%d\n", c);
return 0;
}
int *check(int i, int j)
{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}
This removes the 'static' keyword, then also will not give the correct output as the pointer p is local to the function and on the return of control from the function it gets destroyed - returning a garbage value.
(2)
Praju said:
8 years ago
Can anyone please explain question with the correct program?
Sujan said:
9 years ago
What would be the answer if it wasn't static?
Sushovan said:
9 years ago
Parameters will not be static. Its not allowed.
Shivangi said:
10 years ago
If the function would have declared static than it would be correct to pass static parameters inside a function.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers