|
-
April 3rd, 2007, 10:19 PM
#1
[RESOLVED] Macros
Firstly, this is a macro, yeah?
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;
}
-
April 3rd, 2007, 10:36 PM
#2
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
-
April 3rd, 2007, 10:42 PM
#3
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;
-
April 4th, 2007, 01:25 AM
#4
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 :
That, of course, is not syntactically correct.
-
April 4th, 2007, 01:44 AM
#5
Re: Macros
You might find this FAQ helpful.
-
April 4th, 2007, 07:47 AM
#6
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|