我只是在C编程中试验代码.并且开始了解一种奇怪的行为.嗯...因为我不是C的专家,所以我不知道它是奇怪的还是正常的.
基本上我的问题是关于以下两行代码之间的区别: -
char a = 'h'; // here variable a is not an array of "char"
Run Code Online (Sandbox Code Playgroud)
和
char a = 'hi'; //here variable a is not an array of "char" as well (i don't know if compiler assumes it as an array or not but , at least i didn't declared it that way )
Run Code Online (Sandbox Code Playgroud)
我使用了以下代码
第一:-
char a =0;
for(;a<'hi';a++)
{
printf("%d= hello world \n",a);
}
Run Code Online (Sandbox Code Playgroud)
第二:-
char a;
for(a='h';a<'hi';a++)
{
printf("%d= hello world \n",a);
}
Run Code Online (Sandbox Code Playgroud)
上面提到的两个循环都在永远运行,
有人可以告诉我为什么这样吗?
我可能会错过一个非常基本的编程概念.请帮帮我们
mya*_*aut 10
那是因为'hi'
有类型int
不是char
.它也解析为值26729.但是最可能的循环变量(假设char
是1字节类型,字节有8位)受127限制,之后溢出.
请注意:
char a =0;
char m = 'hi';
for(; a < m; a++)
{
printf("%d= hello world \n",a);
}
Run Code Online (Sandbox Code Playgroud)
将会工作因为'hi'
将被强制为char(105).
'hi'
是一个多字符的文字.它在编程中并不常见,它是"鲜为人知"的C特征,它成为C99标准的一部分.有关它们的更多信息:http://zipcon.net/~swhite/docs/computers/languages/c_multi-char_const.html
归档时间: |
|
查看次数: |
171 次 |
最近记录: |