Const, static, extern and their combinations in C and C ++

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 int xyz = 123; produces error 

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; //gives error redefinition of z!!!?? void abc() { z = 33; //perfectly fine here!!?? } 

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.

+4
source share
4 answers

1)

static means do not put the object in the global symbol table. On the plus side, you can have repeatedly defined characters without problems. On the other hand, symbols are not generated for any static variables / methods, so it can make debugging difficult.

2 & 3)

In the title:

 extern const int xyz; 

In the source file that includes the header (ideally the same .cc whose name matches .h):

 const int xyz = 123; 

Thus, everyone knows about xyz, but it is defined in only one source file.

+4
source

I can declare a const 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 actually useless.

If you want a character associated with an external character, you can declare it in the header file and then define it in one of your source files. You can choose which one.

However, with a const int usually makes no sense to have an external connection. Just give it an internal connection with static const int xyz = 123; in the title.

What for C: in C ++ const by default global global bindings have an internal binding.

+3
source

(OP: you asked a few questions to get partial answers)

To answer this question

Is there a way to define const in the header file and then access it in multiple files by including the header?

You can place the declaration, including the value, of the const variable in the header file:

 extern const int xyz = 123; // note: extern 

Then put the definition in exactly one source file:

 const int xyz; // note: no value provided 

This only works in C ++, and not in C (or rather, I believe that it does not work in C, but I never tested it).

+1
source

1) static: used to declare a function or variable as local. they will only be used in the file that is defined. if they are defined in the header file, so they will be used in files that include this file

extern is used to import an external variable or function. a variable or function must be defined in another file and must not be defined static

2) instead of defining const with const int X = 5 you can use an alternative solution using macros: #define X 5

0
source

Source: https://habr.com/ru/post/1438044/


All Articles