473,473 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why macros are created?

5 New Member
why macros are created, means these are not efficient, and its not good to use them , so why they are created and in what circumstances we should use these??
Feb 3 '11 #1
2 2491
horace1
1,510 Recognized Expert Top Contributor
macros can be used for defining constants that may be used many times in a program, e.g. here I define the clock frequency and baudrates for the PIC24
Expand|Select|Wrap|Line Numbers
  1. // External oscillator frequency (we'll use the value in IrDA_dep.h)
  2. //#define SYSCLK  16000000UL
  3. #define SYSCLK  32000000UL        // 8MHz clock with FNOSC_PRIPLL (baud rate max is 115200)
  4. //#define SYSCLK  8000000UL        // 8MHz clock with FNOSC_PRI (baud rate max 57600)
  5.  
  6. // Define the baud rate constants
  7. #define PC_BAUDRATE     115200UL //57600UL
  8. #define GSM_BAUDRATE    115200UL //57600UL
  9. #define GPS_BAUDRATE     9600UL //57600UL
  10. #define BT_BAUDRATE     115200UL //57600UL
  11.  
if I want to change the clock frequency I can the value associated with SYSCLK - in the above example I can comment and uncomment depending on the hardware.
This saves going thru a large program file by file changing constants like SYSCLK and possibility missing some.

macros with paramters enable one to define function like operations which are eveluated before compile time. e.g.
Expand|Select|Wrap|Line Numbers
  1. // macro to generate port definitions such as PORTDbits.RD6
  2. #define PORT(port, bit) PORT##port##bits.R##port##bit
  3. // macro to generate tris definitions such as TRISAbits.TRISA4
  4. #define TRIS(port, bit) TRIS##port##bits.TRIS##port##bit
  5.  
Feb 3 '11 #2
donbock
2,426 Recognized Expert Top Contributor
The preprocessor macro capability is as old as the C language itself. New features of the language (like inline functions and const variables) have provided new and better ways to accomplish some of the things that were formerly done by macros. There are still legitimate uses for macros.

Why do you say "these are not efficient, and its not good to use them"?
Feb 4 '11 #3

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

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
37
by: seberino | last post by:
I've been reading the beloved Paul Graham's "Hackers and Painters". He claims he developed a web app at light speed using Lisp and lots of macros. It got me curious if Lisp is inherently faster...
8
by: Ulf Kadner | last post by:
Hello! I have read, inline functions are, exactly like macros by the Preprocessor be implemented directly and therefore offer, due to a smaller Overheads a certain speed advantage. Did I...
2
by: Billy R. Moon | last post by:
Has anyone ever come up with a way to disable macro creation within MS Access? Not just on a project level, but to prevent macros from being created and/or ran on a particular PC or by a particular...
3
by: OhMyGaw | last post by:
Hello Excel/automation Gurus, I am working on an application where I have to keep a centralized database of all macros distributed to user and save the changes back on a nightly basis back to...
6
by: DBxGlock | last post by:
I'm trying to create the equivalent of a post build event for a website. I have the "Web Deployment Projects" add-in installed and am attempting to follow the instructions in the "Using Web...
1
by: n4ixt | last post by:
I have two macros I used to use in VS 2003. I recently tried to import them for use in VS 2005, but they don't seem to work. I open the macro explorer, right click and do run, but it's like VS...
4
by: Suzette | last post by:
I have an excel file that automatically updates when opened, pulling information from an Access database into into various pivot tables. I created a macro in Access to perform various necessary...
8
by: dr3586 | last post by:
Hi, I have an Oracle PL/SQL procedure that extracts data into an excel worksheet. I am wondering if there is anyway from the procedure to also set the macros of the excel document that is...
20
by: timmg | last post by:
You know, I've had so much fun reading the thread on lookup field's subservience to the Dark One that I thought I'd provoke another, ah, polite discussion on the topic of Macros. I've always...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.