The Most Stupid C Bug: How a Backslash Turned tmpfile() Into a Comment
This article recounts a baffling C bug where Microsoft's tmpfile() on Windows creates files in C:\, causing a stray backslash to comment out code, and explains how replacing a ternary operator with an if‑else fixed the issue while illustrating common cross‑platform pitfalls.
The story originates from an article titled “The most stupid C bug ever” and describes a subtle bug that can affect even experienced C programmers.
The author wrote a simple HTTP downloader in C that creates a file: if a filename is provided it opens that file, otherwise it calls tmpfile() to create a temporary file. This works on Unix/Linux, but on Windows the Microsoft implementation of tmpfile() places the file in C:\, which can cause permission problems and, more critically, introduces a stray backslash in the source code.
Because in C the backslash \ at the end of a line escapes the newline, the stray backslash caused the following code to be treated as a comment, breaking the logic. The author initially added a // FIXME note and attempted a cross‑platform solution by writing a custom w32_tmpfile() function and redefining tmpfile() via a macro on Windows.
After recompiling, the program still didn’t call the custom function. Debugging revealed that the ternary (question‑mark) expression used in the original code was being ignored due to the comment issue. Replacing the ternary with an explicit if‑else statement resolved the problem.
The article then presents the working and non‑working code snippets side by side. The working version uses:
/* Write new file (plus allow reading once we finish) */
// FIXME Win32 native version fails here because Microsoft's version of tmpfile() creates the file in C:
if (NULL != fname) {
g = fopen(fname, "w+");
} else {
g = tmpfile();
}The broken version suffers from the stray backslash turning the rest of the line into a comment, so the tmpfile() call never executes.
The author also shares a personal mistake involving a missing space that turned a division operation into a comment, illustrating how tiny syntax errors can cause hours of debugging:
float result = num/*pInt; // missing space makes this a comment
/* some comments */
-x<10 ? f(result) : f(-result);Both examples highlight the importance of careful code review, proper handling of platform‑specific functions, and the pitfalls of using terse conditional expressions without considering how comments and line continuations interact.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
