#pragma & #pragma once usage

Hi , I don't have any Ideas about what these preprocessors will do. Can anyone please explain ?


Thanks.
#pragma once does exactly the same as creating header guards, however, pragma is not supported by all compilers.
If you need a reminder of what header guards are:

Header guards prevent a file from being included in the same file more than once.
1
2
3
4
5
6
7
8
9
//file.h
#ifndef FILENAME_H
#define FILENAME_H
//code here
#endif

//main.cpp
#include "file.h" //includes the code between #ifndef en #endif
#include "file.h" //includes nothing, as FILENAME_H is already defined and the #ifndef evaluates false 


I recommend using regular header guards rather than #pragma once, because, as I already said, pragma isn't supported by all compilers.

If anything is still unclear feel free to ask me
Last edited on
Hello King Of CPP,

You may find this line useful. I use it quite often.

https://msdn.microsoft.com/en-us/library/d9x1s805.aspx

Hope that helps,

Andy
Topic archived. No new replies allowed.