Thread: learning to use #define

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    learning to use #define

    Hi

    My elementary programming skills which I acquired not long ago are very much rusty these days. I was practicing with the code below but I received those two errors about the line at #17 in the source code. Would you please help me?

    I remember when I started posting here people would tell me that my code isn't properly organized, like braces are not aligned etc. They used to use a certain term. What is it? Please tell me. Thank you for all the help.

    Code:
    //define_pi.cpp
    // learning to use #define on Pi
    
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        float r, area;
        #define pi = 3.142;
    
        cout << "enter radius"; cin >> r;
    
        area = 2*pi*r;
    
        cout << "area is: " << area << endl;
    
        system("pause");
        return 0;
    }
    Errors:
    Code:
    17|error: expected primary-expression before '=' token|
    17|error: invalid type argument of 'unary *'|
    Last edited by jackson6612; 09-20-2011 at 10:21 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    #define pi = 3.142;
    should be:
    Code:
    #define pi 3.142
    Actually, it should be:
    Code:
    const double pi = 3.142;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, Light.

    Please also help me with this one. I remember when I started posting here people would tell me that my code isn't properly organized, like braces are not aligned etc.

    By the way, is the code in my first post above properly aligned? Please let me know.

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

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, in terms of indentation it is fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Once again, thanks.

    I replaced the line "#define pi = 3.142" with "#define pi 3.142" as you suggested. But I still get one error about the line #17. What's still wrong? Would you please give it a look? Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    My guess is try changing your float variables to double.

    Also, correct me if I'm wrong but is it better coding practice to declare defines outside of main. For example:

    Code:
    #define pi 3.142
    
    using namespace std;
    
    int main()
    {
        double r, area;
     
        cout << "enter radius"; cin >> r;
     
        area = 2*pi*r;
     
        cout << "area is: " << area << endl;
     
        system("pause");
        return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    I replaced the line "#define pi = 3.142" with "#define pi 3.142" as you suggested. But I still get one error about the line #17. What's still wrong? Would you please give it a look?
    What error did you get? I tested just in case I made a mistake, and I can say for certain that I made no mistake.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    No, it didn't fix it; I mean changing float to double. I still get the same error about line #17: 17|error: invalid type argument of 'unary *'|

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

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    No, it didn't fix it; I mean changing float to double. I still get the same error about line #17: 17|error: invalid type argument of 'unary *'|
    This is the program that I tested:
    Code:
    //define_pi.cpp
    // learning to use #define on Pi
     
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
     
    using namespace std;
     
    int main()
    {
        float r, area;
        #define pi 3.142
     
        cout << "enter radius"; cin >> r;
     
        area = 2*pi*r;
     
        cout << "area is: " << area << endl;
     
        system("pause");
        return 0;
    }
    It does not produce the error for me, but according to you, it does. Try it. What error do you get?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    m_pi(3) - Linux man page :

    #define M_PI 3.141592653589793238462643

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by laserlight View Post
    This is the program that I tested:
    Code:
    //define_pi.cpp
    // learning to use #define on Pi
     
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
     
    using namespace std;
     
    int main()
    {
        float r, area;
        #define pi 3.142
     
        cout << "enter radius"; cin >> r;
     
        area = 2*pi*r;
     
        cout << "area is: " << area << endl;
     
        system("pause");
        return 0;
    }
    It does not produce the error for me, but according to you, it does. Try it. What error do you get?
    I'm extremely sorry, Light. It works for me now. Actually this is what happened. In your first post in this thread you told me to use:
    Code:
    #define pi 3.142
    . I thought you just missed the semicolon at the end out of hurry because you have to help so many people. So, I was using semicolon at the end which gave rise to error. By the way, why don't we use semicolon at the end of it? Please tell me. Thanks a lot.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    #define doesn't need a semicolon at the end because the preprocessor just replaces the word with an expression. Semicolons would be placed in the rest of the code.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    By the way, why don't we use semicolon at the end of it?
    Because preprocessor macros are replaced using fairly dumb substitution. With the semi-colon, this:
    Code:
    area = 2*pi*r;
    effectively becomes:
    Code:
    area = 2*3.142;*r;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, whiteflags.

    So, anything which follows "#" or hash sign is called preprocessor directive? One example is use of #include with header files.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is correct.
    But also because the preprocessor is a dumb text replacer at many times, it is not recommended that you use #define for simple constants.
    The errors you just got could easily have been caught earlier and fixed more easily had you used a constant double/float declaration like laserlight showed you.
    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. #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