Thread: Conditional Compilation...

  1. #1
    Registered User
    Join Date
    Jul 2012

    Question Conditional Compilation...

    hey everyone,
    i was in the cpreprocessor tutorial in the beginner C++ tutorial section.
    i understood every thing in the lesson (#include, constants and macros).
    but i couldn't understand a thing about the conditional compilation...
    i understood what the #if #endif #elif #else #ifndef and #ifdef mean..
    but i didn't understand how to put them to use...

    all i wanna know is what is the use of such statements and how to use them..

    PS. Explain in simple terms please, since i do not know much about c++ but i'm well-versed with all the stuff taught in the lessons in the beginner series before the c preprocessor tut.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    An example
    #include <iostream>
    using namespace std;
    int main ( ) {
        int i, sum = 0;
        for ( i = 1 ; i <= 10 ; i++ ) {
            sum += i;
    #ifdef DEBUG
            cout << "Debug: i=" << i << " sum=" << sum << endl;
        cout << "Sum=" << sum << endl;
        return 0;
    To run the code normally, you would do
    gcc -o prog prog.cpp
    and just see the final sum.

    But if it wasn't working properly, you could enable all the debugging
    gcc -DDEBUG -o prog prog.cpp

    Now you would see all the iterations of the loop as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional compilation and the preprocessor
    By tempster09 in forum C Programming
    Replies: 2
    Last Post: 04-30-2010, 04:27 PM
  2. Conditional compilation for object files in Makefile?
    By jutirain in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2007, 06:23 AM
  3. conditional compilation
    By Garfield in forum C Programming
    Replies: 4
    Last Post: 10-21-2001, 09:08 AM
  4. Conditional Compilation
    By Peachy in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 08:54 PM
  5. Preproccessor conditional compilation
    By Garfield in forum C Programming
    Replies: 5
    Last Post: 09-28-2001, 09:28 AM