CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Macros

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [RESOLVED] Macros

    Firstly, this is a macro, yeah?

    Code:
    #define MIN 7;
    Secondly, why doesn't this work if all these "macros" are just numbers?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    #define MIN 7;
    #define MAX 8;
    
    int main()
    {
    	cout << MIN * MAX; //this doesn't work
    	
    	cout << 7 * 8;
    }
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: Macros

    Your macro is turning MIN into 7;
    #define statements do not ordinarily end in a semi-colon. Additionaly your main should return something.

    The correct code is

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #define MIN 7
    #define MAX 8
    
    int main()
    {
    	cout << MIN * MAX; // works now!!
    	cout << 7 * 8;
            return 0;
    }
    Regards
    Alan

  3. #3
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Macros

    Hi.

    The first thing is that you have semi-colons at the end of your define statements. Another thing that is important to know is that in several (or most) of the cases, there's a better approach than using macros. For instance, in this particular case, you could have this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    const int MIN = 7;
    const int MAX = 8;

  4. #4
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: Macros

    To clear things further, consider what happens with the first code listing. MIN is defined as 7; and MAX is defined as 8;. When you write cout << MIN * MAX; the compiler makes a direct substitution of the defined values for MIN and MAX. The generated code will look like this :
    Code:
    cout<<7; * 8;;
    That, of course, is not syntactically correct.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Macros

    You might find this FAQ helpful.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Macros

    It was a quick example so that's why I forgot return statement. :$

    Thanks everyone! I didn't know they held stuff like that... Thanks Cilu for the article I read it top to bottom...
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)