What is the output of the below code?
#include <stdio.h>
int var;
void main()
{
void func(void);
printf("%d\n",var);
func();
}
int var = 100;
void func()
{
printf("%d\n",var);
}
----------------------------------------------------
The above code tests two scenarios:
1. Can a global variable have a duplicate declaration?
2. Can we assign different values to duplicate global variables?
3. Can we have a function declaration within a function?
-----------------------------------------------------
- In C programming, duplicate declaration of the global variable is allowed. However, both the places variable cannot be initialized. Initialization can be done only once.
- Local variable cannot have a duplicate declaration.
- There can be a declaration at any place in the program. However, user has to understand the scope of the usage and declare it.
In our program, var is a global variable and is declared twice and initialization is done only once.
void func(void); is a function declaration and is done within a main() function and is valid.
func() calls it's definition from main() function and prints the value of var which is 100.
The output of the program is 100,100.
0 Comments