What does this mean?

This is a discussion on What does this mean? within the C Programming forums, part of the General Programming Boards category; In one of our lab tutorials, the tutor gave us some header files to use for our lab work, he ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    What does this mean?

    In one of our lab tutorials, the tutor gave us some header files to use for our lab work, he typedef'd "Item" as type int and then he put:

    Code:
     #define eq(A, B)  (A == B)
    I'm guessing an 'eq(A,B)' is to be replaced with (A == B) in the code? But we never ever used that, so perhaps it means something else?

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Nope, you're quite correct. Anything that starts with a # is a command to the preprocessor. In this case, it will cause the preprocessor to find-and-replace all occurences of eq(A, B) with (A == B), whatever A or B is.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, all #defines are "replacement macros" [but they CAN be used to see if they are simply defined or not defined too].

    In this case, you'd have some code like this:
    Code:
    T x, y;
    ... 
    
    if (eq(x, y)) ... 
    ...
    Which would compile as:
    Code:
    ...
    if (x == y) ... 
    ...
    But if you don't use the macro, it does "nothing" [assuming, also, of course nothing else uses it].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Yea, I was just confused as to why he specified it because it was never used.

    Thanks anyways.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    But might I add that you be careful with macros? The above example is bad macro, because all it does is essentially create a "new language within a language," basically creating new syntax which will confuse other programmers.
    Macros are also very hard to debug, so use them sparingly and well.
    That's all
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    But might I add that you be careful with macros? The above example is bad macro, because all it does is essentially create a "new language within a language," basically creating new syntax which will confuse other programmers.
    Macros are also very hard to debug, so use them sparingly and well.
    That's all
    Yes, that particular one looks like it's trying to sort of use Fortran style comparisons (which in early fortran use .ne. and .eq. for example).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

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