请问,以上程序为何不能输出汉字

m0_51507696 2021-05-11 08:01:01
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(void){
int dat=0;
while(1){
dat=getch();
//printf("%d\n\n ",dat);
printf("%c%c",((dat&0xff00)>>8),(dat&0x00ff));
}
system("pause");
return 1;
}

请问,以上程序为何不能输出汉字
...全文
874 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Isnis-fallen 2021-06-27
  • 打赏
  • 举报
回复

汉字应该用字符串保存,你怎么用int

luj_1768 2021-05-17
  • 打赏
  • 举报
回复
现在程序可以这么写了啊? 请问:1. getch(); 的返回值范围? 2. printf(“%c%c”,a,b); 的输出内容? 3. 你能不能解释一下a=(dat&0xff00)>>8;和b=dat&0x00ff; 的可能输出结果? 4. 如果输出汉字,编码应该是16位字长,但是那么多编码方案你准备为哪种编程? 5. 如果你不准备想清楚编码和算法有关的事,至少你应该研究一下把两个%c数据合成一个16位字长的数据吧?不然你怎么输出中文?
均陵鼠侠 2021-05-15
  • 打赏
  • 举报
回复
赵4老湿不老实!没有正面回答我滴问题!差评!中午不准吃饭! 实际上, wprintf 是将宽字符转换为多字节字符输出。正因为有一个转换过程,等价于 wcstomb,所以需要根据当前区域设置来决定如何将宽字符转换为多字节字符。
赵4老师 2021-05-14
  • 打赏
  • 举报
