Fundamentals 10 min read

Unlocking GNU C Extensions: Zero‑Length Arrays, Variable‑Length Arrays, and More

This guide explains key GNU C extensions—including zero‑length and variable‑length arrays, case ranges, statement expressions, typeof, variadic macros, designated initializers, built‑in functions, and attribute syntax—showing how they differ from standard ANSI C and how to use them safely in Linux kernel development.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Unlocking GNU C Extensions: Zero‑Length Arrays, Variable‑Length Arrays, and More

For embedded software engineers, C is the primary language, but GNU C on Linux adds extensions that differ from standard ANSI C and can cause syntax errors if misused.

Zero‑length and variable‑length arrays

GNU C permits zero‑length arrays, useful for flexible‑size structures:

struct var_data {
    int len;
    char data[0];
};

The array occupies no memory; sizeof(struct var_data) equals sizeof(int). You can iterate over the data placed after the structure in memory.

Variable‑length arrays can be declared with a runtime size:

int main(int argc, char *argv[]) {
    int i, n = argc;
    double x[n];
    for (i = 0; i < n; i++)
        x[i] = i;
    return 0;
}

Case range syntax

GNU C supports case '0'...'9' to match a range of values, equivalent to enumerating each case individually.

Statement expressions

Compound statements enclosed in parentheses become expressions, allowing loops or locals inside an expression:

#define min_t(type,x,y) ({
    type _x = (x);
    type _y = (y);
    _x < _y ? _x : _y;
})

This avoids side‑effects common in standard macros.

typeof operator

typeof(x)

yields the type of x, enabling type‑safe macros without passing the type explicitly.

#define min(x,y) ({
    const typeof(x) _x = (x);
    const typeof(y) _y = (y);
    (void)(&_x == &_y);
    _x < _y ? _x : _y;
})

Variadic macros

GNU C allows macros with a variable number of arguments using ... and ## to swallow a preceding comma when no arguments are supplied:

#define pr_debug(fmt,arg...) printk(fmt, ##arg)

Designated initializers (labelled elements)

Arrays and structs can be initialized out of order using indexes or member names, e.g.:

unsigned char data[MAX] = { [0 ... MAX-1] = 0 };
struct file_operations ext2_file_operations = {
    .llseek = generic_file_llseek,
    .read   = generic_file_read,
    ...
};

Current function name

GNU C provides __FUNCTION__ and __PRETTY_FUNCTION__; in C99 __func__ is preferred.

Special attribute syntax

Attributes such as noreturn, format, aligned, and packed are added with __attribute__((...)) to control optimization, checking, and layout.

Built‑in functions

GNU C offers built‑ins like __builtin_return_address, __builtin_constant_p, and __builtin_expect (used by likely() / unlikely()) to aid performance and compile‑time analysis.

When compiling with -ansi -pedantic, GNU extensions generate warnings (e.g., zero‑size arrays), so developers must decide whether to enable or suppress them based on project requirements.

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.

Linux kernelbuilt-in functionsC extensionscompiler attributesGNU Cmacro tricks
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.