What is the difference between a macro and a function in C?
| Macro | Function |
|---|---|
| Text replace by the preprocessor | Compiled code that is called |
| No type checking | Types are checked |
| Code is copied at each use (bigger program) | One copy of the code |
| Faster, no call overhead | Small call overhead |
Arguments can run two times (SQUARE(i++)) | Arguments are evaluated once |
Use a function when you can. Use macros for constants and small tricks only. See Preprocessor and Macros.
Learn more about the similar topics:
| Tutorials |
|---|
| C Preprocessor and Macros |
| C File Handling |
| C Read From a File |
| C Command Line Arguments |
| Exercises & Assignments |
|---|
| No Content Found. |