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.
Renjith said:
1 decade ago
@Abiramidevaraj and @M@C:
Because,
int static a=1; is a global variable and
static int a=10; is a local one
The function call() definition is outside the main block so it only gets the global variable a.
This will give more details
#include<stdio.h>
#include<conio.h>
int static a=1;
int main()
{
static int a=10;
a++;
void call()
{
printf("Weclome to IndiaBIX.com..! %d\n",a);
}
call();
pr();
getch();
}
pr()
{
printf("Weclome to IndiaBIX.com..! %d",a);
}
Weclome to IndiaBIX.com..! 11
Weclome to IndiaBIX.com..! 1
Because,
int static a=1; is a global variable and
static int a=10; is a local one
The function call() definition is outside the main block so it only gets the global variable a.
This will give more details
#include<stdio.h>
#include<conio.h>
int static a=1;
int main()
{
static int a=10;
a++;
void call()
{
printf("Weclome to IndiaBIX.com..! %d\n",a);
}
call();
pr();
getch();
}
pr()
{
printf("Weclome to IndiaBIX.com..! %d",a);
}
Weclome to IndiaBIX.com..! 11
Weclome to IndiaBIX.com..! 1
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)
Manish said:
1 decade ago
While executing getting following error.
p15.c:2: error: storage class specified for parameter 'type name'
p15.c:2: error: storage class specified for parameter 'type name'
p15.c: In function 'main':
p15.c:8: warning: format '%d' expects type 'int', but argument 2 has type 'int *'
p15.c: At top level:
p15.c:11: error: storage class specified for parameter 'i'
p15.c:11: error: storage class specified for parameter 'j'
p15.c:2: error: storage class specified for parameter 'type name'
p15.c:2: error: storage class specified for parameter 'type name'
p15.c: In function 'main':
p15.c:8: warning: format '%d' expects type 'int', but argument 2 has type 'int *'
p15.c: At top level:
p15.c:11: error: storage class specified for parameter 'i'
p15.c:11: error: storage class specified for parameter 'j'
Venky498 said:
1 decade ago
Hi friends,
According to ANSI C standards we can not pass the variables which are declared as storage class type to function as aruguments except register storage class variables but in the above program even register is also not works...why bcoz at the LINE:14 we are trying to access the address of register.
According to ANSI C standards we can not pass the variables which are declared as storage class type to function as aruguments except register storage class variables but in the above program even register is also not works...why bcoz at the LINE:14 we are trying to access the address of register.
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.
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)
J'mishra said:
1 decade ago
#include<stdio.h>
#include<conio.h>
void call();
int static a=1;
int main()
{
static int a=10;
a++;
call();
//return 0;
getch ();
}
void call()
{
static int a=4;
printf("Weclome to IndiaBIX.com..! %d",a);
}
Here local variable is printed an o/p is 4 instead of 1 or 11.
#include<conio.h>
void call();
int static a=1;
int main()
{
static int a=10;
a++;
call();
//return 0;
getch ();
}
void call()
{
static int a=4;
printf("Weclome to IndiaBIX.com..! %d",a);
}
Here local variable is printed an o/p is 4 instead of 1 or 11.
Apurva Nigam said:
1 decade ago
@Mani:
Here the function is NON STATIC. so does it mean it cant access static variables???
What if both(ie parameters and function) were static??
Then the code would be correct??
Or
@Nikhil:
u mean Parameters can never be static whether its a STATIC or NON STATIC function???
Here the function is NON STATIC. so does it mean it cant access static variables???
What if both(ie parameters and function) were static??
Then the code would be correct??
Or
@Nikhil:
u mean Parameters can never be static whether its a STATIC or NON STATIC function???
Yash said:
8 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.
Bhu said:
10 years ago
Line 4: error: storage class specified for parameter 'type name'.
Line 4: error: storage class specified for parameter 'type name'.
Line 13: error: storage class specified for parameter 'i'.
Line 13: error: storage class specified for parameter 'j'.
Line 4: error: storage class specified for parameter 'type name'.
Line 13: error: storage class specified for parameter 'i'.
Line 13: error: storage class specified for parameter 'j'.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers