473,382 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

#pragma once vs #ifndef on V.S.

464 Expert 256MB
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 15332
RedSon
5,000 Expert 4TB
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 Expert Mod 8TB
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 Expert 256MB
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 Expert Mod 8TB
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 Expert Mod 8TB
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 Expert 256MB
Thanks guys for the information.
Oct 25 '07 #7
solita
17
#pragma once is compiler dependent.#ifndef has no such problem
Dec 9 '09 #8

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

Similar topics

27
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...
6
by: Shri | last post by:
Can anybody tell me where i can find a detailed document on #pragma .... --shri
187
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...
8
by: karunesh | last post by:
why this #pragma use very extnsivly please tell me the usses thaks!! Regards, Karuensh
26
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? ...
5
by: raashid bhatt | last post by:
What is #pragma once used for and what is #WIN#@_LEN_AND_MEAN
3
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? ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.