473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Question on Macros and #define using C language

[b]what is the diffence between Macro and #define?
Aug 1 '07 #1
5 2357
weaknessforcats
9,208 Expert Mod 8TB
A macro is a placeholder name for a complex definition. You use the macro name instead of the having the complex definition duplicated.

In C and C++ macros are identified by the symbol following a #define.

#define MAX 10

Here MAX is a macro which is a placeholder for 10. You use MAX in your code and the proprocessor will change it to 10 before sedning your code to the compiler.

#define TRACE(x) cout << x << endl

Here TRACE is a macro in your code you can:

int data = 10;
TRACE(data);

The preprocessor changes TRACE(x) to:

cout << data << endl

and the semi-colon, which is not part of the macro, follows. The final result is:

cout << data << endl;

and that is what is sent to the compiler.
Aug 1 '07 #2
JosAH
11,448 Expert 8TB
#define TRACE(x) cout << x << endl
I'd put parentheses around that 'x' if I were you.

kind regards,

Jos (<--- nitpicker ;-)
Aug 1 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You're right. I missed that. It should be:

#define TRACE(x) cout << (x) << endl

I haven't used these for years.
Aug 1 '07 #4
Banfa
9,065 Expert Mod 8TB
You're right. I missed that. It should be:

#define TRACE(x) cout << (x) << endl

I haven't used these for years.
I am surprised that you haven't mentioned that there is little reason to use macros in C++ since constant variables, inlined and templated functions cover pretty much anything you could do with a macro.
Aug 1 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
I am surprised that you haven't mentioned that there is little reason to use macros in C++ since constant variables, inlined and templated functions cover pretty much anything you could do with a macro.
Yes. I never use macros myself. They are an absolute disaster. However the OP didn't say if it was C or C++. Had C++ been mentioned, I certainly would have said something about not using macros in C++.
Aug 2 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
25
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include...
8
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to...
37
by: hasadh | last post by:
Hello, probably this may be a simple qn to u all but I need an answer plz. In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who included this code as a library into his module...
15
by: Jorge Naxus | last post by:
Hello I would like to write a macro like that: #ifdef DEBUG #define printj(...) printf(...) #else #printj(...) #endif
38
by: Andrew Arro | last post by:
so, we have built-in __FILE__ and __LINE__ how could we define __FILELINE__ macros so it would be smth like, for example, "file.c:11" i've tried #define __FILELINE__ __FILE__ ## ":" ##...
3
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v +=...
47
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant...
37
by: mdh | last post by:
In one of the answers to a K&R exercise, the first couple of lines are: enum loop { NO, YES}; enum loop okloop=YES; I get the first line, but not the second. Sorry about the LOL question. ...
27
by: Cephalobus_alienus | last post by:
Hello, I know that macros are evil, but I recently came across a problem that I couldn't figure out how to solve with templates. I wanted to create a set of singleton event objects, and wrote...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.