求教C高手

jasmine 2011-03-01 08:52:06
#include <stdio.h>
void main()
{
char c1='a',c2='b',c3='c',c4='\101',c5='\116';
printf("a%cb%c\tc%c\tabc\n",c1,c2,c3);
printf("\tb%c%c\n",c4,c5);
system ("pause");
}
解释一哈第二个printf输出结果的由来
...全文
171 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wayne-Woo 2011-03-02
  • 打赏
  • 举报
回复
c4='\101',c5='\116'表示八进制的数
将其转换成十进制:
101:1*8^2 + 0*8^1 + 1*8^0 = 65 而65代表的是字符‘A’的ASCII码的值;
116:1*8^2 + 1*8^1 + 6*8^0 = 78 而78代表的是字符‘N’的ASCII码的值;
所以printf("\tb%c%c\n",c4,c5);输出的就是字符AN;
rach09 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]

8进制转义字符。转换成ascii码值是65和78对应的字符时A和N
[/Quote]
最明确的解释
「已注销」 2011-03-02
  • 打赏
  • 举报
回复
The final two forms (\0oo and \xhh) are special representations of the ASCII code. To represent a character by its octal ASCII code, precede it with a backslash (\) and enclose the whole thing in single quotes. For example, if your compiler doesn't recognize the alert character (\a), you could use the ASCII code instead:

beep = '\007';

You can omit the leading zeros, so '\07' or even '\7' will do. This notation causes numbers to be interpreted as octal, even if there is no initial 0.

elegant87 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]

8进制转义字符。转换成ascii码值是65和78对应的字符时A和N
[/Quote]
正解
赵4老师 2011-03-02
  • 打赏
  • 举报
回复
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

flysnowhite 2011-03-02
  • 打赏
  • 举报
回复
转义字符。
delphiwcdj 2011-03-02
  • 打赏
  • 举报
回复
c4='\101',c5='\116'
这种格式是转义,表示八进制。例如,c4 == 64+1 == A的ascii,因此输出A
wizard_tiger 2011-03-02
  • 打赏
  • 举报
回复
将一个八进制ASCII码转换成字符输出。
wxwlll 2011-03-02
  • 打赏
  • 举报
回复
\t是制表符,b会原样输出
jinzhou520 2011-03-02
  • 打赏
  • 举报
回复
见所未见闻所未闻. 长见识了.
jackon1990 2011-03-01
  • 打赏
  • 举报
回复
这是八进制表示ASCII码,拿c4来说101(八进制)='65'=A,明白不?字符的另一种表示方式,我个人感觉这个不常用,呵呵,欢迎交流
MSOKD 2011-03-01
  • 打赏
  • 举报
回复
printf("\tb%c%c\n",c4,c5);

里多了个b
c4,c5的值是转义字符
所以输出AN
pengzhixi 2011-03-01
  • 打赏
  • 举报
回复
8进制转义字符。转换成ascii码值是65和78对应的字符时A和N

70,020

社区成员

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

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