回复
setlocale, _wsetlocale Define the locale. char *setlocale( int category, const char *locale ); wchar_t *_wsetlocale( int category, const wchar_t *locale ); Routine Required Header Compatibility setlocale <locale.h> ANSI, Win 95, Win NT _wsetlocale <locale.h> or <wchar.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value If a valid locale and category are given, the function returns a pointer to the string associated with the specified locale and category. If the locale or category is invalid, the function returns a null pointer and the current locale settings of the program are not changed. For example, the call setlocale( LC_ALL, "English" ); sets all categories, returning only the string English_USA.1252. If all categories are not explicitly set by a call to setlocale, the function returns a string indicating the current setting of each of the categories, separated by semicolons. If the locale argument is a null pointer, setlocale returns a pointer to the string associated with the category of the program’s locale; the program’s current locale setting is not changed. The null pointer is a special directive that tells setlocale to query rather than set the international environment. For example, the sequence of calls // Set all categories and return "English_USA.1252" setlocale( LC_ALL, "English" ); // Set only the LC_MONETARY category and return "French_France.1252" setlocale( LC_MONETARY, "French" ); setlocale( LC_ALL, NULL ); returns LC_COLLATE=English_USA.1252; LC_CTYPE=English_USA.1252; LC_MONETARY=French_France.1252; LC_NUMERIC=English_USA.1252; LC_TIME=English_USA.1252 which is the string associated with the LC_ALL category. You can use the string pointer returned by setlocale in subsequent calls to restore that part of the program’s locale information, assuming that your program does not alter the pointer or the string. Later calls to setlocale overwrite the string; you can use _strdup to save a specific locale string. Parameters category Category affected by locale locale Locale name Remarks Use the setlocale function to set, change, or query some or all of the current program locale information specified by locale and category. “Locale” refers to the locality (country and language) for which you can customize certain aspects of your program. Some locale-dependent categories include the formatting of dates and the display format for monetary values. _wsetlocale is a wide-character version of setlocale; the locale argument and return value of _wsetlocale are wide-character strings. _wsetlocale and setlocale behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tsetlocale setlocale setlocale _wsetlocale The category argument specifies the parts of a program’s locale information that are affected. The macros used for category and the parts of the program they affect are as follows: LC_ALL All categories, as listed below LC_COLLATE The strcoll, _stricoll, wcscoll, _wcsicoll, and strxfrm functions LC_CTYPE The character-handling functions (except isdigit, isxdigit, mbstowcs, and mbtowc, which are unaffected) LC_MONETARY Monetary-formatting information returned by the localeconv function LC_NUMERIC Decimal-point character for the formatted output routines (such as printf), for the data-conversion routines, and for the nonmonetary-formatting information returned by localeconv LC_TIME The strftime and wcsftime functions The locale argument is a pointer to a string that specifies the name of the locale. If locale points to an empty string, the locale is the implementation-defined native environment. A value of “C” specifies the minimal ANSI conforming environment for C translation. The “C” locale assumes that all char data types are 1 byte and that their value is always less than 256. The “C” locale is the only locale supported in Microsoft Visual C++ version 1.0 and earlier versions of Microsoft C/C++. Microsoft Visual C++ supports all the locales listed in Appendix A, Language and Country Strings. At program startup, the equivalent of the following statement is executed: setlocale( LC_ALL, "C" ); The locale argument takes the following form: locale :: "lang[_country[.code_page]]" | ".code_page" | "" | NULL The set of available languages, countries, and code pages includes all those supported by the Win32 NLS API. The set of language and country codes supported by setlocale is listed in Appendix A, Language and Country Strings. If locale is a null pointer, setlocale queries, rather than sets, the international environment, and returns a pointer to the string associated with the specified category. The program’s current locale setting is not changed. For example, setlocale( LC_ALL, NULL ); returns the string associated with category. The following examples pertain to the LC_ALL category. Either of the strings ".OCP" and ".ACP" can be used in place of a code page number to specify use of the system default OEM code page and system-default ANSI code page, respectively. setlocale( LC_ALL, "" ); Sets the locale to the default, which is the system-default ANSI code page obtained from the operating system. setlocale( LC_ALL, ".OCP" ); Explicitly sets the locale to the current OEM code page obtained from the operating system. setlocale( LC_ALL, ".ACP" ); Sets the locale to the ANSI code page obtained from the operating system. setlocale( LC_ALL, "[lang_ctry]" ); Sets the locale to the language and country indicated, using the default code page obtained from the host operating system. setlocale( LC_ALL, "[lang_ctry.cp]" ); Sets the locale to the language, country, and code page indicated in the [lang_ctry.cp] string. You can use various combinations of language, country, and code page. For example: setlocale( LC_ALL, "French_Canada.1252" ); // Set code page to French Canada ANSI default setlocale( LC_ALL, "French_Canada.ACP" ); // Set code page to French Canada OEM default setlocale( LC_ALL, "French_Canada.OCP" ); setlocale( LC_ALL, "[lang]" ); Sets the locale to the country indicated, using the default country for the language specified, and the system-default ANSI code page for that country as obtained from the host operating system. For example, the following two calls to setlocale are functionally equivalent: setlocale( LC_ALL, "English" ); setlocale( LC_ALL, "English_United States.1252" ); setlocale( LC_ALL, "[.code_page]" ); Sets the code page to the value indicated, using the default country and language (as defined by the host operating system) for the specified code page. The category must be either LC_ALL or LC_CTYPE to effect a change of code page. For example, if the default country and language of the host operating system are “United States” and “English,” the following two calls to setlocale are functionally equivalent: setlocale( LC_ALL, ".1252" ); setlocale( LC_ALL, "English_United States.1252"); For more information see the setlocale pragma in Preprocessor Reference. Example /* LOCALE.C: Sets the current locale to "Germany" using the * setlocale function and demonstrates its effect on the strftime * function. */ #include <stdio.h> #include <locale.h> #include <time.h> void main(void) { time_t ltime; struct tm *thetime; unsigned char str[100]; setlocale(LC_ALL, "German"); time (<ime); thetime = gmtime(<ime); /* %#x is the long date representation, appropriate to * the current locale */ if (!strftime((char *)str, 100, "%#x", (const struct tm *)thetime)) printf("strftime failed!\n"); else printf("In German locale, strftime returns '%s'\n", str); /* Set the locale back to the default environment */ setlocale(LC_ALL, "C"); time (<ime); thetime = gmtime(<ime); if (!strftime((char *)str, 100, "%#x", (const struct tm *)thetime)) printf("strftime failed!\n"); else printf("In 'C' locale, strftime returns '%s'\n", str); } Output In German locale, strftime returns 'Donnerstag, 22. April 1993' In 'C' locale, strftime returns 'Thursday, April 22, 1993' Locale Routines See Also localeconv, mblen, _mbstrlen, mbstowcs, mbtowc, strcoll Functions, strftime, strxfrm, wcstombs, wctomb
赵4老师 2021-05-14
  • 打赏
  • 举报
回复
引用 11 楼 均陵鼠侠 的回复:
[quote=引用 7 楼 m0_51507696 的回复:][quote=引用 3 楼 赵4老师 的回复:]getwchar可以返回汉字
老师,您看一下,我将程序改成了这样,结果输出如图那样您觉得有可能是什么原因 #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <wchar.h> int main(void){ int dat=0; while(1){ printf("输入:"); dat=getwchar(); printf("%d——",dat); printf("%c%c",((dat&0xff00)>>8),(dat&0x00ff)); } system("pause"); return 1; } [/quote] 大胆!你这是为难赵4老湿。 我来替赵4老湿回答你的问题。你只需要用下面的方法即可解决:
# include <locale.h>
# include <wchar.h>
# include <stdio.h>

int main (void)
{
    setlocale (LC_ALL, "chs");
    wprintf (L"->%lc", getwchar ());
}
注:在Linux环境下,程序中的“chs”要改成“zh_CN.UTF-8”(如果默认是UTF-8编码)。 帮了赵4老湿这么大的忙,请赵老湿回答一下,为什么这个程序要用 setlocale 设置区域呢?如果不设置有什么问题?[/quote] https://www.baidu.com/s?wd=setlocale%20LC_ALL%20chs%20zh_CN.UTF-8
六道佩恩 2021-05-14
  • 打赏
  • 举报
回复
来,按你的意思改一波

#include<stdio.h>
#include<conio.h>
int main( void )
{
    int a=0, b=0;
    while(1)
    {
    	a=getch();
    	printf("%c", a );
    }
    return 0; 
} 
注意,getch不回显,这个“哈哈哈”是printf输出来的
六道佩恩 2021-05-14
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<conio.h>
int main( void )
{
    int a=0, b=0;
    a=getch();
    b=getch();
    printf("得到%c%c %x %x", a, b, a, b );
    return 0; 
} 
这不是可行吗,B9 FE,这就是汉字'哈'的GBK编码呀
均陵鼠侠 2021-05-14
  • 打赏
  • 举报
回复
引用 7 楼 m0_51507696 的回复:
[quote=引用 3 楼 赵4老师 的回复:]getwchar可以返回汉字
老师,您看一下,我将程序改成了这样,结果输出如图那样您觉得有可能是什么原因 #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <wchar.h> int main(void){ int dat=0; while(1){ printf("输入:"); dat=getwchar(); printf("%d——",dat); printf("%c%c",((dat&0xff00)>>8),(dat&0x00ff)); } system("pause"); return 1; } [/quote] 大胆!你这是为难赵4老湿。 我来替赵4老湿回答你的问题。你只需要用下面的方法即可解决:
# include <locale.h>
# include <wchar.h>
# include <stdio.h>

int main (void)
{
    setlocale (LC_ALL, "chs");
    wprintf (L"->%lc", getwchar ());
}
注:在Linux环境下,程序中的“chs”要改成“zh_CN.UTF-8”(如果默认是UTF-8编码)。 帮了赵4老湿这么大的忙,请赵老湿回答一下,为什么这个程序要用 setlocale 设置区域呢?如果不设置有什么问题?
均陵鼠侠 2021-05-14
  • 打赏
  • 举报
回复
引用 9 楼 m0_51507696 的回复:
[quote=引用 6 楼 均陵鼠侠 的回复:]汉字处理是个复杂的话题,但是就你这个程序而言,又不是太难。
# include <stdio.h>

int main (void)
{
    char b [2048] = {0};
    int c, x = 0;

    while ((c = getchar ()) != EOF) b [x ++] = c;

    printf (b);
}
函数 getchar 每次只读取 1 个字节,然后扩展到 int 类型。所以,我们的任务是将这些字节依次读取并存储起来。这个程序的好处是不必考虑操作系统实际使用的执行字符集,因为发送给程序的字符编码与系统输出(即 printf 内部调用的操作系统接口部分)使用的字符编码方案总是一致的。
请问,这个printf (b);是怎么回事,没有格式控制字符串编译器会自动判断输出类型吗[/quote] 因为 printf 本来就是一个变参函数,第一个参数是指向字符串的指针,如果里面有格式控制字符,后面才会有别的参数:
int printf (const char * restrict, ...);
m0_51507696 2021-05-14
  • 打赏
  • 举报
