Thread: #define

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

    #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.
    Using Windows 10 with Code Blocks and MingW.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Ok i get it, thank you!
    Using Windows 10 with Code Blocks and MingW.

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, 12:52 PM
  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, 09:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM