Thread: learning to use #define

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot.

    I understand one shouldn't use #define for simple comstants as Elysia suggests. Anyway, won't it be a good idea to place the "#define pi 3.142" outside the main()?

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Macros do not follow scope rules, so you gain nothing from doing that (as opposed to using a constant).
    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.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Elysia View Post
    Macros do not follow scope rules, so you gain nothing from doing that (as opposed to using a constant).
    Thank you, Elysia.

    So, a macro (or, pre-processor directive) would be accessible from anywhere in the code. But still I have to define (or, write it) before using it. Correct?

    I'm not an English speaker as you know, so would you please tell me what you mean by "as opposed to using a constant"?

    Thanks a lot.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, you got it right.
    By "as opposed to using a constant" I meant that you will get benefits from carefully choosing a scope when using a constant (that is, a variable declared as const), but you don't get any benefits from carefully choosing scope with macros (the scope is ignored).
    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.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, Elysia.

    Is there a particular term to refer to the feature of C++ which requires one to define something before using it? Please let me know. Thanks.

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #21
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by jackson6612 View Post
    Thank you, Elysia.

    Is there a particular term to refer to the feature of C++ which requires one to define something before using it? Please let me know. Thanks.

    Regards
    Jackson
    Define as in #define ..........or function, class, type etc definitions ?

  7. #22
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Well C++ is a typesafe language, meaning that it prevents using type A in place of type B when type B is required and that's pretty much why variables have to be defined before they can be used.

  8. #23
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    I think I need to rephrase my question.

    Is there a particular term to refer to the feature of C++ which requires one to define something before using it? For example, one cannot use a variable unless it has been defined earlier.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #24
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    declaring a variable...

  10. #25
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by jackson6612 View Post
    I think I need to rephrase my question.

    Is there a particular term to refer to the feature of C++ which requires one to define something before using it? For example, one cannot use a variable unless it has been defined earlier.
    That statement is not entirely true, not for C++. The term you are looking for here is "name lookup" rules and they can be rather convoluted in C++.
    Last edited by AndrewHunter; 09-21-2011 at 09:59 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @jackson6612 ... Have they gotten you adequately confused yet?

    Preprocessor statements such as #define, #include, #ifdef, etc. are not source code and probably should not be included in source code... What they do is give the compiler instructions on how to see your code while compiling it. I always make it a rule that *whenever possible* the preprocessor stuff should be at the top of the page, outside of any code... So...
    Code:
    //define_pi.cpp
    // learning to use #define on Pi
    
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    
    #define pi 3.142
    
    
    using namespace std;
    
    int main()
    {
        float r, area;
        cout << "enter radius"; cin >> r;
    
        area = 2*pi*r;
    
        cout << "area is: " << area << endl;
    
        system("pause");
        return 0;
    }

  12. #27
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #define
    By Johnathan1707 in forum C Programming
    Replies: 3
    Last Post: 08-02-2009, 09:10 AM
  2. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  3. #define
    By tikelele in forum C Programming
    Replies: 2
    Last Post: 12-01-2007, 11:43 PM
  4. Learning Dos and learning Windows
    By blankstare77 in forum C++ Programming
    Replies: 8
    Last Post: 07-31-2005, 03:48 PM