我在哪里声明 x 和 y 才能使此 c 代码工作?
我有:
#include <stdio.h>
int sum ( int x, int y );
main ()
{
int theSum = sum (10, 11);
printf ( "Sum of %i and %i is: %i\n", x, y, theSum );
}
int sum ( int x, int y )
{
return x + y;
}
但是,当我编译并运行时,它说 x 和 y 未声明?非常感谢任何帮助。谢谢
I have:
#include <stdio.h>
int sum ( int x, int y );
main ()
{
int theSum = sum (10, 11);
printf ( "Sum of %i and %i is: %i\n", x, y, theSum );
}
int sum ( int x, int y )
{
return x + y;
}
However, when I compile and run it says x and y are undeclared? Any help greatly appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在第三行中,您所做的就是声明一个函数
sum
,它接受两个参数,均为整数,称为x
和y
。您尚未声明任何变量。这些参数只能在函数本身内部引用。下面是一个简化的内容,它将在这个阶段帮助您,但是您应该尝试阅读一本基本的编程书籍。 Kernighan 和 Ritchie 所著的《C 编程语言》是一个很好的起点。变量是您通过名称引用的内存块。它们可以在程序的生命周期中采用任何(其类型的)值 - 因此称为“变量”。在使用它们之前必须声明它们;您可以通过告诉编译器它们的类型和名称来完成此操作。
int a
的意思是“为我保留一块足够大的内存块来容纳任何整数,让我稍后用名称a
来引用它”。您可以为其赋值:a = 10
,并且可以使用它:a + 20
。您需要了解参数和变量之间的区别才能了解这里发生的情况。函数的参数基本上是仅在该函数的生命周期内存在的变量。这是您的
sum
:注意顶行看起来就像一个变量声明
int x
。那是因为它是。x
和y
是可以在函数中使用的变量。您可以通过传入值来调用
sum
。实际上,编译器将函数中的x
和y
替换为您传入的值。在您的情况下,您传递的是 literals : 10 和 11。当程序调用sum
时,参数x
和y
的值为 10 和 11,因此return 变为return 10 + 11;
当然是 21。只要记住参数
x
和y
仅存在于该函数中。您只能在您的函数中引用它们。为什么?因为每对大括号{
和}
定义一个范围,并且在该范围内声明的任何内容都只能在该范围内使用。这包括变量和参数。因此,这是一个更完整的示例。我更改了字母,以便您可以看到使用变量和参数的不同方式:
In line three all you have done is declare a function
sum
which takes two parameters, both integers, calledx
andy
. You haven't declared any variables. Those parameters can only be referred to inside the function itself. Below is a simplification which will help you at this stage, but you should try to read a basic programming book. "The C Programming Language" by Kernighan and Ritchie is a fine place to start.Variables are chunks of memory that you refer to by name. They can take on any value (of their type) during the life of your program - hence the name 'variable'. They must be declared before you use them; you do this by telling the compiler their type and their name.
int a
means 'reserve me a block of memory big enough to hold any integer, and let me refer to it later with the namea
'. You can assign values to it:a = 10
and you can make use of it:a + 20
.You need to understand the difference between parameters and variables to get what's going on here. A function's parameters are basically variables which exist only during the life of that function. Here's your
sum
again:Notice how the top line looks just like a variable declaration
int x
. That's because it is.x
andy
are variables you can use in the function.You call
sum
by passing in values. The compiler, in effect, replacesx
andy
in your function with the values you pass in. In your case, you're passing literals: 10 and 11. When the program reaches the call tosum
, the parametersx
andy
take on the values 10 and 11, so the return becomesreturn 10 + 11;
which is of course 21.Just remember that the parameters
x
andy
only exist in that function. You may only refer to them within your function. Why? Because each pair of curly braces{
and}
define a scope, and anything declared within that scope can only be used within that scope. That includes variables and parameters.So, here is a more complete example. I have changed the letters so you can see the different ways you use variables and parameters:
在调用 sum 之前声明 x 和 y:
declare x and y right before the call to sum:
您必须在 main 中声明 x 和 y,以便调用 sum(x,y) 而不是在 2 个文字上调用它。目前,x 和 y 仅在函数 sum 中定义。
You would have to declare x and y in main, so that you call sum(x,y) rather than calling it on 2 literals. At the moment, x and y are only defined in the function sum.
不,你没有。您刚刚告诉编译器函数
sum
需要两个整数。您可以将其写为因此您应该写:
No, you haven't. You just told the compiler that the function
sum
takes two ints. You could have written that asTherefore you should write:
x 和 y 变量仅作为参数传递给函数。您将无法在主函数中引用这些内容。如果你愿意的话,它会是这样的:
这应该可行。我希望这有帮助!
The x and y variables are only passed as arguments to the function. You would not be able to reference those in the main function. If you wanted to, it would be like this:
This should work. I hope this helps!
在您的
main
中,您既没有声明x
也没有声明y
— 您只是传入文字值 10 和 11。sum
方法,这两个名称都没有任何含义。快速修复:In your
main
you’re declaring neitherx
nory
—you’re just passing in the literal values 10 and 11. Outside the scope of thesum
method, neither name has any meaning. Quick fix: