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.
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)
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)
Sourav said:
10 years ago
What will be the output if we remove static from the function argument in the above mentioned program. It is showing -347380. I mean how?
Ashi said:
1 decade ago
Can anyone tell me what is the reason why we can not pass static variables in a function?
Anonymous said:
1 decade ago
Static variables can not be declared inside method. These are always declared inside the main method.
Damodharam said:
1 decade ago
@ M@C:
The static variable is initialized to "1" as global variable and in local block variable is initialized to "10". Since both are same variables when the local block finishes and comes to the function call it will take value of global variable and it is printed as output.
The static variable is initialized to "1" as global variable and in local block variable is initialized to "10". Since both are same variables when the local block finishes and comes to the function call it will take value of global variable and it is printed as output.
Rasu said:
1 decade ago
I am using visual c++, in that I can able to pass static variable as the arguments, it is showing only warning.
Sourav pal said:
1 decade ago
Because static variable can not be change, It is initialized at one time. So answer is 1.
Kushal said:
1 decade ago
Cannot use static in function argument.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers