Predict the output of following C programs.
Both of the above programs don’t compile in C. We get the following compiler error in C.
error: initializer element is not constant
In C, static and global variables are initialized by the compiler itself. Therefore, they must be initialized with a constant value.
Note that the above programs compile and run fine in C++, and produce the output as 10.
As an exercise, predict the output of following program in both C and C++.
This article is contributed by Shankar Shastri. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Internal Linkage and External Linkage in C
- What is size_t data type in C language?
- Different ways to declare variable as constant in C and C++
- Why variable name does not start with numbers in C ?
- Redeclaration of global variable in C
- Storage Classes in C
- Static Variables in C
- Data Types in C
- Variables and Keywords in C
- Write a C program that displays contents of a given file like 'more' utility in Linux
- Integer Promotions in C
- Use of bool in C
- Complicated declarations in C
- How are variables scoped in C - Static or Dynamic?
- Understanding "extern" keyword in C