473,401 Members | 2,139 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,401 software developers and data experts.

macro?

what is macro in C programming language?and what does it do?can some body
explain it to me in simple terms?
Thank you!
Jan 8 '10 #1
2 1587
weaknessforcats
9,208 Expert Mod 8TB
A macro is a way to refer to something indirectly. For example, suppose your program deals with the color red and that red has a value of 2. Next suppose you have hard-coded those 2's in about 500 places in your program and then your boos changes the specs so that red is now zero. How you have to go through the code change all the 2's to zero and not accidentally chnage a 2 to zero that should remain a 2 becuse it refers to something else.

A macro works around this problem. Here is a macro:

Expand|Select|Wrap|Line Numbers
  1. #define RED  2
The pre-processor runs before your compiler and it looks for lines starting with a #. These are preprocessor directives and the #define is a macro. The preprocessor will go through the program and change all occurrences of RED to 2. The compiler sees the 2.

Now when you boss changes red to zero all you need do is:

Expand|Select|Wrap|Line Numbers
  1. #define RED 0
and rebuild yout program.

You can use macros to represent blocks of code also.
Jan 8 '10 #2
donbock
2,426 Expert 2GB
It may be helpful to think of the programming process as consisting of a sequence of steps:
  • edit-time (when you use an editor to write the source code)
  • compile-time (when the tool-chain compiles source code into object code)
  • link-time (when the tool-chain links object code into executable code)
  • execution-time (when the program actually executes)
(This is only a partial list of the steps.)

Some editors allow you to mechanize part of the editing process (through macros, regular expression substitution, cut and paste, etc.). These mechanizations can be interpreted as small standalone programs that execute during edit-time to alter/create the C source code.

C macros can similarly be thought of as small standalone programs that execute during compile-time to alter/create the C source code. Actually, the macros execute (or "expand") during preprocess-time which is just prior to compile-time, but that distinction can be deferred.

Finally, your program does its thing during execution-time.

By the way, each step can produce error messages. It is much easier to interpret the error message when you take into account which step generated it.
Jan 8 '10 #3

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

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
3
by: Alexander Ulyanov | last post by:
Hi all. Is it possible to pass the whole blocks of code (possibly including " and ,) as macro parameters? I want to do something like: MACRO(FOO, "Foo", "return "Foobar";", "foo();...
8
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
5
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.