Fundamentals 2 min read

What’s the Hidden Trick Behind This Unusual C Macro in Kernel Code?

The author discovers an unusual macro definition in kernel source that omits the usual parameter list, explains the standard macro usage, shows the original and altered code snippets, and highlights why the macro without arguments works, offering a practical insight for C and embedded developers.

Liangxu Linux
Liangxu Linux
Liangxu Linux
What’s the Hidden Trick Behind This Unusual C Macro in Kernel Code?

While browsing kernel source late at night, the author encountered a macro definition that differs from the usual pattern and decided to share it for readers who might not have seen it before.

Typical macro usage in C/embedded code includes a parameter list, for example:

#include "stdio.h"
#define UART_RBR(base)         (unsigned long)(base+0x01) /* Read only */

unsigned int mtk_uart_read_byte()
{
    unsigned long base = 0x1231231;
    return UART_RBR(base);
}

int main()
{
    printf("%lx
", mtk_uart_read_byte());
    getchar();
    return 0;
}

The author then shows the same code where the macro is defined without the parameter list:

#include "stdio.h"
#define UART_RBR         (unsigned long)(base+0x01) /* Read only */

unsigned int mtk_uart_read_byte()
{
    unsigned long base = 0x1231231;
    return UART_RBR;
}

int main()
{
    printf("%lx
", mtk_uart_read_byte());
    getchar();
    return 0;
}

Even though the macro lacks an argument list, it still expands correctly because the identifier UART_RBR is replaced by the expression that uses the variable base defined in the surrounding function scope. This demonstrates a useful macro trick for low‑level C and kernel development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

c-languagekernel programmingEmbedded Cmacro tricksC macros
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.