in Education by
I have a C project where all code is organized in *.c/*.h file pairs, and I need to define a constant value in one file, which will be however also be used in other files. How should I declare and define this value? Should it be as static const ... in the *.h file? As extern const ... in the *.h file and defined in the *.c file? In what way does it matter if the value is not a primitive datatype (int, double, etc), but a char * or a struct? (Though in my case it is a double.) Defining stuff inside *.h files doesn't seem like a good idea generally; one should declare things in the *.h file, but define them in the *.c file. However, the extern const ... approach seems inefficient, as the compiler wouldn't be able to inline the value, it instead having to be accessed via its address all the time. I guess the essence of this question is: Should one define static const ... values in *.h files in C, in order to use them in more that one place? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The rule I follow is to only declare things in H files and define them in C files. You can declare and define in a single C file, assuming it will only be used in that file. By declaration, I mean notify the compiler of its existence but don't allocate space for it. This includes #define, typedef, extern int x, and so on. Definitions assign values to declarations and allocate space for them, such as int x and const int x. This includes function definitions; including these in header files frequently lead to wasted code space. I've seen too many junior programmers get confused when they put const int x = 7; in a header file and then wonder why they get a link error for x being defined more than once. I think at a bare minimum, you would need static const int x so as to avoid this problem. I wouldn't be too worried about the speed of the code. The main issue with computers (in terms of speed and cost) long ago shifted from execution speed to ease of development.

Related questions

0 votes
    I have a C project where all code is organized in *.c/*.h file pairs, and I need to define ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    What is the meaning of the below declarations? 1. const int a; 2. int const a; 3. const int *a; 4. int * const a; 5. int const * a const;c...
asked Jan 21, 2021 by JackTerrance
0 votes
    I'm getting the following error when trying to compile my code on Angular 7.2.0 with TypeScript version ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I'm getting the following error when trying to compile my code on Angular 7.2.0 with TypeScript version ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I'm relatively new to C++, recently moved from C# and Java (and before that, used to work in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Take the following code snippet: #include std::vector good; //illegal, because std::allocator is ill-formed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Is the following code supposed to compile? #include void foo() { const std::pair x = {1, 2}; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Is the following code supposed to compile? #include void foo() { const std::pair x = {1, 2}; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    What is the difference between const and macro?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    What is the difference between const and readonly in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    Which of these is an correct way of defining generic class? (a) class name(T1, T2, , Tn) { /* ... Interfaces & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these is an correct way of defining generic method? (a) name(T1, T2, , Tn) { /* */ } ... topic in portion Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which is the alternative way of defining margins? (a) Mar (b) Par (c) Char (d) Nar This question ... Neural Networks of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    You can restrict the entry of data into a field by defining a for it a calculated values b volatile rule c. validation rule Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    What is the process of defining more than one method in a class differentiated by method signature? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
...