932 views
0 0 votes
#include<stdio.h>
int a = 10;
int main()
{
fun();
fun();
return 0;
}
int fun()
{
static int a = 1;
printf("%d ",a);
a++;
return 0;
}

1 Answer

Best answer
4 4 votes

@Lord_Krishna Indeed output is 1 2.

Because in

1) presence of local variable with the same name, function will never access the global variable

2) Static variable declaration will takes place only once. So it can retain its value in different function calls. 

selected by
Position:
Show:

Related questions

8 8 votes
1 answers 1 answer
1.9k
1.9k views
Desert_Warrior asked May 16, 2016
1,911 views
#include <stdio.h void f(char ); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char p) { char *t; t = (p += sizeof(int)...
0 0 votes
1 answers 1 answer
959
959 views
Desert_Warrior asked May 16, 2016
959 views
#include<stdio.h #include<stdlib.h int main() { char s[] = "Opendays2012"; int i = 0; while(*(s++)) i++; printf("%d",i); return 0; }(a) Segmentation Fault (b) Compile Err...
1 1 vote
1 answers 1 answer
2.9k
2.9k views
Desert_Warrior asked May 16, 2016
2,859 views
#include<stdio.h void foo(char *); int main() { char *string = "Hello"; foo(string); printf("%s",string); return 0; } void foo(char *a) { while(*a) { *a += 1; a++; } }(a)...
0 0 votes
1 1 answer
733
733 views
Desert_Warrior asked May 16, 2016
733 views
#include <stdio.h int main(void) { char a[5] = { 1, 2, 3, 4, 5 }; char *ptr = (char*)(&a + 1); printf("%d %d\n", *(a + 1), *(ptr - 1)); return 0; }options are : (a) Compi...