Fundamentals 6 min read

The Stupid C Bug That Turns a Ternary Operator into a Comment

A seemingly harmless C ternary expression fails on Windows because a backslash in a comment turns the rest of the line into a comment, leading to a cross‑platform tmpfile bug and a similar spacing mistake that broke the code.

Liangxu Linux
Liangxu Linux
Liangxu Linux
The Stupid C Bug That Turns a Ternary Operator into a Comment

The original program downloads an HTTP file and decides whether to write to a user‑provided filename or to a temporary file using the expression g = fname ? fopen(fname, "w+") : tmpfile();. A // FIXME comment notes that Microsoft’s implementation of tmpfile() creates the file in C:\, which causes permission problems on Windows.

Attempted Cross‑Platform Fix

To avoid the Windows issue, the author defines a custom function w32_tmpfile() and uses a macro to rename tmpfile to this implementation on Win32:

#ifdef _WIN32
  #define tmpfile w32_tmpfile
#endif

FILE * w32_tmpfile(void) {
  // code for Windows
}

However, after recompiling the program the custom function is never called.

Root Cause: Comment Backslash

Debugging shows that the line containing the ternary operator is parsed incorrectly because the preceding comment ends with a backslash ( \). In C, a backslash at the end of a line escapes the newline, causing the rest of the line to be treated as part of the comment. Consequently the ternary expression is effectively commented out, and the compiler uses the original code path.

Replacing the ternary expression with an explicit if‑else block works because the problematic line is now commented out, and the logic becomes clear:

if (NULL != fname) {
    g = fopen(fname, "w+");
} else {
    g = tmpfile();
}

Another Similar Pitfall

The author also shares a personal mistake where missing whitespace after a block comment caused the compiler to treat part of the code as a comment. The original line:

float result = num/*pInt; …
/*  some comments */
-x<10 ? f(result):f(-result);

was parsed as:

float result = num-x<10 ? f(result):f(-result);

The missing space after /* turned the intended division into a comment, producing a completely different expression.

Both examples illustrate how tiny syntactic details—an escaped newline in a comment or a missing space—can introduce hard‑to‑detect bugs in C code.

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.

Cross-platformC++bugPreprocessortmpfileternarycomment
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.