Fundamentals 5 min read

5 Little‑Known C Tricks That Can Boost Your Code

This article explores five obscure yet useful C language features—enum bit‑fields, label‑and‑goto error handling, varargs functions, unconventional union usage, and conditional compilation—providing clear explanations and code examples to help programmers write more flexible and efficient code.

Liangxu Linux
Liangxu Linux
Liangxu Linux
5 Little‑Known C Tricks That Can Boost Your Code

Enum Bit‑Field Definition

Enums can be used as bit‑field flags. Assign each enumerator a power‑of‑two value, combine with |, test with &.

typedef enum {
    FLAG1 = 1,   // 0b001
    FLAG2 = 2,   // 0b010
    FLAG3 = 4    // 0b100
} Flags;

Flags flags = FLAG1 | FLAG3;   // set FLAG1 and FLAG3
if (flags & FLAG1) {
    printf("Flag 1 is set
");
}

Label and goto for Centralised Error Handling

Using a label at the end of a function allows all early‑exit error paths to jump to a single cleanup block, reducing duplicated code.

int func(void) {
    FILE *fp = fopen("data.txt", "r");
    if (!fp) goto error;

    /* other operations */
    if (some_condition) goto error;

    /* normal return */
    fclose(fp);
    return 0;

error:
    /* cleanup resources */
    if (fp) fclose(fp);
    return -1;
}

Variable‑Argument Functions (Varargs)

Include <stdarg.h> and use va_list, va_start, va_arg, and va_end to process an arbitrary number of arguments. Example shows a simple printer that receives a count followed by that many int values.

#include <stdarg.h>
#include <stdio.h>

void printValues(int count, ...) {
    va_list ap;
    va_start(ap, count);
    for (int i = 0; i < count; ++i) {
        int v = va_arg(ap, int);
        printf("%d ", v);
    }
    printf("
");
    va_end(ap);
}

int main(void) {
    printValues(3, 10, 20, 30);   // prints: 10 20 30
    return 0;
}

Unconventional Union Usage

A union overlays different types in the same memory region. This can be used for type‑punning, e.g., interpreting the bit pattern of a float as an int without casting.

union {
    int   i;
    float f;
} data;

data.f = 3.14f;
printf("Float bits as int: %d
", data.i);

Conditional Compilation

Preprocessor macros enable selective compilation. Define a platform macro and guard code with #ifdef / #endif to produce platform‑specific builds.

#define PLATFORM_WINDOWS   /* or PLATFORM_LINUX */

#ifdef PLATFORM_WINDOWS
    /* Windows‑specific implementation */
#endif

#ifdef PLATFORM_LINUX
    /* Linux‑specific implementation */
#endif
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.

CAdvanced Techniquesconditional compilationUNIONVarargsGotoEnum Bitfields
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.