473,795 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#pragma once vs #ifndef on V.S.

464 Recognized Expert Contributor
Okay I have been wondering this for a while now. I am a little confused on the difference between these two defines #pragma once & #ifndef.

From my understanding #pragma is compiler dependent, meaning that it will be handled differently depending on the compiler? #ifndef is not dependent on the compiler.

So from that i take it that its more universal to use #ifndef because its the standard, but if I use #pragma once in visual studios and then create the .exe for the program does it matter whether #ifndef or #pragma once was used?

I guess this could extend into what happens when i build my .exe how are the different files defined. The complier takes the code and translates it into an intermediate language which is easier for the computer to understand right?

Hopefully this question didn't get to mucked up and now i feel like i am rambling.
Oct 24 '07 #1
7 15355
RedSon
5,000 Recognized Expert Expert
Okay I have been wondering this for a while now. I am a little confused on the difference between these two defines #pragma once & #ifndef.

From my understanding #pragma is compiler dependent, meaning that it will be handled differently depending on the compiler? #ifndef is not dependent on the compiler.

So from that i take it that its more universal to use #ifndef because its the standard, but if I use #pragma once in visual studios and then create the .exe for the program does it matter whether #ifndef or #pragma once was used?

I guess this could extend into what happens when i build my .exe how are the different files defined. The complier takes the code and translates it into an intermediate language which is easier for the computer to understand right?

Hopefully this question didn't get to mucked up and now i feel like i am rambling.
#pragma supports many different directives. #ifndef only detects if something has been previously defined.

http://msdn2.microsoft.com/en-us/lib...05(vs.71).aspx
Oct 24 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
The #ifndef/#define/#endif is the ANSI standard way of constructing an inclusion guard.

#pragma once is a Microsoft whiz-bang that does the same thing but is not portable. There is no #pragma in ANSI C or C++.

Wherever possible use the ANSI proprocessor directives.
Oct 25 '07 #3
Studlyami
464 Recognized Expert Contributor
Now after the program is built and i send out the .exe it doesn't matter what was used #pragma once or the #ifndef, #define right? So it would only matter if i sent out my source code and someone tried to built it on a different compiler?
Oct 25 '07 #4
Banfa
9,065 Recognized Expert Moderator Expert
There is no #pragma in ANSI C or C++.
Hmmm, I thought that the #pragma directive was defined in both ANSI C and C++ with the following conditions.

1. The ANSI C and C++ standards do not define any syntax following the #pragma directive since they are compiler dependent commands.

2. The ANSI C and C++ standards does specify that compilers should ignore unrecognised commands (or at least not produce an error halting compilation).


Apart from that I agree, there is little point using the MS specific way when you can use a method supported by everyone.

Even in Visual Studio generated projects the standard method is used in headers. If an MS compiler is detected then it additionally uses the "#pragma once" directive.

In a very large project the advantage of using #pragma once is that the header never even gets opened again during a compile, this can speed up the compile time of the project.
Oct 25 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
I stand corrected. I looked in K&R The C Programming Language 1978 and there is no #pragma.

Based on your comment, I looked in K&R The ANSI C Progammiong Language 1988 and there it is.

I really need to throw that old K&R away.
Oct 25 '07 #6
Studlyami
464 Recognized Expert Contributor
Thanks guys for the information.
Oct 25 '07 #7
solita
17 New Member
#pragma once is compiler dependent.#ifnd ef has no such problem
Dec 9 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

27
8591
by: Jacob Jensen | last post by:
Hi I have a problem creating C++ code that is multiplatform compilable. My problem is using the "#pragma once" directive which can be used by microsoft Visual Studio pre-compiler but which gives warnings when used by gcc for example. I would like to use it when compiling with Visual C++ compiler, but how can I create a smart macro or something, so that it is truly ignored when compiler with gcc. I have tried the following which actually...
6
3968
by: Shri | last post by:
Can anybody tell me where i can find a detailed document on #pragma .... --shri
187
6558
by: Lasse Espeholt | last post by:
Hi... I am relativ new to the impressive and powerfull C language, but i thinks it is obsolete... The idea with header/source files where methods can clash into eachother i don't like... Look at C# which is much cleaner with namespaces. Why has C not namespaces and a "area idea" where some methods and
8
2240
by: karunesh | last post by:
why this #pragma use very extnsivly please tell me the usses thaks!! Regards, Karuensh
26
3830
by: Rick | last post by:
I'm told that "#pragma once" has made it into the ISO standard for either C or C++. I can't find any reference to that anywhere. If it's true, do any of you have a reference I can use? Thanks...
5
25226
by: raashid bhatt | last post by:
What is #pragma once used for and what is #WIN#@_LEN_AND_MEAN
3
3902
by: Subrat | last post by:
Hello, I have used #pragma once in a header file. During compilation I get a warning : #pragma once is obsolete. How do I switch off this warning, without making any change to my make file? Thanks in Advance, Subrat
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.