Thread: #define function

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    8

    #define function

    Hello everyone. I started learning C and everything till now is perfect. But there is something which i couldn't completely understand.
    Why do we use the #define function ?
    what is the difference between these two :

    age = 20;

    and

    #define AGE 20;

    i couldnt understand what is the use of #define function. I hope anyone could explain it to me.
    Thanks!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's not a function. It's a "preprocessor instruction". They can be function-esque.* The preprocessor goes through the code before it is compiled. What it does is literally replace an instance of a #define label with it's output. So in your example, anywhere the label "AGE" occurs in the code, the preprocessor replaces it with the number 20. This is very handy if you want to say, use a file in a number of different places:

    Code:
    #define INPUT_FILE "thatdata.txt"
    Now, if you want to change the file used, rather than search and replace the name everywhere, you use the define label in it's place, and all you have to do is change that one #define.

    There are also defines that work like functions, called macros. So for example:
    Code:
    #define MAX(a, b)  (a)>(b) ? a : b
    This uses the ternary operator -- of the form condition ? true : false -- to output the higher value. So if you use this macro somewhere:
    Code:
    int x = 5, y = 6, z = MAX(x,y);
    The preprocessor will replace that with 6.

    * actually you can write more or less literal C functions into a define but they have some logical limitations.
    Last edited by MK27; 06-17-2010 at 12:25 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    To follow up on MK27's post, something I consider important. Take his example:

    Code:
    #define MAX(a, b)  (a)>(b) ? a : b
    And the following piece of code:
    Code:
    c = MAX(a++, b++);
    This actually becomes:
    Code:
    c = (a++)>(b++) ? a++ : b++;
    So you'll get incorrect and unexpected results ;-). So best avoid using macro's, most of the time.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by EVOEx View Post
    So best avoid using macro's, most of the time.
    Between you & Elysia we'll all be avoiding programming soon -- it is just too error prone
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MK27 View Post
    Between you & Elysia we'll all be avoiding programming soon -- it is just too error prone
    Not at all. Programming is finding the right tool for the right problem. In case of the max function, it may sometimes be useful using a macro (if it needs to work with any type). I'd prefer using C++ with a template function, however. But for pure C, yeah, MAX should probably be a macro.
    But still; be careful with macro's.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by EVOEx View Post
    I'd prefer using C++ with a template function
    Heck, I'd prefer to just use a dynamically typed language with a nice foolproof built-in. But that is not very useful advice for someone trying to learn C.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'd prefer to just use a dynamically typed language with a nice foolproof built-in.
    O_o

    Really?

    Soma

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phantomotap View Post
    O_o

    Really?

    Soma
    Yeah, I'm not religious so have no commitment to any particular church. Altho to be honest a macro for MAX will do me just fine. Danger be damned!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm not religious so have no commitment to any particular church.
    O_o

    Oh? This has taken a weird turn. So all those "C is better than C++" posts of yours, those before and after trying to learn C++, are hyperbole? I never realized. You defend hyperbole with a strange aggression.

    Soma

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phantomotap View Post
    all those "C is better than C++" posts of yours, those before and after trying to learn C++,
    There aren't any. Read more carefully. I have made some grouchy observations about specific details of C++, such as the defanging of void*, and tried to provide some perspective on "C++ salemanship", but I have never said anything to the effect that one language is ultimately "better" than the other.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    True. You probably didn't ever use the word "better" or any other comparative. Since when does one need to use a comparative to proselytize?

    Soma

  12. #12
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    int age = 20
    is declaring a variable.
    Code:
    #define age 20
    simply replaces every occurance of "age" with "20".

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    #define code helped me a lot Thanks everyone

  14. #14
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    C is getting harder now. I copy the same code that i read from the book but it doesn't work for me.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you have to show us the code you're copying. Otherwise we can't help.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM