关于字符常量的几个问题

q245413856 2012-11-13 07:13:52
注册了好久,也没咋看过问过。现在有些问题想请教大家,希望大家帮忙:
1、比较具体:
char c;
c='*';或者c=‘\*’;
c的值是一样的,为什么明明多了"\"还一样,其他字符,诸如字符?,也是无论加不加"\",值都是一样的。
2、一般情况下(或者大多数情况),定义一个字符是用int好还是char好?
...全文
188 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-11-14
  • 打赏
  • 举报
回复
C++ Character Constants Character constants are one or more members of the “source character set,” the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the “execution character set,” the character set on the machine where the program executes. Microsoft Specific For Microsoft C++, the source and execution character sets are both ASCII. END Microsoft Specific There are three kinds of character constants: Normal character constants Multicharacter constants Wide-character constants Note Use wide-character constants in place of multicharacter constants to ensure portability. Character constants are specified as one or more characters enclosed in single quotation marks. For example: char ch = 'x'; // Specify normal character constant. int mbch = 'ab'; // Specify system-dependent // multicharacter constant. wchar_t wcch = L'ab'; // Specify wide-character constant. Note that mbch is of type int. If it were declared as type char, the second byte would not be retained. A multicharacter constant has four meaningful characters; specifying more than four generates an error message. Syntax character-constant : 'c-char-sequence' L'c-char-sequence' c-char-sequence : c-char c-char-sequence c-char c-char : any member of the source character set except the single quotation mark ('), backslash (\), or newline character escape-sequence escape-sequence : simple-escape-sequence octal-escape-sequence hexadecimal-escape-sequence simple-escape-sequence : one of \' \" \? \\ \a \b \f \n \r \t \v octal-escape-sequence : \octal-digit \octal-digit octal-digit \octal-digit octal-digit octal-digit hexadecimal-escape-sequence : \xhexadecimal-digit hexadecimal-escape-sequence hexadecimal-digit Microsoft C++ supports normal, multicharacter, and wide-character constants. Use wide-character constants to specify members of the extended execution character set (for example, to support an international application). Normal character constants have type char, multicharacter constants have type int, and wide-character constants have type wchar_t. (The type wchar_t is defined in the standard include files STDDEF.H, STDLIB.H, and STRING.H. The wide-character functions, however, are prototyped only in STDLIB.H.) The only difference in specification between normal and wide-character constants is that wide-character constants are preceded by the letter L. For example: char schar = 'x'; // Normal character constant wchar_t wchar = L'\x81\x19'; // Wide-character constant Table 1.2 shows reserved or nongraphic characters that are system dependent or not allowed within character constants. These characters should be represented with escape sequences. Table 1.2 C++ Reserved or Nongraphic Characters Character ASCII Representation ASCII Value Escape Sequence Newline NL (LF) 10 or 0x0a \n Horizontal tab HT 9 \t Vertical tab VT 11 or 0x0b \v Backspace BS 8 \b Carriage return CR 13 or 0x0d \r Formfeed FF 12 or 0x0c \f Alert BEL 7 \a Backslash \ 92 or 0x5c \\ Question mark ? 63 or 0x3f \? Single quotation mark ' 39 or 0x27 \' Double quotation mark " 34 or 0x22 \" Octal number ooo — \ooo Hexadecimal number hhh — \xhhh Null character NUL 0 \0 If the character following the backslash does not specify a legal escape sequence, the result is implementation defined. In Microsoft C++, the character following the backslash is taken literally, as though the escape were not present, and a level 1 warning (“unrecognized character escape sequence”) is issued. Octal escape sequences, specified in the form \ooo, consist of a backslash and one, two, or three octal characters. Hexadecimal escape sequences, specified in the form \xhhh, consist of the characters \x followed by a sequence of hexadecimal digits. Unlike octal escape constants, there is no limit on the number of hexadecimal digits in an escape sequence. Octal escape sequences are terminated by the first character that is not an octal digit, or when three characters are seen. For example: wchar_t och = L'\076a'; // Sequence terminates at a char ch = '\233'; // Sequence terminates after 3 characters Similarly, hexadecimal escape sequences terminate at the first character that is not a hexadecimal digit. Because hexadecimal digits include the letters a through f (and A through F), make sure the escape sequence terminates at the intended digit. Because the single quotation mark (') encloses character constants, use the escape sequence \' to represent enclosed single quotation marks. The double quotation mark (") can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a character constant, you must type two backslashes in a row (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.)
netimmortal 2012-11-14
  • 打赏
  • 举报
回复
因为 * 和 \* 其实是一样的。。 \是用于一些不能显示的字符的,比如\r换行之类的
guest123000 2012-11-14
  • 打赏
  • 举报
回复
因为\用在那里没什么实际意义,所以两个结果都是一样的,在实际编程中也没人会那样用的。 在定义字符变量时肯定是用char好,int是整型变量,不能定义字符型的。
super_01 2012-11-14
  • 打赏
  • 举报
回复
c语言中有些字符属于不可打印字符,不可打印的字符实际上是不可显示的字符,如退格符,回车符等。还有一些在语言中比较特殊的字符,如单双引号,星号等。不可打印字符或者特殊字符都要用转义字符来表示,转义字符都以反斜线开始表示。 楼主可以理解‘*’和‘\*’是同一个字符用在不同场合的不同表示形式,但是‘\*’貌似并没有实际的意义,所以其对应的sacii吗和‘*’一样。然而‘\n’有明确的意义,所以其对应的ascii的吗值和‘n’是不一样。 反斜杠只是将同一个字符用不同的形式表示出来以适应不同的场合,当加了反斜杠的字符其他的意义时,其对应的ascii值和原字符对应的值会有所不同。 个人理解
赵4老师 2012-11-14
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 有人说一套做一套,你相信他说的还是相信他做的? 其实严格来说这个世界上古往今来所有人都是说一套做一套,不是吗? 不要写连自己也预测不了结果的代码!
ldxab 2012-11-14
  • 打赏
  • 举报
回复
\是转义字符,用来处理一些特别含义字符 一个字节能盛放的一般都用一个字节
飞天御剑流 2012-11-13
  • 打赏
  • 举报
回复
引用 楼主 q245413856 的回复:
注册了好久,也没咋看过问过。现在有些问题想请教大家,希望大家帮忙: 1、比较具体: char c; c='*';或者c=‘\*’; c的值是一样的,为什么明明多了"\"还一样,其他字符,诸如字符?,也是无论加不加"\",值都是一样的。 2、一般情况下(或者大多数情况),定义一个字符是用int好还是char好?
1. '\*'叫多字节字符,类型属于int,而对于c='\*'这样的行为,结果是把'\*'的LSB赋予c。 2. 这个不用说什么了吧,对于字符,当然是char。
q245413856 2012-11-13
  • 打赏
  • 举报
回复
引用 1 楼 zhuankeshumo 的回复:
百度转义字符 百科里讲的很清楚 定义字符当然是char
百科之,全文也没有与我问的问题对上号的回答,麻烦再解释解释。 基本语法是得看看,我看的是《c程序设计语言(第二版)》,这书应该是不抓细节的,所以直接求助了
jixingzhong 2012-11-13
  • 打赏
  • 举报
回复
可以好好看看基本语法。
转角天边 2012-11-13
  • 打赏
  • 举报
回复
引用 1 楼 zhuankeshumo 的回复:
百度转义字符 百科里讲的很清楚 定义字符当然是char
+1
newtee 2012-11-13
  • 打赏
  • 举报
回复
百度转义字符 百科里讲的很清楚 定义字符当然是char

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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