Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define char in header

I have multiple C files 1.c 2.c and 3.c and their correspondent headers 1.h 2.h 3.h. these files use the same static char* variable so I want to define this variable in one header file. Is there any solution?

like : #define nameVariable valueVariable

NB :

  • None of the c files include another header (i.e 1.c dont include 2.h and 3.h etc..).

  • All of the 3 files include a 4.h file.

  • All of the 3 files have the same Makefile.

like image 363
W.draoui Avatar asked Jun 30 '17 18:06

W.draoui


People also ask

How do you declare a constant character?

const char *YourClass::SOMETHING = "something"; C++ standard, 9.4. 2/4: If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression.

What is strings h in c?

h is the header in the C standard library for the C programming language which contains macro definitions, constants and declarations of functions and types used not only for string handling but also various memory handling functions; the name is thus something of a misnomer.

How do you declare a character in C?

In order to declare a variable with character type, you use the char keyword followed by the variable name. The following example declares three char variables. char ch; char key, flag;.


1 Answers

If the variable in question is a constant string that will never change, you can get away with using a #define for this.

In 4.h:

#define MY_STATIC_STRING "my_string"

This will perform a text substitution in each source file anyplace you use MY_STATIC_STRING.

like image 200
dbush Avatar answered Sep 24 '22 10:09

dbush