Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C language functions and macros

Shulou Source: shulou.com Published: 2022-05-31 21:58:00 09月11日 Update

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)

Tags: Functions results languages compilation output efficiency programs compilers learning code content side effects functions magic files are made by examples types macros input Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple OPPO Reno NVidia Linux