回复
引用 6 楼 均陵鼠侠 的回复:
汉字处理是个复杂的话题,但是就你这个程序而言,又不是太难。
# include <stdio.h>

int main (void)
{
    char b [2048] = {0};
    int c, x = 0;

    while ((c = getchar ()) != EOF) b [x ++] = c;

    printf (b);
}
函数 getchar 每次只读取 1 个字节,然后扩展到 int 类型。所以,我们的任务是将这些字节依次读取并存储起来。这个程序的好处是不必考虑操作系统实际使用的执行字符集,因为发送给程序的字符编码与系统输出(即 printf 内部调用的操作系统接口部分)使用的字符编码方案总是一致的。
请问,这个printf (b);是怎么回事,没有格式控制字符串编译器会自动判断输出类型吗
m0_51507696 2021-05-14
  • 打赏
  • 举报
回复
引用 6 楼 均陵鼠侠 的回复:
汉字处理是个复杂的话题,但是就你这个程序而言,又不是太难。
# include <stdio.h>

int main (void)
{
    char b [2048] = {0};
    int c, x = 0;

    while ((c = getchar ()) != EOF) b [x ++] = c;

    printf (b);
}
函数 getchar 每次只读取 1 个字节,然后扩展到 int 类型。所以,我们的任务是将这些字节依次读取并存储起来。这个程序的好处是不必考虑操作系统实际使用的执行字符集,因为发送给程序的字符编码与系统输出(即 printf 内部调用的操作系统接口部分)使用的字符编码方案总是一致的。
引用 6 楼 均陵鼠侠 的回复:
汉字处理是个复杂的话题,但是就你这个程序而言,又不是太难。
# include <stdio.h>

int main (void)
{
    char b [2048] = {0};
    int c, x = 0;

    while ((c = getchar ()) != EOF) b [x ++] = c;

    printf (b);
}
函数 getchar 每次只读取 1 个字节,然后扩展到 int 类型。所以,我们的任务是将这些字节依次读取并存储起来。这个程序的好处是不必考虑操作系统实际使用的执行字符集,因为发送给程序的字符编码与系统输出(即 printf 内部调用的操作系统接口部分)使用的字符编码方案总是一致的。
谢谢您,您的回答解决了我的问题
m0_51507696 2021-05-14
  • 打赏
  • 举报
回复
引用 3 楼 赵4老师 的回复:
getwchar可以返回汉字
老师,您看一下,我将程序改成了这样,结果输出如图那样您觉得有可能是什么原因 #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <wchar.h> int main(void){ int dat=0; while(1){ printf("输入:"); dat=getwchar(); printf("%d——",dat); printf("%c%c",((dat&0xff00)>>8),(dat&0x00ff)); } system("pause"); return 1; }
源代码大师 2021-05-11
  • 打赏
  • 举报
回复
希望对您有帮助,盼采纳:https://blog.csdn.net/it_xiangqiang/category_10581430.html
赵4老师 2021-05-11
  • 打赏
  • 举报
回复
getwchar可以返回汉字
自信男孩 2021-05-11
  • 打赏
  • 举报
回复
汉字不是占1个字节的字符(可以了解一下一个汉子占几个字节),之所以getch返回int就像getchar()返回int是一样的道理:类型提升;

因此如果要输出汉子,建议用字符串;

赵4老师 2021-05-11
  • 打赏
  • 举报
回复
因为getch()返回不了汉字对应的GBK或UTF8或Unicode编码。
均陵鼠侠 2021-05-11
  • 打赏
  • 举报
回复
汉字处理是个复杂的话题,但是就你这个程序而言,又不是太难。
# include <stdio.h>

int main (void)
{
    char b [2048] = {0};
    int c, x = 0;

    while ((c = getchar ()) != EOF) b [x ++] = c;

    printf (b);
}
函数 getchar 每次只读取 1 个字节,然后扩展到 int 类型。所以,我们的任务是将这些字节依次读取并存储起来。这个程序的好处是不必考虑操作系统实际使用的执行字符集,因为发送给程序的字符编码与系统输出(即 printf 内部调用的操作系统接口部分)使用的字符编码方案总是一致的。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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