GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Dec-2008, 00:00
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Definition of a Macro C++


what is a macro ? i see a lot of these statement
#defime Max=10;
any one can define breifly a macro with an example would be great
thanks all.
  #2  
Old 11-Dec-2008, 00:21
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Definition of a Macro c++


Quote:
Originally Posted by zatora
what is a macro ?
Macros are #define substitutions performed by the preprocessor. #define macros were common in C, but don't have the same prominence in C++ because C++ provides equivalent features (inline functions & constants) which allows the compiler to perform type checking (verify the syntax...). Type checking is not performed by the preprocessor.
Quote:
any one can define breifly a macro with an example would be great
CPP / C++ / C Code:
#include <iostream>

using namespace std;

#define VALUE        10

int
main() {
    cout << "VALUE =\t" << VALUE << endl;
    return 0;
}
Macros can also be parameterized:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

#define SQUARE(x)       (x * x)

int
main() {
    cout << "SQUARE(2) =\t" << SQUARE(2) << endl;
    return 0;
}
Note that in the following (incorrect) example, The preprocessor will blindly perform the substitution, but the resulting code is not legal:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

#define SQUARE(x)      (x * x)

int
main() {
    cout << "SQUARE(\"abc\") =\t" << SQUARE("abc") << endl;
    return 0;
}
The preprocessor simply performs substitutions with #define directives. It has no concept of legal syntax nor any intelligence built into it to distinguish what is correct. The preprocessor is meant to be a very simple tool.

Macros will make more sense if you learn to use the preprocessor available with whatever compiler you are using. In this manner, you will learn what code the compiler is really compiling after all preprocessor commands have been expanded (This includes #include directives too...). Most frequently, this will be an executable binary entitiled cpp or cpp.exe. Look it up in whatever documentation is available for your particular compiler, & experiment.

Once you learn what functionality macros provide, recognize that C++ offers other features which are better & better integrated with the compiler itself -- especially when a debugger is used. Although macros by convention are capitalized, some libraries exist which don't follow this dictum. This can chew up time when a programmer is trying to understand why code isn't functioning as it is expected, & what appears to be a function call is really a macro expansion.
Last edited by ocicat : 11-Dec-2008 at 00:55.
  #3  
Old 11-Dec-2008, 03:11
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Definition of a Macro c++


Thanks, ok OCICAT i hope you really can provide some help here. i am just done with my class data structure I so i am taking dat structure II than no more programming classes.
I am trying to get familiar more with c++ comilers and built in classes so these are my questions : ( knowing that i am using visual studio express edition 2008).

1- how to get more information about the c++ language (like macros...)
2- i want to know how the class integer is defined and by that i mean (private members, public members)

thanks all, OCICAT and DAVE (special thanks)
  #4  
Old 11-Dec-2008, 10:59
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Definition of a Macro c++


Quote:
Originally Posted by zatora
1- how to get more information about the c++ language (like macros...)
Read.

One of the better online resources is http://www.cplusplus.com/, however a better choice would be to get one of the better textbooks, & read it cover-to- cover. The best, in my opinion, is Stroustrup:

http://www.amazon.com/C-Programming-...14123&sr= 8-1

...but be advised that this book is meant for people who have some programming background. Likewise, I don't know if it has been translated into your native language. That is for you to determine.

Lastly, as I mentioned before, macros are not as important in C++. In fact, there is little reason to use them at all.

If you are really interested in learning C++, you may be better served by learning more of where it came from. The best book on C is Kernighan & Ritchie:

http://www.amazon.com/Programming-La...14640&sr= 8-1

Chances are, this book may have been translated into your native language.
Quote:
2- i want to know how the class integer is defined and by that i mean (private members, public members)
Your question is unclear. Fundamental types defined by the language do not follow the same public/private implementation form as user-defined types. If you are wanting to know how compilers are written, again, read a book. One of the best is the following:

http://www.amazon.com/Compilers-Prin...9014370&sr=1-1
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to initialize an array in a class definition or in constructor? pablowablo C++ Forum 17 05-Sep-2009 13:14
Macro problem with Assembly language Widgets Assembly Language 5 11-Nov-2007 13:30
Dynamic definition? yuide MS Visual C++ / MFC Forum 1 15-Apr-2006 12:56
SWAP macro alcoholic C Programming Language 4 15-Jan-2006 18:39
Macro Scripting in Power Point alcedo Computer Software Forum - Windows 0 27-Aug-2005 02:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.