本人初学者,c语言学习中碰到的6个问题,望大大解惑,谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/17 06:02:09
问题有点多啊,呵呵,谢谢大家的回答啊,分数最少15分,回答的好再加50分恩,谢谢啊。;-)
(1)
#include"stdio.h"
main(){
char a,b;
printf("please input two string:");
a=getchar();
b=getchar();
while(b!=0)
if(a==b)
printf("there are same numble");
else
printf("there are different numble");
getch();
}

输出是对的,就是满屏幕的there are same numble,停都停不下来恩

(2)
#include"stdio.h"
main()
{
int a[3],i,max;
printf("please input 10 numble");
for(i=0;i<=2;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<=2;i++)
if(a[i]>max)
max=a[i];
printf("%d",max);

getch();}

在scanf("%d",&a[i]);的%d后面加个空格,输出就会错误,这是为什么啊?

(3)
#include"stdio.h"
main(){
char st1[30]="my name is";
char st2[10];
printf("what you name? \n");
gets (st2);

1 你的循环条件一直满足,退不出来的
2 scanf的格式化序列中留个空格干吗呢?这样输入流中肯定要多出空格了吧,读入数据因此错误
3 strcat将后一个字符数组拼接至前一个,不是什么赋值。
puts,gets不就是行输入输出吗?
你在st2中输入那么多,最后难道没有截断吗?运行当然可以。
4 仍然是结果截断但可以运行嘛。
5 scanf("%d",&a[i])......

给你的建议:
找一本好书看,那些库函数在bible上都有详细的说明和用法,甚至他们是怎么写出来的。
另:C的很多库函数不会去检测你有意的逻辑错误,所以像那些溢出就会直接截断了。

1.改为如下形式:
#include"stdio.h"
#include <conio.h> //用到了getch()函数,所以建议包含此文件
main(){
char a,b;
printf("please input two string:");

while ((a=getchar())!='\n'&&(a=getchar()))
{
if(a==b)
printf("there are same numble\n");
else
printf("there are different numble\n");

}

getch();
}

2.
如果%d后面有空格,那么在输入时,字符输入完成之后,还应该输入一个对应的空格。

3.strcat是连接字符串的函数,并且将连接后的字符串保存到ST1,gets()函数和puts()函数的参数只能是一个数组名。
输入的字符数大于10时依然可以运行,C会读取前10个字符存入数组。

4.原理同3,请尝试将char st1[1]为char *st1