Define Constant Variables in C++ Header

shared c constants in a header

In some .c file, write what you've written.
In the appropriate .h file, write

extern const char* QUERY; //just declaration

Include the .h file wherever you need the constant

No other good way :)
HTH

Define constant variables in C++ header

You could simply define a series of const ints in a header file:

// Constants.h
#if !defined(MYLIB_CONSTANTS_H)
#define MYLIB_CONSTANTS_H 1

const int a = 100;
const int b = 0x7f;

#endif

This works because in C++ a name at namespace scope (including the global namespace) that is explicitly declared const and not explicitly declared extern has internal linkage, so these variables would not cause duplicate symbols when you link together translation units. Alternatively you could explicitly declare the constants as static.

static const int a = 100;
static const int b = 0x7f;

This is more compatible with C and more readable for people that may not be familiar with C++ linkage rules.

If all the constants are ints then another method you could use is to declare the identifiers as enums.

enum mylib_constants {
a = 100;
b = 0x7f;
};

All of these methods use only a header and allow the declared names to be used as compile time constants. Using extern const int and a separate implementation file prevents the names from being used as compile time constants.


Note that the rule that makes certain constants implicitly internal linkage does apply to pointers, exactly like constants of other types. The tricky thing though is that marking a pointer as const requires syntax a little different that most people use to make variables of other types const. You need to do:

int * const ptr;

to make a constant pointer, so that the rule will apply to it.

Also note that this is one reason I prefer to consistently put const after the type: int const instead of const int. I also put the * next to the variable: i.e. int *ptr; instead of int* ptr; (compare also this discussion).

I like to do these sorts of things because they reflect the general case of how C++ really works. The alternatives (const int, int* p) are just special cased to make some simple things more readable. The problem is that when you step out of those simple cases, the special cased alternatives become actively misleading.

So although the earlier examples show the common usage of const, I would actually recommend people write them like this:

int const a = 100;
int const b = 0x7f;

and

static int const a = 100;
static int const b = 0x7f;

how to define constant variables in header files in C language

You should extern you variable.

.h file:

#ifndef HDR_H
#define HDR_H

typedef struct
{
int kind; /* it has a constant value 0x01*/
} tcp_opt_nop;

extern const tcp_opt_nop opt_nop;

#endif

.c file:

#include "hdr.h"

const tcp_opt_nop opt_nop = {0x01};

main file:

#include "hdr.h"

int main()
{
printf("%i\n", opt_nop.kind);
// ...
}

How to declare constant parameters in separate class or header file in C++?

Why do you feel that you need a class for this? That doesn't seem appropriate. FORTRAN comparisons are also not likely to bear much fruit as C++ is a different language with different idioms and concepts.

To me, it seems like you should simply put those constants in a header file. Be sure to make them static const constexpr to avoid linker clashes.

// Constants.hpp
#ifndef MYLIB_CONSTANTS_HPP
#define MYLIB_CONSTANTS_HPP
#pragma once

static constexpr const int Number_0 = 234;
static constexpr const float Number_1 = 34.76;
static constexpr const double Number_2 = 98.78;

#endif

Now you just #include "Constants.hpp" in any translation unit that requires access to these values.

For the record, the old-school C approach to do the same thing would be to use #defines.

Don't forget to give these constants meaningful names.

C literal constants : in header or C file?

Header files in C are nothing special; the .h extension won't change how the compiler handles them. It's more of a hint for humans "this file probably doesn't contain any code".

So if you put actual binary data in there, the compiler will create a copy of the array in each file in which you include the header (instead of simply adding a reference to a shared global array).

GIMP creates a header file because it doesn't know how you plan to use the data. The idea is that you'll include this header file exactly once in a .c file which then processes the data in some way. If it wrote a .c file and you made changes to the code, GIMP would have to merge the changes when you ask it to update the data - it would be messy.

Static const variable declaration in a header file

I answered this at length here. That answer is for C++, but it holds true for C as well.

The translation unit is the individual source file. Each translation unit including your header will "see" a static const int. The static, in this context, means the scope of my_variable is limited to the translation unit. So you end up with a separate my_variable for each translation unit (".c file").

The compiler would not be "smart" to create only one instance for all files, it would be faulty, because you explicitly told it not to do so (static).

Declare and initialize constant in header file

In C++, const objects have internal linkage unless explicitly declared extern, so there is no problem with putting a definition into a header file such as:

const int myInt = 55;

With this definition and first declaration, myInt can be used as an integer constant expression such as for array bounds and the like.

I can't answer for Objective C.

Where to define class constants in C++?

This depends on your actual usage but consider using proper strong types instead of basic types. For instance, to declare sizes, make them of type size, not float. This doesn’t directly solve your grouping problem but it gives you all of the other benefits of strong typing, and may also help with grouping:

struct size {
private: float value;
public:
explicit constexpr size(float value) : value{value} {}
explicit constexpr operator float() const noexcept { return value; }
};

namespace default_sizes {
constexpr auto SMALL = size{1.5f};
constexpr auto MEDIUM = size{2.5f};
// …
}

In fact, for domain-specific usage, except in very limited cases, I’d generally avoid bare basic types and always encapsulate them in their own domain-specific type.

Multiple definition of const variables at header file

The #include directive in C simply copies the text from the header file. That means that when you compile both link.c and linkedlist.c, the constant definitions from flag.h gets copied into both, and all these constants are now defined in both link.o and linkedlist.o`. When you link the program you get a name you get a multiple definition error.

You need to split the declaration from the definition, and create a flag.c file where you define const int OK = 1; etc., and at flag.h you'll stay with const int OK; etc. That way, the constants will be compiled into a single object file - flag.o - and you won't get a multiple definition error when you link.



Related Topics



Leave a reply



Submit