如何用c++输出汉字?

infinitefff 2010-11-08 08:20:27
程序如下:
#include <iostream>
using namespace std;
int main()
{
WCHAR a='天'
cout<<a;
}
在VC6.0中运行报错:
error C2065: 'WCHAR' : undeclared identifier

请问是不是缺少什么头文件啊?
另外,有人说在WIN32中char也占十六位,为什么我用sizeof(char)得出的结果是1呢?

...全文
1943 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xspace_time 2010-11-09
  • 打赏
  • 举报
回复
#include<iostream>
#include<locale>
#include<String.h>
using namespace std;
void main()
{
setlocale(LC_ALL,"chs");
string str("中");
cout<<str<<endl;
}
"中"是三个char字节的指针类型,C语言不用指针就不行了,用C++其实还是指针,只不过看起来习惯点罢了
infinitefff 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cqsjwj 的回复:]

#include<iostream>
#include<locale>
using namespace std;
void main()
{
setlocale(LC_ALL,"chs");
wchar_t aa[]=L"中";
wcout<<aa<<endl;
}
[/Quote]
这个可以正确输出,但是还是用的首地址,没有使用字符变量输出啊
HELLO345T 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cqsjwj 的回复:]
#include<iostream>
#include<locale>
using namespace std;
void main()
{
setlocale(LC_ALL,"chs");
wchar_t aa[]=L"中";
wcout<<aa<<endl;
}
[/Quote]

同意~~~
Homedaddy 2010-11-08
  • 打赏
  • 举报
回复
wchar 才占用2个字符

一般我们使用 TCHAR 来表示字符, 这样它会自动定义为 char 或者 wchar (根据你选择的 字符集决定的
看你是 选择的是 多字符集 还是 unicode 字符集


不过得加头文件: #include <tchar.h>

TCHAR a=_T('天');

_tprintf(_T("%s"), a );

所以建议使用 tchar 类型,而不是使用 wchar, (要不然你会在多字符集的平台下出错!)
vagent 2010-11-08
  • 打赏
  • 举报
回复
char 本来就占1个字符

wchar 才占用2个字符

一般我们使用 TCHAR 来表示字符, 这样它会自动定义为 char 或者 wchar (根据你选择的 字符集决定的
看你是 选择的是 多字符集 还是 unicode 字符集


不过得加头文件: #include <tchar.h>

TCHAR a=_T('天');

_tprintf(_T("%s"), a );

所以建议使用 tchar 类型,而不是使用 wchar, (要不然你会在多字符集的平台下出错!)
cqsjwj 2010-11-08
  • 打赏
  • 举报
回复
#include<iostream>
#include<locale>
using namespace std;
void main()
{
setlocale(LC_ALL,"chs");
wchar_t aa[]=L"中";
wcout<<aa<<endl;
}
infinitefff 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 frankhb1989 的回复:]

wchar_t a = L'天';
wcout << a;
[/Quote]
这样输出的是一段数字
infinitefff 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cdsxyx 的回复:]

winnt.h
[/Quote]
这样依旧报错,错误是:
c:\program files\microsoft visual studio\vc98\include\winnt.h(152) : error C2146: syntax error : missing ';' before identifier 'WCHAR'
infinitefff 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 csuxiaowu 的回复:]

你要这样输出汉字直接用char *就ok了嘛
谁说的?
[/Quote]
这样是可以,但我是想找一个可以存储汉字的字符变量
FrankHB1989 2010-11-08
  • 打赏
  • 举报
回复
wchar_t a = L'天';
wcout << a;
infinitefff 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 healer_kx 的回复:]
wcout<<L"给分不?";
[/Quote]
wcout是怎么用的?我如果不加L则输出一段十六进制数,加L则什么也不输出了
半斗 2010-11-08
  • 打赏
  • 举报
回复
winnt.h
Csuxiaowu 2010-11-08
  • 打赏
  • 举报
回复
你要这样输出汉字直接用char *就ok了嘛
谁说的?
healer_kx 2010-11-08
  • 打赏
  • 举报
回复


wcout<<L"给分不?";

64,660

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