Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,200 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,316 people online right now. Registration is fast and FREE... Join Now!




#pragma

 
Reply to this topicStart new topic

#pragma

Suryakant
7 Jul, 2008 - 04:49 AM
Post #1

New D.I.C Head
*

Joined: 2 Jun, 2007
Posts: 7


My Contributions
what is
#if
#endif
#elseif ????
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: #pragma
7 Jul, 2008 - 04:56 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Preprocessor directives. Basically they are commands which we give to the compiler.

The most typical uses are:
#pragma once tells the compiler to only include a specific file once
#ifndef MYHEADER_H_INCLUDED if the file has not been defined, then we need to do something
#define MYHEADER_H_INCLUDED will define the header class
#endif ends the if statement
The most common way to define a header file is by using ifndef like so:
cpp
#ifndef MYHEADER_H_INCLUDED
#define MYHEADER_H_INCLUDED
// declarations go here, eg:
class CmyClass
{
private:
int a;
public:
CmyClass();
~CmyClass();
};
#endif

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: #pragma
7 Jul, 2008 - 11:18 AM
Post #3

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
Here is a quick run down/tutorial (very good) on some other preprocessor directives:
http://www.cplusplus.com/doc/tutorial/preprocessor.html
User is offlineProfile CardPM
+Quote Post

polymath
RE: #pragma
7 Jul, 2008 - 04:45 PM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 415



Thanked: 4 times
Dream Kudos: 500
My Contributions
Anything that starts with a # is a preprocessor directive. Basically, it's a very easy way to include code and define variables. Though they don't make it into the machine code, the compiler carries them out and changes the file before use. For example, if you use #define PI 3.14, then PI would not be treated as an identifier by the compiler, but each instance of PI would be replaced by 3.14

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: #pragma
7 Jul, 2008 - 05:52 PM
Post #5

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,463



Thanked: 10 times
Dream Kudos: 325
My Contributions
The preprocessor runs through your code before you compile and replaces preprocessor macros (things that start with #) with code.

For example, the snippet below would only insert the hypothetical debug code if DEBUG is defined (which you can do with #define DEBUG or by supplying -DDEBUG as a command line argument to the compiler (supported on GCC, not sure about how many others).
cpp

#ifdef DEBUG

// Put some debug code here.

#endif


It's basically an easy way to toggle different parts of your code on/off, and to insert values and bits of code without having to manually write them in.

More on it here. smile.gif
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:23PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month