无法使用scanf()将参数传递给函数
我无法使用无法使用scanf()将参数传递给函数,c,C,我无法使用scanf()将参数传递给函数 我收到未声明的错误 有办法解决这个问题吗?甚至可能吗?您需要intx,y在main()中,这样就在堆栈上分配了本地作用域(查找!)变量,以便将指针传递到scanf()在scanf中使用变量x和y时,必须先声明它,然后才能在scanf中使用它 #include<stdio.h> //ADD Function void add(int x, int y){ int result = x + y; printf("%d + %d = %d\n",
scanf()
将参数传递给函数
我收到未声明的错误
有办法解决这个问题吗?甚至可能吗?您需要intx,y代码>在main()
中,这样就在堆栈上分配了本地作用域(查找!)变量,以便将指针传递到scanf()
在scanf中使用变量x和y时,必须先声明它,然后才能在scanf中使用它
#include<stdio.h>
//ADD Function
void add(int x, int y){
int result = x + y;
printf("%d + %d = %d\n", x, y, result);
}
int main(void)
{
int x;
int y;
printf("Enter The First Number: ");
scanf("%d", &x);
printf("Enter The Second Number: ");
scanf("%d", &y);
add(x, y);
return 0;
}
#包括
//添加函数
无效添加(整数x,整数y){
int结果=x+y;
printf(“%d+%d=%d\n”,x,y,结果);
}
内部主(空)
{
int x;
int-y;
printf(“输入第一个数字:”);
scanf(“%d”和&x);
printf(“输入第二个数字:”);
scanf(“%d”和“&y”);
加(x,y);
返回0;
}
scanf需要参数的地址来更改值:scanf(“%d”,&x);你还需要声明和定义x和yOh抱歉地址不是这样的。我只是忘了把它放在stackoverflow代码里。我已经在add()函数中声明了它们。我需要创建一个全局变量吗?我很惊讶这样编译。即使在函数中定义了它们,它们对main?也不可见,并且也用零初始化x,y。
#include<stdio.h>
//ADD Function
void add(int x, int y){
int result = x + y;
printf("%d + %d = %d\n", x, y, result);
}
int main(void)
{
int x;
int y;
printf("Enter The First Number: ");
scanf("%d", &x);
printf("Enter The Second Number: ");
scanf("%d", &y);
add(x, y);
return 0;
}
You can write the code that way, and the program will work.
You must declare the variables X Y so that it knows where to input
----------------------------------------------------------------------------
//ADD Function
void add(){
int x,y;
printf("Enter The First Number: ");
scanf("%d", &x);
printf("Enter The Second Number: ");
scanf("%d", &y);
int result = x + y;
printf("%d + %d = %d\n", x, y, result);
}
int main(void)
{
add();
return 0;
}