2

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?

gyuunyuu
  • 526
  • 5
  • 17
  • Use storage-class specifier static. – Vlad from Moscow Sep 30 '19 at 11:38
  • 2
    Use `static int your_constant = value;` in your `.c` at global scope – David Ranieri Sep 30 '19 at 11:38
  • 3
    Please add a small example of what you are doing, and explain why it is not what you want. – user694733 Sep 30 '19 at 11:39
  • 1
    _I want to declare constants in a file_... What kind of file are these constants sourced in? Is this a .c file? a .h file? From what scant information you have provided, the simple answer is just place the `const` keyword in front of any variable in file global scope, i.e. outside of, and before any functions that will use them within the .c file they will be used in. E.g.` const int value = 0;` or `const struct values {...};`. The answer to _...depend on compiler and IDE?_ is no. – ryyker Sep 30 '19 at 11:45
  • One solution: put the constants in a .h file with `#define` and include that file only into the .c files that are supposed to use the constants. It's hard to give an advice here because your question is not very clear (see other comments) – Jabberwocky Sep 30 '19 at 12:02
  • 1
    It could be a .h file or .c file but should be separate from the file where the functions using these constants are defined. – gyuunyuu Sep 30 '19 at 12:26

2 Answers2

2

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)

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
1

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.

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • `static const int` in a .h means duplication of _variables_ in every source file where the .h is included. That is not very efficient. – Paul Ogilvie Sep 30 '19 at 17:52
  • Ryker, is that an "RV" registration of that plane? RV used to be Iran? Couldn't find an RY registration country prefix. Definitely aerobatics. Yours? – Paul Ogilvie Sep 30 '19 at 18:01
  • @PaulOgilvie - ***1st*** comment: That is why I specified `_only the file the variables are intended for._` in the text prior to that declaration. But I do not disagree with your comment, and have removed the `static` modifier :). ***2nd*** comment: The picture is one I snagged somewhere. Unfortunately, the plane is not mine, it is only an aspiration at this point. The _RV-8_ tail number I suspect may indicate a temporary, granted to _[Van's Aviation in Oregon](https://www.vansaircraft.com/rv-8/)_ for marketing purposes, but your guess is as good as mine. – ryyker Sep 30 '19 at 19:28