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

In the following control structure which is faster? 1.Switch 2.If-else and which consumes more memory?

4 Answers  


main() { struct s1 { char *str; struct s1 *ptr; }; static struct s1 arr[] = { {"Hyderabad",arr+1}, {"Bangalore",arr+2}, {"Delhi",arr} }; struct s1 *p[3]; int i; < BR> for(i=0;i<=2;i++) p[i] = arr[i].ptr; printf("%s ",(*p)->str); printf("%s ",(++*p)->str); printf("%s ",((*p)++)->str); }

0 Answers   Wilco,


what is the output of the following code? main() { int I; I=0x10+010+10; printf("x=%x",I); } give detailed reason

3 Answers  


what is the difference between exit() and _exit() functions?

2 Answers  


Write a program to find given number is even or odd without using any control statement.

2 Answers  






What are compound statements?

0 Answers  


A routine usually part of the operation system that loads a program into memory prior to execution a) linker b) loader c) preprocessor d) compiler

0 Answers  


Find if a number is power of two or not?

1 Answers  


increment operateor (++)and decrament(--) #include<stdio.h> #inclide<conio.h> main() { int x=15; while(x!=0) scanf("%d",&x); {

2 Answers   HCL, Syntel, TCS,


When can you use a pointer with a function?

0 Answers  


Write a program on swapping (100, 50)

0 Answers   BPL,


How we can insert comments in a c program?

0 Answers  


Categories