what is the diff b/w static and non static variables in C.
Give some examples plz.

Answers were Sorted based on User's Feedback



what is the diff b/w static and non static variables in C. Give some examples plz...

Answer / anil kumar

Static variables are used for internal contextual
communication purpose.
non static variables are not used for contextual
communication
for that please go through the below code:

static int i=10;
int main()
{
int x=20;
Printf(“%d %d”,x, i);
Fun();
return 0;
}
Void Fun()
{
Printf(“%d”, i);
}

In the above code “i” is the static variable and “x “is the
local variable

Is This Answer Correct ?    22 Yes 7 No

what is the diff b/w static and non static variables in C. Give some examples plz...

Answer / parth ujenia

main()
{
int i=5;

while(i!=0)
{
printf("%d",i--);
main();
}
getch();
}

output: 54321

Is This Answer Correct ?    7 Yes 1 No

what is the diff b/w static and non static variables in C. Give some examples plz...

Answer / vignesh1988i

when we have declared a variable as static...... we cant
initilize it again....... the meaning of static storage
class is for only one time initilization.... whenever the
compailer come accross the same static keyword ,... the
present value in that variable will get printed as the
compailer ignores the line static.........
eg:::::
#include<stdio.h>
#include<conio.h>
void main()
{
for(int i=0;i<3;i++)
{
static int p=1;
printf("%d ",p);
p++;
}
output will be:: 1 2 3
since the compailer ignores the static int p=1 after it
initilizs once...... and one more thing.. when we refer
variable p after the loop structure it will give an error
that::: "UNDEFINED SYMBOL 'P'" ,because the scope of this
static is only under the block and not ourtside.....

non static :: it is called as automatic storage class.... in
programs we would have given as;;
int a; or char sd; etc...
these inside the compailer treated as automatic storage
class..... the scope of this storage class is only undere
the block... after comming out it dies......

eg:::::::
#include<stdio.h>
#include<conio.h>
void main()
{
int a=12;
{
a=90;
printf("%d",a);
}
printf("%d",a);
}
the output will be::::::: 90 12.... because of the above
mentioned scope.....

i think you can clearely understand the concept....... if
you didnt understand ... send mail to me.. we can
discuss.... softvig_88@yahoo.com...
thank you

Is This Answer Correct ?    6 Yes 4 No

Post New Answer

More C Interview Questions

write a program in 'c' to find the value of p[i+1]^n.p,i,n are arguments of a macro and n is a integer

1 Answers  


What are type modifiers in c?

0 Answers  


Why pointers are used?

0 Answers  


Result of the following program is main() { int i=0; for(i=0;i<20;i++) { switch(i) case 0:i+=5; case 1:i+=2; case 5:i+=5; default i+=4; break;} printf("%d,",i); } } a)0,5,9,13,17 b)5,9,13,17 c)12,17,22 d)16,21 e)syntax error

8 Answers   IBM,


change to postfix a/(b+c*d-e)

8 Answers   Value Labs,






what is linkage error when it occurs in c program

3 Answers  


Explain how many levels deep can include files be nested?

0 Answers  


How can I invoke another program from within a C program?

8 Answers  


HOW DO YOU HANDLE EXCEPTIONS IN C?

2 Answers   AppLabs,


What are the difference between a free-standing and a hosted environment?

0 Answers   Infogain,


How can I access memory located at a certain address?

3 Answers   Verizon,


p*=(++q)++*--p when p=q=1 while(q<=6)

0 Answers   KINPOE,


Categories