ASCII码 字符输出

xiuye1987 2008-11-27 09:16:18
#include<stdio.h>
void main()
{
for(int i=0;i<256;i++)
printf("%s ",i);
}


运行结果:
<null>
然后就跳出提示:
Cpp2.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。
...全文
159 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
季昂 2008-11-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 xiuye1987 的帖子:]
C/C++ code#include<stdio.h>voidmain()
{for(inti=0;i<256;i++)
printf("%s",i);
}

运行结果:
<null>
然后就跳出提示:
Cpp2.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。
[/Quote]

i是0~256之间的数字,printf("%s",i);语句中把char*类型,也就是当作地址了,
你访问0~256之间的地址都是系统禁止访问的,肯定要产生问题了
就呆在云上 2008-11-28
  • 打赏
  • 举报
回复
#include<stdio.h>
void main()
{
for(int i=0;i<256;i++)
printf("%s ",i);
}


格式串的问题,整数是不能用字符串格式输出的,这样:
#include<stdio.h>
void main()
{
for(int i=0;i<256;i++)
printf("%c ",(char)i);
}

或者
#include<stdio.h>
void main()
{
for(int i=0;i<256;i++)
printf("%d ",i);
}

lunarfan 2008-11-28
  • 打赏
  • 举报
回复
i又不是指针%s什么
还有一些不可打印的字符。。。。
xiuye1987 2008-11-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tlw112 的回复:]
C/C++ code
#include<stdio.h>
void main()
{
for(int i=0;i<256;i++)
printf("%c ",i); /* %s ->%c */
}
[/Quote]


還是不行,發出同樣的提示啊
qt_freelancer 2008-11-27
  • 打赏
  • 举报
回复
你那样就输入以i值作为地址的内存内容,而这部分内存你又没有分配,故访问越界!
你应该修改一下你的格式化输出串形式
lzp765 2008-11-27
  • 打赏
  • 举报
回复
你输出单个字符试试
wuyu637 2008-11-27
  • 打赏
  • 举报
回复
printf("%s ",i);

===========
printf("%c ",i);
tlw112 2008-11-27
  • 打赏
  • 举报
回复

#include<stdio.h>
void main()
{
for(int i=0;i<256;i++)
printf("%c ",i); /* %s ->%c */
}

69,382

社区成员

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

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