Search code examples
cmacrosincludec-preprocessor

Using Definitions in C Header Files?


I'm currently learning the C programming language (coming from Java) and I'm a bit confused as to how to define a macro.

In order for other code to use the macro, the header file must have it. But if I define the macro in the header file, then the source file can't use it. Do I have to define it in both or do I have to #include the source file's own header file?


Solution

  • Source files virtually always include their "own" header file -- i.e., a header that declares the functions defined in a source file. Declaring a function before actually defining it is perfectly legal and often desirable: you may get compile errors if the header is accidentally mismatched, and that's a good thing.