Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2009
Member
 
Join Date: Feb 2009
Posts: 96
What is MACROS in programming?

What is the concept of macros in programming languages ?
Reply With Quote
  #2  
Old 06-03-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: What is MACROS in programming?

A macro is a fragment of code which has been given a name called macroname. Whenever the macroname is used in developing a program, it is replaced by the contents of the macro.

There are two kinds of macros. They mostly differ in what they look like when they are used.
  • Object-like macros resemble data objects when used,
  • Function-like macros resemble function calls.
Reply With Quote
  #3  
Old 06-03-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: What is MACROS in programming?

In programming, macro represents the segment of code. It has to be defined along with the header files.
It is represented as :

In C :
#include<stdio.h>
#define macro macroname
void main ()
{
-------
-------
}


In C++ :
#include<iostream.h>
#define macro macroname
void main ()
{
-------
-------
}
Reply With Quote
  #4  
Old 06-03-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: What is MACROS in programming?

There are two types of macros, object-like and function-like.
Object-like macros do not take parameters; function-like macros do.

The generic syntax for declaring an identifier as a macro of each type is, respectively,

#define <identifier> <replacement token list>
#define <identifier>(<parameter list>) <replacement token list>

Note that the function-like macro declaration must not have any whitespace between the identifier and the first, opening, parenthesis. If whitespace is present, the macro will be interpreted as object-like with everything starting from the first parenthesis added to the token list.

Whenever the identifier appears in the source code it is replaced with the replacement token list, which can be empty. For an identifier declared to be a function-like macro, it is only replaced when the following token is also a left parenthesis that begins the argument list of the macro invocation. The exact procedure followed for expansion of function-like macros with arguments is subtle.

Object-like macros were conventionally used as part of good programming practice to create symbolic names for constants, e.g.

#define PI 3.14159

instead of hard-coding those numbers throughout one's code. However, both C and C++ provide the const directive, which provides another way to avoid hard-coding constants throughout the code.

An example of a function-like macro is

#define RADTODEG(x) ((x) * 57.29578)

This defines a radians to degrees conversion which can be written subsequently, e.g. RADTODEG(34) or RADTODEG (34). This is expanded in-place, so the caller does not need to litter copies of the multiplication constant all over his code. The macro here is written as all uppercase to emphasize that it is a macro, not a compiled function.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is MACROS in programming?"
Thread Thread Starter Forum Replies Last Post
What is the use of macros in the word? bharti_rajpal Windows Software 3 07-12-2009 01:41 PM
Socket programming: Is any new Programming Language? Kushan Software Development 3 14-11-2009 11:13 AM
How to Run Excel Macros from ASP Chain-SmokeR Software Development 2 04-07-2009 12:23 PM
Macros in Word DanB Windows Software 1 02-06-2009 08:09 AM
JetBrains Introduces a New Programming Paradigm With its Meta Programming System JoeFrat Software Development 3 13-12-2008 01:49 PM


All times are GMT +5.5. The time now is 12:50 PM.