#define

This is a discussion on #define within the C++ Programming forums, part of the General Programming Boards category; I kind of understand what #define is good for, but when i see a thing like that, Code: #define SIZE ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    #define

    I kind of understand what #define is good for, but when i see a thing like that,

    Code:
    #define SIZE 256
            char First[SIZE];
            char Second[SIZE];
    wouldnt it be just simpler to write,

    Code:
    char First[256];
    char Second[256];
    or maybe i missunderstood something?

    Thanks.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might read about Magic numbers.

    However, it doesn't have to be a #define, and it might be better to use a normal const int size = 256; (for all the reasons why macros are "evil").
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    wouldnt it be just simpler to write,
    What if you decided to change the size from 256 to 512 and you had a Third[256] and a Fourth[256]...and so on. Which one is simpler now ?
    Spidey out!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I kind of understand what #define is good for, but when i see a thing like that,

    The only thing defines should be used for is conditional compilation. Any other uses I can think of would fall into the category of "bad practices".
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Code:
    int main ( ) {
        char    histogram[256] = { 0 };
        char    buff[256];
        int     i;
        fgets( buff, 256, stdin );
        for ( i = 0 ; i < 256 && buff[i] != '\0' ; i++ )
            histogram[buff[i]]++;
        for ( i = 0 ; i < 256 ; i++ )
            printf( "%d %d\n", i, histogram[i] );
    }
    Fancy changing the buffer size by hand? (imagine a program with 10K lines instead).

    Or even just reading the code and trying to figure out in each instance what that particular number really means.

    Compare with
    Code:
    #define BUFF_SIZE   256
    #define MAX_CHAR    256
    int main ( ) {
        char    histogram[MAX_CHAR] = { 0 };
        char    buff[BUFF_SIZE];
        int     i;
        fgets( buff, BUFF_SIZE, stdin );
        for ( i = 0 ; i < BUFF_SIZE && buff[i] != '\0' ; i++ )
            histogram[buff[i]]++;
        for ( i = 0 ; i < MAX_CHAR ; i++ )
            printf( "%d %d\n", i, histogram[i] );
    }
    The code is much easier to read, and changing any of the constants is a snap.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Ok i get it, thank you!
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 11:52 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21