How to use C language functions and macros
This article mainly explains "how to use C language functions and macros". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how C language functions and macros are used.
Functions and macros
Macros are directly replaced and expanded by preprocessors, and the compiler does not know that macros exist.
A function is an entity compiled directly by the compiler, and the calling behavior is determined by the compiler.
Using macros multiple times will result in an increase in the size of the final executable program
The function is executed by a jump, and only one function body exists in memory.
Macros are more efficient than functions because they are expanded directly and have no calling overhead.
When a function is called, an activity record is created, which is not as efficient as a macro
Let's take a look at an example of functions and macros. Let's look at this program first:
# include # define RESET (p, len)\ while (len > 0)\ ((char*) p) [--len] = 0 void reset (void* p, int len) {while (len > 0) ((char*) p) [--len] = 0;} int main () {int array [] = {1,2,3,4,5}; int len = sizeof (array); int I = 0 RESET (array, len); for (iTuno; I)