Macros in C
Macros :
- Macro is a process where an identifier in a program is replaced by a predefined string or value.
- #define is a preprocessor statement and is used to define macros.
Syntax:
Form 1 : (simple macro)
#define identifier predefined_string or value
Example :
#define pf printf
#define sf scanf
C – MACRO EXAMPLE PROGRAM :
Form 2 : (complex macro or macro with arguments)
Syntax:
#define identifier(arg-1,arg-2,……….arg-n) definition
Example :
#define square(x) x*x
C – COMPLEX MACRO EXAMPLE PROGRAM :
Difference between function and macro :
Function
|
Macro
|
Function is a self contained block of statements. | Macro is a preprocessor statement. |
Function replaces its return value | Macro replaces its definition. |
We can use only specified data types in functions. | In Macros Data types are generic. |
Execution speed of function is less. | Execution speed of Macro is more compared to function. |
C – MACRO EXAMPLE PROGRAM :
Comments
Post a Comment