Compilation (gcc error)Compiler Syntax5 min read

Fix “stray '\342' / non-ASCII character in program” in C

C source tokens outside string literals and comments must use standard ASCII punctuation (`"`, `'`, `-`, `;`, spaces). When code is copied from a PDF, word processor, or webpage, UTF-8 multi-byte sequences such as curly quotes (`0xE2 0x80 0x9C`, octal `\342\200\234`) or non-breaking spaces (`0xC2 0xA0`, octal `\302\240`) appear in source lines and GCC rejects them as stray bytes or invalid extended characters.

Quick Fix Summary

Retype quotation marks as straight ASCII " and ', replace typographic minus/en-dash – with -, and delete invisible indentation characters on the reported line before recompiling.

Why This Error Happens

Why octal numbers like \342 or \302 appear in older GCC output

UTF-8 curly quotes and dashes begin with byte 0xE2 (octal 342), while non-breaking spaces begin with 0xC2 (octal 302). Newer GCC releases name the Unicode character directly, while older toolchains print each byte in octal.

Invisible non-breaking spaces (U+00A0) and zero-width spaces (U+200B)

Code copied from rich-text editors often replaces indentation spaces with U+00A0, causing errors on lines that look visually normal.

Top Root Causes & Specific Code Fixes

Curly double or single quotes pasted around printf format strings

Word processors auto-convert straight ASCII `"` into `“` and `”`.

Broken Pattern
printf(“Hello, world!\n”);
Corrected Pattern
printf("Hello, world!\n");
Illustrative diagnostic (not executed):error: extended character “ is not valid in an identifier

En-dash (–) instead of ASCII hyphen-minus (-) in subtraction or flags

Slides often turn `- 1` into `– 1`, which is not the C subtraction operator.

Broken Pattern
int last = count – 1;
Corrected Pattern
int last = count - 1;

Complete Broken and Corrected Programs

Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.

Read the broken program
#include <stdio.h>
int main(void) {
    /* Smart quotes around Hello cause stray character errors when unfixed */
    printf(“Hello C\n”);
    return 0;
}
Open broken program in compiler
Read the corrected program
#include <stdio.h>
int main(void) {
    printf("Hello C\n");
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

Hello C

Debugging Techniques and Commands

Highlight Non-ASCII Bytes (LC_ALL=C)
LC_ALL=C grep -n '[^ -~\t]' main.c

Prints exact line numbers containing hidden UTF-8 bytes outside printable ASCII.

GCC Diagnostic Caret
gcc -std=c17 -Wall main.c -o app

Points directly at the first offending non-ASCII character column.

Frequently Asked Questions

Are UTF-8 characters allowed inside C string literals?

Modern GCC and Clang accept UTF-8 text inside `"..."` string literals and comments, but delimiters themselves (`"`, `'`, `;`, `-`) must always be plain ASCII.

What is stray \302 \240 in program?

Octal `\302 \240` (`0xC2 0xA0`) is a UTF-8 non-breaking space (`&nbsp;`), frequently inserted when copying code from HTML tables or chat messages. Delete the whitespace on that line and press the normal Spacebar.

What is stray \357 \273 \277 at line 1?

Octal `\357 \273 \277` (`0xEF 0xBB 0xBF`) is a UTF-8 Byte Order Mark (BOM) saved by some Windows editors; save the file as UTF-8 without BOM.