Results 1 to 19 of 19

Thread: #define,#ifdef,#else,#endif questions! book isn't very clear how they work..

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Question #define,#ifdef,#else,#endif questions! book isn't very clear how they work..

    Hello everyone,
    I'm not new to programming in C++, i just bought a new book, a "software eingeering" one. and he's covering some things i must have skipped along the ways of reading bad online tutorials,



    okay anyways here is the code

    Code:
    #define MT
        #ifdef MT
        #define NFILE 40
        #else
        #define NFILE 20
    #end if
    alright so called explanation which i don't get really:
    This code is similiar to what you find in the header files; if hte symbol MT is defined, the limit for the number of files is 40; if we remove its definition form the source file, the lmit will be 20.

    now what does it mean "if the symbol MT is defined" ?
    can u give me an example when number one will be true? the number of files == 40, and one of the examples when the number of files == 20? or make up your own example that is eaiseer to understand
    also i don't understand
    Code:
    #ifdef CPLUSPLUS
    //whatever is needed when the program is wirtten in C++
    #endif
    or
    Code:
    #define __cplusplus
       #ifdef __cplusplus
    //whatever is needed when the program is written in C++
    #end if
    can u give me an code example that makes those statements run.

    I understand how a simple one works like this:

    Code:
    #include <iostream>
    #include <cmath>
    #define Pi 3.1415926
    using namespace std;
    
    
    int main()
    {
    	double x,y = 1;
    	double result;
    
    	cout <<"Welcome to the vb-world.com!" << endl;
    	x = y + 1;
    	result = pow(Pi,x);
    	cout <<"In that world, pi square is: " << result << endl;
    	cout <<"Have a nice day!" << endl;
    return 0;
    }


    thansk for your time!!

  2. #2
    Zaei
    Guest
    Using #define <something> without a value (#define MT, for example), you define a symbol. Then, you can use #ifdef, #else, and #endif just like you would a regular boolean statement. So:
    Code:
    #define MT
    #ifdef MT
    #define FILES 40
    #else
    #define FILES 20
    #endif
    is just like a boolean statement. In this case, FILES gets 40, and #define FILES 20 nevers gets executed. If you remove the #define MT:
    Code:
    #ifdef MT
    #define FILES 40
    #else
    #define FILES 20
    #endif
    MT is not defined, so #define FILES 40 doesnt get executed, and #define FILES 20 does.

    Get it? =).

    Z.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Try to avoid macros, they are not type safe. Use constants and typedefs instead of defines:


    #define Pi 3.1415926

    can be replaced with

    const double Pi = 3.1415926;

    #define MT
    #ifdef MT
    #define NFILE 40
    #else
    #define NFILE 20
    #end if

    can be replaced with

    struct useMT{const int NFILE=40;};
    struct noMT{const int NFILE=20;};

    typedef MT useMT;
    ...
    MT::NFILE
    ...
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Zaei
    Guest
    You can usually make Macros type safe. Anything with a decimal defaults to a double, but if you want a float, append an "f" to the end of the value. If no decimal, the default is an int, etc.

    Z.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Keda is right about the constants thing, but I have to disagree with the other thing:
    Code:
    #ifdef _cplusplus
    extern "C" {
    #endif
    
      // all things go here
    
    #ifdef _cplusplus
    }
    #endif
    What about this?

    struntz: everything that starts with # is a precompiler directive. Those will be processed before the real compiler does it's work.
    #define PI 3.1415926
    tells the precompiler to find every occurence of PI in the file and replace it with 3.1415926. In this case, the C++ method:
    const double PI = 3.1415926;
    is better because it's a double, and nothing else.
    #define MT
    defines a symbol. It just tells the precompiler that this is defined. For example, _cplusplus is automatically defined by most compilers when it's compiling C++ code, while it's not defined when compiling C code. MT is defined by VC++ when the application is multithreaded.
    You can later test for these symbols using the defined keyword and the directives for conditioned compiling:
    Code:
    #if defined(MT) // if this is not defined REMOVE the code after this
    // code
    #else // else remove the code after this
    // code
    #endif // until here
    You can also use !defined (not defined). You can concatenate them using || and &&. You can use #ifdef instead of #if defined and #ifndef instead of #if !defined.
    You will often find this:
    Code:
    #ifndef _SOMENAME_
    #define _SOMENAME_
    // code
    #endif
    This is used in header files: if a unique symbol is not defined, define it and include all the stuff in the header. If the header is included a second time, the symbol will already be defined and the code therefore not included.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well that's not the thing i was mentioning, I was pointing out how you can defy macros for alternative values.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I fully agree that you should replace defines by real C++ constants and macros by inline functions, but symbols should still be used, even in C++.

    #define MT
    #ifdef MT
    #define NFILE 40
    #else
    #define NFILE 20
    #end if

    should therefore be replaced by

    #define MT
    #ifdef MT
    const unsigned int NFILE = 40;
    #else
    const unsigned int NFILE = 20;
    #end if
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    the double header avoiding #include and #pragma's are ok
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Talking

    Thanks guys, that hepled me understand it alot better, and i rember the book saying In C++, we use inline functions istead of macros. This wis why i will not discuss macros in further detail, even though a few years ago you had to know how to write macros to pass as a C programmmer. Macros are great fun, but they are a source of errors that are diffuclt to debug.

    Your examples were very helpful,

    thanks again!


  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Well, I once had a terrible problem with the ni/max macros:
    Code:
    #define min(a, b) (((a) < (b)) ? (a) : (b))
    
    int SomeRecursive(int i)
    {
       return min(SomeRecursive(i+1), 1);
    }
    This is expanded to

    Code:
    int SomeRecursive(int i)
    {
       return (((SomeRecursive(i+1)) < (1)) ? (SomeRecursive(i+1)) : (1));
    }
    And suddenly I had two function calls... (My function added contents to a tree control, some items appeared twice)

    Therefore: AVIOD MACROS WHENEVER POSSIBLE (= always)!!!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    indeed

    template <class T>
    inline T min(T a,T b){return a<b?a:b;};
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    template <> inline char* min(char* a, char* b)
    {
    return (strcmp(a, b) < 0) ? a : b;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    nice template specification
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    standard one - learned it together with the templates themselves

    Oh yeah, windows.h contains the min/max macros - tell parksie to replace them by the inline templates
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    If he haven't done that allready, I'll put them in, just wondering what they are doing in windows.h?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    just remember to define NOMINMAX before including windows.h.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I mean do they really belong there?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Well, they are useful (sometimes). And they don't hurt. But one doesn't want them in C++.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I mean windows.h what else do you put in there just because it's "useful" And yep, they're not advocating OOP really
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width