Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

advantages and disadvantages of macros

Status
Not open for further replies.

soniya_ahuja

Newbie level 4
Joined
Feb 11, 2006
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Mumbai
Activity points
1,348
Queries in C


Can anyone please tell me what are the advantages and disadvantages of macros? I would also lie to know the main difference between malloc and calloc.

One more question -

Can you define a function that can have indefinite parameters as inputs?

Thank you
Regards
Soniya
 

razor_guy_mania

Junior Member level 1
Joined
Feb 11, 2006
Messages
16
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,283
Activity points
1,443
disadvantages of macros

Part I -Macros

1. Macros make program readable
eg: #define sqr(a) a*a

so everytime preprocessor will replace say sqr(2) by 2*2
so they r easier to use instead of functions in some cases
2. Macros are faster than functions
Macros may make compiling slower but compiled programs are faster
because functions involve passing values which increase cpu usage.

3. They make our prog compact
4. Defining arbitrary constants is easy.
eg:-
#define matrix_size 3
main()
{
int mat[matrix_size][matrix_size];
/*ur rest program on processing matrix*/
}

Thus in above case the arbitrary constant matrix_size can be changed
instead of changing each and every process step.
5. Macros can used to shorten frequently used commands or operations.

Part II - malloc() and calloc()

malloc() - Allocates a specified number of bytes in memory. Returns a pointer
to the beginning of the allocated block

calloc() - Similar to malloc(), but initialises the allocated bytes to zero. This
function also allows us to allocate memory for more than one object
at a time.
 

swgchlry

Member level 4
Joined
Dec 8, 2003
Messages
70
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
718
advantages of macros in c

disadvantage:
1.It make the program unreadable if you use too much.
2.You should use ansi c99's standard about macros
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top