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);
}
10
20
Error: Non portable pointer conversion
Error: cannot use static for function parameters
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
50 comments Page 2 of 5.

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'.

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?

Sujan said:   1 decade ago
return 0. Can any one explain?

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.

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

Abiramidevaraj said:   1 decade ago
I can't understand why it is printing 1 instead 11.

Abi said:   1 decade ago
Couldn't understand. Please someone explain clearly. Whats static? why it shouldn't be used?

Ravi said:   1 decade ago
Since when a function is called the arguments are pushed onto the stack for a particular function and according to the c protocols a static variables should be stored in the data sections.

Kushal said:   1 decade ago
Cannot use static in function argument.

Sourav pal said:   1 decade ago
Because static variable can not be change, It is initialized at one time. So answer is 1.


Post your comments here:

Your comments will be displayed after verification.