ahmedwahdan
ahmedwahdan

Reputation: 79

How does preprocessor make that expansion (define macro found in included header file )

I have a header file that contain a constant F_CPU and I use macro guard

header.h

#ifndef F_CPU
#define F_CPU  1000000UL
#endif

and the source.c file

#define F_CPU 16000000UL
#include "header.h"

how the first macro(in c file ) expand the value that not included yet?

Upvotes: 0

Views: 161

Answers (2)

fatalerror
fatalerror

Reputation: 41

Use the -E option of the preprocessor to follow what happens. You define the F_CPU macro in the first line of source.c and after that when the header file is included, the macro definition in the header does not takes place because of the #ifndef guard. Note that there is no macro expansion in your code. Expansion takes place when you use your macro.

Upvotes: 4

Wojtek Surowka
Wojtek Surowka

Reputation: 21003

The first #define in the .c file is the macro definition, perfectly legal in this place. Macro definitions are not limited to included files - they may appear in .c files as well.

Upvotes: 0

Related Questions