1) How do static, extern and const and their usage differ in C and C ++? (Default Communication and Other Differences)
2) The following declarations and definitions are allowed in the header file, which is used in C, which is then included in several files.
static int testvar = 233; extern int one; extern int show(); int abc; const int xyz;
Const construct causes compile-time errors (possibly due to several definitions). However, I can declare a constant variable in the header file, but since we can define it by providing a value, and we also cannot assign a value in the files in which this header is included, it is practically useless. Is there a way to define const in the header file and then access it in multiple files by including the header?
3) What changes (if at all) need to be made so that this header can be included in several files in C ++ ?
4) consider the following
header.h
static int z = 23;
test.c
#include"header.h" z = 33;
static vars defined / declared in the header have an internal binding in each file, so each file will have a separate copy of this variable. Then why assigning a value to this variable outside of any function gives an override error while it works perfectly inside the function?
Edit: Added 4th question. This is very confusing.
** PS: Now I'm only looking for answers to questions 1 and 4.
source share