Results 1 to 4 of 4

Thread: What is MACROS in programming?

  1. #1
    Join Date
    Feb 2009
    Posts
    96

    What is MACROS in programming?

    What is the concept of macros in programming languages ?

  2. #2
    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.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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 ()
    {
    -------
    -------
    }

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    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.

Similar Threads

  1. What is the use of macros in the word?
    By bharti_rajpal in forum Windows Software
    Replies: 3
    Last Post: 07-12-2009, 01:41 PM
  2. Socket programming: Is any new Programming Language?
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 11:13 AM
  3. How to Run Excel Macros from ASP
    By Chain-SmokeR in forum Software Development
    Replies: 2
    Last Post: 04-07-2009, 12:23 PM
  4. Macros in Word
    By DanB in forum Windows Software
    Replies: 1
    Last Post: 02-06-2009, 08:09 AM
  5. Replies: 3
    Last Post: 13-12-2008, 01:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,905,942.01632 seconds with 17 queries