Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's difference between #pragma and #ifndef? [duplicate]

Tags:

c

Possible Duplicate:
#pragma once vs include guards?

When should I use #pragma once?

When should I use #ifndef HEADER_H_INCLUDED?

like image 786
machchakumar Avatar asked Jul 11 '10 05:07

machchakumar


2 Answers

The difference is that the latter is C and the former is not. Never use #pragma once; always use #ifndef.

One other thing to note when using the #ifndef method is that any preprocessor symbol beginning with two underscores or an underscore followed by a capital letter is reserved and cannot be used. You should use things like #ifndef MYHEADER_H and not #ifndef _MYHEADER_H.

like image 119
R.. GitHub STOP HELPING ICE Avatar answered Sep 28 '22 09:09

R.. GitHub STOP HELPING ICE


The construct

myfoo.h

#ifndef MYFOO_H
#define MYFOO_H

/* header information for myfoo.h */

#endif

belongs in every header-file. The trick is: you can include a header file (accidentally) more than once without thinking abaout double declarations. so this is for the preprocessor.

The #pragma is for the compiler, and a preprocessor should ignore pragmas it does not understand.

like image 35
Peter Miehle Avatar answered Sep 28 '22 11:09

Peter Miehle