C++ build errors

C++ "stray '\342' in program": Fix Pasted Curly Quotes

GCC 10 and later reject pasted curly quotes, en dashes, and non-breaking spaces when they appear where the source requires an identifier or operator. The compiler calls each character an extended character that is not valid in an identifier.

What a stray character diagnostic identifies

GCC 9 and earlier reported valid UTF-8 curly quotes as stray '\342' in program, where \342 is octal for byte 0xE2. That older wording does not describe the UTF-8 examples on this page. GCC 16 still reports a stray-octal-byte error when the source file is not valid UTF-8.

error: extended character “ is not valid in an identifier
error: extended character ” is not valid in an identifier

Curly quotes are not C++ string delimiters

C++ source uses the straight quotation mark " to begin and end an ordinary string literal. A word processor may replace that character with opening and closing curly quotes. Those marks look similar on screen, but they are distinct Unicode characters and do not delimit a C++ string.

#include <iostream>
int main() {
    std::cout << “Ready” << '\n';
}

With cl /std:c++17 /EHsc /W4 /permissive- /utf-8, MSVC reports error C3873: 'U+201c': this character is not allowed as a first character of an identifier. It also reports error C3872: 'U+201d': this character is not allowed in an identifier for the closing quote. Replace both marks with straight quotes.

#include <iostream>
int main() {
    std::cout << "Ready" << '\n';
}
Ready

MSVC wording depends on the source encoding setting

MSVC reports Unicode code points when you compile with /utf-8 or when the file has a UTF-8 BOM. Without either setting, MSVC reads a file without a BOM using the system code page. The same UTF-8 bytes then produce different wording because the compiler decodes them differently.

For the same curly-quote source without /utf-8 or a BOM, MSVC reports error C3872: 'U+9d': this character is not allowed in an identifier. That code point reflects the system-code-page interpretation. Do not treat it as the original Unicode character; use the UTF-8 capture when you want a diagnostic tied to the file's actual encoding.

A pasted en dash is not the minus operator

The minus operator is the ASCII hyphen-minus -. Text editors can substitute an en dash (U+2013), whose UTF-8 bytes also begin with 0xE2. The compiler does not reinterpret the typographic dash as arithmetic punctuation.

#include <iostream>
int main() {
    int value = 3;
    return value – 1;
}
error: extended character – is not valid in an identifier

With /utf-8, MSVC reports error C3873: 'U+2013': this character is not allowed as a first character of an identifier. It follows with C2146 and C2065 messages because parsing cannot continue through that token. Replace the en dash with the keyboard hyphen-minus.

#include <iostream>
int main() {
    int value = 3;
    std::cout << value - 1 << '\n';
}
2

A non-breaking space is not source whitespace

A U+00A0 character sits between int and main below.

A non-breaking space (U+00A0) can appear when code is copied from formatted text. It looks like an ordinary space, but it is not one of the source characters MSVC accepts between tokens. Use an ASCII space or tab between the type and function name.

#include <iostream>
int main() {
    std::cout << "Ready\n";
}

With /utf-8, MSVC reports error C3872: 'U+a0': this character is not allowed in an identifier. Later C4430 and C4508 diagnostics follow because the declaration no longer parses as intended. GCC also warns no return statement in function returning non-void [-Wreturn-type] because it reads int main as one identifier. Replace the character rather than adding punctuation around it.

error: extended character   is not valid in an identifier
#include <iostream>
int main() {
    std::cout << "Ready\n";
}
Ready

A Windows-1252 file contains invalid UTF-8 bytes

Windows-1252 uses bytes 0x93 and 0x94 for curly double quotes. Those bytes do not form valid UTF-8, so GCC 16 reports each Windows-1252 quote byte, \223 and \224, as a stray character. Save the file as UTF-8 and replace the quotes with straight quotes.

#include <iostream>
int main() {
    std::cout << “Ready” << '\n';
}
error: stray '\223' in program
error: stray '\224' in program

With /utf-8, MSVC reports error C2018: character 'U+93' is not permitted here and the corresponding C2018 error for U+94.

#include <iostream>
int main() {
    std::cout << "Ready" << '\n';
}
Ready

Check the source file, not its appearance

If a line looks correct in the editor, inspect its encoding or compare its bytes with a plain-text version. For the left curly quote, UTF-8 stores e2 80 9c. These bytes are a valid encoding of U+201C, but the quote is still the wrong token outside a string literal. Replacing it with ASCII punctuation fixes the C++ source; changing the encoding alone does not turn typographic punctuation into an operator or delimiter.

After correcting a line, compile the edited file again with the same encoding option. If a C2065 or missing-token message follows the character error, fix the earliest C3872 or C3873 first. A later parser message can disappear once the compiler sees ordinary source punctuation.

Exercise

Replace the pasted curly quotation marks so the program prints Ready.

Ready
#include <iostream>
int main() { std::cout << “Ready” << '\n'; }
Show one possible solution
#include <iostream>
int main() { std::cout << "Ready" << '\n'; }

Keep learning with CppPath

Continue with the full lesson Program Structure. It includes a quiz and checked coding challenge available with a free account.