Study notes for printf's wide and narrow character output

I have a problem when writing code

LPSTR a1 = "abc";
	printf("窄字符abc=%s,size=%llu\n", a1,sizeof("abc"));
	LPSTR a2 = "我是";
	printf("窄字符我是=%s,size=%llu\n", a2, sizeof("我是"));
	LPWSTR a3 = L"abc";
	printf("宽字符abc=%ls,size=%llu\n", a3, sizeof(L"abc"));
	LPWSTR a4 = L"我是";
	printf("宽字符我是=%ls,size=%llu\n", a4, sizeof(L"我是"));
	LPSTR a5 = "123";
	printf("窄字符123=%s,size=%llu\n", a5, sizeof("123"));
	LPWSTR a6 = L"123";
	printf("宽字符123=%ls,size=%llu\n", a6, sizeof(L"123"));

output result

窄字符abc=abc,size=4
窄字符我是=我是,size=5
宽字符abc=abc,size=8
宽字符我是=窄字符123=123,size=4
宽字符123=123,size=8

The fourth output does not match expectations, the output result is (wide character I am =), other output is normal

Three questions arise,
1. Since Windows has two encodings, unicode and ansi, why can ansi encoding transmit Chinese character strings normally?
2. Why does wide character output fail?
3. What is the difference between unicode and UTF-8? Then why do many programming tutorials say that it is unicode encoding instead of UTF-8 encoding? Because there is actually another encoding called UTF-16 encoding, which is a fixed-length encoding. All symbols are two bytes. UTF-16 encoding is used on windows. etails / 71642234






https://blog.csdn.net/liaowenxiong/article/details/123971677

Guess you like

Origin blog.csdn.net/sanqiuai/article/details/124342680