这样的C程序合法吗?求救!

来源:百度知道 编辑:UC知道 时间:2024/06/18 13:14:39
#include<stdio.h>
void main()
{
printf("输入你的答案\n");
int a,b;
scanf("%d",&a);
b=abc(a);
printf("答案%d是正确的\n\n\n\n",b);
getchar();
getchar();
}
int abc(int x)
{
int y;
if(x==111)y=x;
else
return(y);
}

我想请问高手,我想用这种方式弄一个程序。但是被调用函数abc是我自己建立出来的,请问这种做法是否合法?
P.S. 我的目的是想当输入答案111的时候,才输出正确答案。
最原始的那种程序我自己已经试过可以了,但是我想知道这种调用函数方法是否也能编出来..

int abc(int x)
{
int y;
if(x==111)y=x;
else
return(y);
}
这里y没有赋初值,你要他传回什么?

而且照你的代码,你输入什么都会输出
printf("答案%d是正确的\n\n\n\n",b); .

你运行一下就知了.

改动了部分地方。
首先在mian函数前面要先声明一下你写的abc函数。
其他改动的地方自己看下,完善了一下你的算法。
#include<stdio.h>

int abc(int x);

void main(void)
{
int a, b;
printf("输入你的答案\n");
scanf("%d", &a);
b = abc(a);
if(b != 0)
{
printf("答案%d是正确的\n\n\n\n", b);
}
else
{
printf("答案错误!");
}
getchar();
getchar();
}

int abc(int x)
{
int y = 0;
if(111 == x)
{
y = x;
return y;
}
else
return y;
}

语法是对的.

但你abc里面的y没有赋值,当x!=111时返回的是个随机值