I want to declare constants in a file and then use them in function calls from specific source files. These constants must not be visible to all files. How can this be achieved in C?
Does the solution to this problem depend on compiler and IDE?
I want to declare constants in a file and then use them in function calls from specific source files. These constants must not be visible to all files. How can this be achieved in C?
Does the solution to this problem depend on compiler and IDE?
I want to declare constants in a file and then use them in function calls from specific source files. These constants must not be visible to all files.
The normal way of doing this is to define the constants in a .h file and then only include that .h file in the source files that need to know about these constants. For example:
// constants.h
#define CONSTVAL1 1
#define CONSTVAL2 2
#define CONSTVAL3 3
#define CONSTVAL4 "a string"
and a source file:
// mysource.c
#include "constants.h"
Now you can call the fuction like
myfunc(CONSTVAL1);
(I will not show an advanced example that uses include guards. Search for "include guards" on google or stack exchange)
EDIT to address answer in comments under OP: It could be a .h file or .c file but should be separate from the file where the functions using these constants are defined.
The answer then is made simple. In C the common way to provide constant variables to a dedicated .c source file from another file is to use a header ( .h
) file that defines a constant value, and is included ( #include
) into only the file the variables are intended for. Example
example.h
// examples of constant values for use in a header file in C
//using macros
#define intVal 10
#define floatVal 10.503
#define strVal "string value"
//using integral types:
const int = 10;
const double = 10.503;
const char[] = {"string value"};
(original anser content)
In general limiting scope (visibility and duration) of variables in C is straight forward. There are many tutorials on the topic, such as this one: (the following excerpts are just a small part of the content.)
...learn about the scope and storage classes of data in C. The main topics covered in this lesson are
Block scope Function scope File scope Program scope The auto specifier The static specifier The register specifier The extern specifier The const modifier The volatile modifier
What you are describing suggests File Scope would work for you. An excerpt from the same link:
File Scope and the Hierarchy of Scopes
...
Most large programs consist of several program files.
The following portion of source code shows variables with file scope:
int x = 0; /* program scope */ static int y = 0; /* file scope */ static float z = 0.0; /* file scope */ int main() { int i; /* block scope */ . . . return 0; }
To make the above file scope variables constants use the const
modifier:
static const int y = 0; /* file scope constant */
static const float z = 0.0; /* file scope constant */
Another method commonly used is C macros. See this discussion.