mainajaved
mainajaved

Reputation: 8173

Include Guards syntax in C

Hello everyone I want to ask a question about including guards in C programming. I know their purpose but in some programms I have seen a 1" written after #define like this:

#ifndef MYFILE_H
#define MYFILE_H 1

What is the purpose of this 1? Is it necessary?

Upvotes: 1

Views: 542

Answers (3)

ouah
ouah

Reputation: 145919

It is not necessary if the MYFILE_H macro is not used elsewhere in your code.

If it is used elsewhere with an #ifdef or #ifndef directive like here:

#ifdef MYFILE_H 

then the 1 is not required in the macro definition-

but it if it used elsewhere with an #if directive like here:

#if MYFILE_H

then the 1 (or any value != 0) is required in the macro definition.

Note these directives could be used in a source file to verify if the header is included or not.

Upvotes: 2

Woodrow Douglass
Woodrow Douglass

Reputation: 2655

It's a style thing, as far as i know. That '1' is unnecessary in my opinion; it doesn't really do anything.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182774

It's not necessary, #define MYFILE_H should do the trick. The fact that MYFILE_H is defined (the condition tested by ifndef) is separated from its value. It could be 0, ' ', 42, etc.

Upvotes: 6

Related Questions