Ⅰ 关于c语言中会回车键的功能
你说的执行是回车符吧('\r',
0x0d),
在这里你可以无视它.
你在按下回车键时,实际读入输入缓冲区的只有一个换行符('\n',
0x0a)。
scanf("%d",&num[i]);执行后,
你输入数字123,
按回车,输入缓冲区的内容是"123\n".
这时数字被scanf读入,
但是输入缓冲区里还有一个'\n'.
getchar()把'\n'取出,以免影响gets.
而gets在遇到'\n'时会停止读数据,所以如果不用getchar取出'\n',
gets遇到'\n'立即停止,只能读到一个空串,
'\0'.
这个'\0'是gets自己添加到字符串尾的,读不到数据就会有这样一个字符,这个是字符串结束标志,并非读到的字符。
Ⅱ c语言 输入1 2 3 4 5 0回车输出结果为6566456我想问它一步步算的流程
/***********************************************************************
你输入1 2 3 4 5 0,输出结果为6566456
流程:
1.输入1时,程序进入case1中printf打印s+5=6;因为这个case语句没有加break;所以继续往下执行case2中,又打印s+4=5,遇到break退出switch语句.
2.输入2时,程序进入case2中打印s+4=6,遇到break退出switch语句.
3.输入3时,程序进入case3中打印s+3=6;没有遇到break,顺序往下执行default,s+1=4,遇到break退出switch语句.
4.输入4时,switch语句中没有相对应的case,所以执行default,结果s+1=5;
5.输入5时,switch语句中没有相对应的case,所以执行default,结果为s+1=6;
6.输入0退出while循环,因为之前输入数都大于0,所以会循环执行
************************************************************************/
scanf("%d",&s);
while(s>0)
{
switch(s)
{
case 1: printf("%d",s+5);
case 2: printf("%d",s+4);break;
case 3: printf("%d",s+3);
default: printf("%d",s+1);break;
}
scanf("%d",&s);
}
Ⅲ C语言,键盘输入123 456 8回车 然后 为什么输出的b和pow()函数都等于零呢
不可能是你说的那样子:b不会是0;pow结果是0是因为输出函数错用了%d,改为printf("pow(%d,%d)=%f ",d,i,pow(d,i));就可以了……看图片:
Ⅳ c语言中多个scanf怎么输入
1、首先在打开的C语言中,写上注释内容,然后定义一个枚举,enum 枚举的名称,如下图所示。
Ⅳ 计算机C语言单个字符输入输出问题
scanf不对回车进行处理,它是以空格作为字符输入的结束。 而getchar()会对空格进行处理,以回车作为输入结束。
你输入123回车,回车符被c4接收,c5,c6接收4,5
结果为:
正确输入方法: 123456
Ⅵ C语言中 若从键盘上输入123*并以回车键结束 这句表示什么意思
看的时候注意下,break 语句,有它才结束,没有顺序执行
Ⅶ C语言怎么解决scanf()把回车作为输入值的问题
scanf()是不会把回车拷贝到字符窜里面的。
这里是一段英文定义:the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- seeisspace).
除了回车,空格也不会拷贝到字符窜中。
再来看下gets(),英文定义是这样的:Reads characters from thestandard input(stdin) and stores them as a C string intostruntil a newline character or theend-of-fileis reached.The newline character, if found, is not copied intostr.
好了。这里写的很清楚了。gets()虽然可以把输入都拷贝到字符窜里,比如空格,但是不包含回车。
如果需要回车,可以用fgets()。英文是这样定义的:Reads characters fromstreamand stores them as a C string intostruntil (num-1) characters have been read or either a newline or theend-of-fileis reached, whichever happens first. A newline character makesfgetsstop reading, but it is considered a valid character by the function and included in the string copied tostr.
最后一句话说了,回车作为结束,不过会拷贝到字符窜中。
那么文档说的对不对呢?写一段代码测试一下。
#include<stdio.h>
intmain()
{
inti=0;
printf("scanf... ");
charscanf_content[256]={0};
scanf("%s",scanf_content);
printf("value:%s ",scanf_content);
while(scanf_content[i])
{
if(scanf_content[i]==' ')
printf("\n");
else
printf("%d ",(int)scanf_content[i]);
++i;
}
i=0;
printf("gets... ");
chargets_content[256]={0};
gets(gets_content);//unsafe
printf("value:%s ",gets_content);
while(gets_content[i])
{
if(gets_content[i]==' ')
printf("\n");
else
printf("%d ",(int)gets_content[i]);
++i;
}
i=0;
printf("fgets... ");
charfgets_content[256]={0};
fgets(fgets_content,256,stdin);
printf("value:%s ",fgets_content);
while(fgets_content[i])
{
if(fgets_content[i]==' ')
printf("\n");
else
printf("%d ",(int)fgets_content[i]);
++i;
}
return0;
}
输入“123 123”,你会发现scanf只会得到123,而gets可以得到空格123。最后fgets可以得到' '。这里为了看到空格和回车,可以把字符窜转成int打印出来。
最后的结论就是,如果需要回车,就使用fgets。
Ⅷ c语言....
你好
应该是
c1='1'
c2='2'
c3='3'
c4
='\n'
c5='4'
c6='5'
scanf()读入字符时,回车也被当做字符读入
所以c4
='\n'
c5=getchar();c6=getchar();
读入的是下面的4
5
希望对你有帮助啊
Ⅸ c语言,二维数组,输入:1 2 3回车,求输出结果
for(i=0;i<2;i++)
{ ptr=a+i;
scanf("%d",ptr);ptr++;
}
//这个for循环是给二维数组的第0列赋值,但是只接受1 2 前两个值
所以数组输出的结果是
1 0
2 0
0 0