跪拜 Guibai
← Back to the summary

The '烫烫烫' Mystery and Other C/C++ Beginner Traps: Full-Width Characters, Memory Layout, and Encoding Mismatch

Full-width vs Half-width Distinction (Chinese vs English)

Half-width: English characters, occupy one byte, narrower width, standard ASCII code

Full-width: Chinese characters, occupy two bytes, appear wider, Chinese GBK or Unicode encoding

In C/C++ and other programming languages, the compiler strictly distinguishes between Chinese and English symbols

Garbled-character errors: C3872 or invalid character

Character Type vs String Type: Single/Double Quotes and Character Count

  1. Fundamental type and memory footprint differences

Single quote: In C, a single-quoted value is a character constant. Essentially an integer, i.e., the ASCII code value of the corresponding character. It occupies only 1 byte.

Double quote: C has no native string type. A double-quoted value is called a string literal; it is essentially a character array. C specifies that every string must end with a hidden null character \0.

  1. "A" and 'A' are completely different in memory:

'A' occupies 1 byte, content: 65

"A" occupies 2 bytes, content: 65 0

  1. Assignment target differences

A character can be assigned to a char variable;

A string cannot be directly assigned to a char variable; it can only be assigned to a character array or a character pointer.

  1. You cannot put multiple characters inside single quotes!

'AB'—sometimes this might compile, known as a "multi-character constant." Its value is determined by the compiler, usually by concatenating ASCII codes. Such code is non-portable and is an absolute programming bad habit.

  1. Surface length vs actual memory footprint of a string

The memory a string occupies is its surface length plus one \0

strlen("Hello") calculates the surface length; it stops counting at \0, so the result is 5

sizeof("Hello") calculates memory footprint, including the hidden \0, so the result is 6

Can a Character-Type Quantity Within Single Quotes Only Be One Character?

There is exactly one character inside single quotes—this statement is absolutely correct logically.

But there is a subtle detail that can cause ambiguity: escape characters appear to have more than one character on the surface, but as a whole they count as one character.

Fun fact: In C++ (VS uses C++ by default), the type of 'A' is char, which indeed occupies 1 byte; But in pure C, there is a bizarre rule: the character constant inside single quotes is actually of type int, usually occupying 4 bytes. Only when it is assigned to a char variable does it get truncated and squeezed into a 1-byte variable.

Can We Say That Any Quantity That Can Be Written Inside Single Quotes as a Character Type Has a Matching Character in the ASCII Table, One-to-One?

This statement is basically accurate, but a few qualifiers must be added:

  1. It must be a "single-byte" character

The standard ASCII table has only 128 characters; the extended version has 256, covering only English letters, digits, punctuation, and control characters.

If you write a '中', it is invalid. A Chinese character typically occupies 2~3 bytes under UTF-8 or GBK encoding, while a general char type has only 1 byte—it simply doesn't fit, and the compiler will error or truncate it.

  1. The "visual deception" of escape characters, as discussed above

  2. It must conform to syntax rules (excluding multi-character constants)

Also discussed above: writing 'AB' is a "multi-character constant." It is not a standard single character. In C, its value is determined by the compiler itself (usually by concatenating ASCII codes), and it does not correspond one-to-one with a specific ASCII character. This is a programming bad habit.

In summary, in C/C++, any legal, single-byte character constant (including visible characters and escape characters) necessarily corresponds to a unique integer value in the ASCII table.

Why Does VS2026 Recommend Checking "Place Solution and Project in the Same Directory" When Creating a New Project? What Is a Solution?

Project: Contains specific code, resources, and build settings; ultimately generates a standalone program (e.g., .exe)

Solution: A top-level container (.sln file) that manages one or more related projects

Checked: Flat management. All files are directly under \ProjectName\, with a clean path, fast to locate code and back up.

Unchecked: Produces a "Russian doll" nesting path \ProjectName\ProjectName. For single-file beginner exercises, this is unnecessary and only adds trouble finding files.

In the future, when developing large enterprise multi-project applications, unchecking this box is needed, using physical isolation to standardize the project structure.

How to Set Line Numbers in VS? How to Adjust Fonts?

Top menu bar---Tools---Options---Text Editor---C/C++---General---Display---Line numbers

Top menu bar---Tools---Options---Environment---Fonts and Colors

Recommended monospaced font Consolas: clearly distinguishes digit 0 from letter o, and digit 1 from letter l

Q: When printing a string without a trailing \0, why does printf, even though a newline is set, read backward through the "烫烫烫" region and include the next line's string, matching the content of the next print?

A: My understanding of the above example: a string without \0 keeps reading backward in memory, looking for a place to stop. Other complete strings printed later are located near the first one in memory, so after reading through the "烫烫烫" in the middle, it continues to output the content of the next array until it hits that string's \0 and stops printing.

Core: out-of-bounds memory read & adjacent memory layout

Origin of "烫": In VS's Debug mode, the system fills uninitialized stack memory with 0xCC. Under GBK encoding, 0xCC happens to be the character "烫," which is why a series of "烫" appears.

The \n in the code is not a signal for printf to stop reading the string; it is only an "output format control character." The entire out-of-bounds read process completes before \n is executed for a newline. This causes the next printf to show overlapping content.

Screenshot 2026-10-29 165356.png

Rough Flow of C Language Compilation and Linking

Editor text file .c source file --- VS cl.exe compile --- machine instructions .obj object file + standard library file .lib --- link --- binary executable .exe

Chinese Garbled Text Problem (Partial)

Screenshot 2026-10-29 170535.png

Screenshot 2026-10-29 170658.png

Root cause: encoding mismatch

VS saves the code as UTF-8 by default, while Windows' cmd console uses GBK by default (code page 936)

Using a GBK dictionary to forcibly decode UTF-8 Chinese characters inevitably produces garbled text

One solution:

At the start of main, force the console encoding to UTF-8:

#include <stdlib.h>

Force switch the console code page to UTF-8, >nul hides the success message

system("chcp 65001 > nul");

Conclusion

This article was written on the afternoon of 2026/10/29, the eve of the third-year internship at a vocational college.

A few days ago I signed up for Bitejiuye (Bit Employment) course, costing over 10,000 RMB, paid in 24 interest-free installments, without telling my parents.

College time passes in a blink; like snow on a wire, things change rapidly.

Lately I feel lost and confused, deeply shaken; past days have been wasted.

Only by seizing the present and learning skills well can I move forward.

Best wishes to all.