Using define in header file c

I'm just starting to learn C, so hopefully this is not a dumb question. The problem I am facing is regarding header files and using #define for constants. I read that I should use the following to prevent my header from compiling more than once.

#ifndef NAME_OF_FILE
#define NAME_OF_FILE
.
. //my header file content
.
#endif

I want to add a constant, and I believe that I would also use #define, for example

#ifndef NAME_OF_FILE
#define NAME_OF_FILE

#define NUM_CONST 5 //the constant I want in my header file
.
.
.
#endif

How does C know that #define NAME_OF_FILE refers to a .h file, and #define NUM_CONST 5 is just a constant? Is it because of the value at the end of NUM_CONST? Or am I all of this completely wrong?

+4
source share
4 answers

. #define NAME_OF_FILE #define NUM_CONST 5 - . ( ).

, :

printf("%d\n", NAME_OF_FILE NUM_CONST NAME_OF_FILE);

:

printf("%d\n", 5);

. "". , , " ".

NAME_OF_FILE .h, , ifndef .

+4

. NAME_OF_FILE .

#ifndef PRETTY_PINK_PRINCESS
#define PRETTY_PINK_PRINCESS
/* definitions */
#endif

. NAME_OF_FILE - , .

+4

#define IDENTIFIER #ifdef, , , .

,

#define NAME_OF_FILE

int main()
{
    int x = NAME_OF_FILE;
}

int main()
{
    int x = ;
}

, "", , .

( #define NAME_OF_FILE NAME_OF_FILE, , , .)

+3

#define NAME_OF_FILE

#define NUM_CONST 5

#ifndef NAME_OF_FILE
#define NAME_OF_FILE
.
. //my header file content
.
#endif

, , "NAME_OF_FILE" , . , , , , , . , "c-" , marco __TEST_H, , , "test.h". , , , , IDE , , IDE . , , ( , ), , .

0
source

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


All Articles