我在哪里声明 x 和 y 才能使此 c 代码工作?

发布于 2024-12-22 06:55:26 字数 282 浏览 2 评论 0原文

我有:

 #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪我太投入 2024-12-29 06:55:26

在第三行中,您所做的就是声明一个函数 sum,它接受两个参数,均为整数,称为 xy。您尚未声明任何变量。这些参数只能在函数本身内部引用。下面是一个简化的内容,它将在这个阶段帮助您,但是您应该尝试阅读一本基本的编程书籍。 Kernighan 和 Ritchie 所著的《C 编程语言》是一个很好的起点。

变量是您通过名称引用的内存块。它们可以在程序的生命周期中采用任何(其类型的)值 - 因此称为“变量”。在使用它们之前必须声明它们;您可以通过告诉编译器它们的类型和名称来完成此操作。 int a 的意思是“为我保留一块足够大的内存块来容纳任何整数,让我稍后用名称 a 来引用它”。您可以为其赋值:a = 10,并且可以使用它:a + 20

您需要了解参数和变量之间的区别才能了解这里发生的情况。函数的参数基本上是仅在该函数的生命周期内存在的变量。这是您的 sum

int sum(int x, int y) {
    return x + y;
}

注意顶行看起来就像一个变量声明 int x。那是因为它是。 xy 是可以在函数中使用的变量。

您可以通过传入值来调用 sum。实际上,编译器将函数中的 xy 替换为您传入的值。在您的情况下,您传递的是 literals : 10 和 11。当程序调用 sum 时,参数 xy 的值为 10 和 11,因此return 变为 return 10 + 11; 当然是 21。

只要记住参数 xy 仅存在于该函数中。您只能在您的函数中引用它们。为什么?因为每对大括号 {} 定义一个范围,并且在该范围内声明的任何内容都只能在该范围内使用。这包括变量和参数。

因此,这是一个更完整的示例。我更改了字母,以便您可以看到使用变量和参数的不同方式:

#include <stdio.h>

int sum ( int x, int y );

main ()
{
   /* We declare our variables */
   int a;
   int b;

   /* We assign values to them */
   a = 10;
   b = 11;

   /* We pass them as parameters to your sum function */
   int theSum = sum (a, b);
   /* And we use them as parameters again, in a call to the printf function */
   printf ( "Sum of %i and %i is: %i\n", a, b, theSum );
}

int sum ( int x, int y )
{
   return x + y;
}

In line three all you have done is declare a function sum which takes two parameters, both integers, called x and y. 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 name a'. 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:

int sum(int x, int y) {
    return x + y;
}

Notice how the top line looks just like a variable declaration int x. That's because it is. x and y are variables you can use in the function.

You call sum by passing in values. The compiler, in effect, replaces x and y 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 to sum, the parameters x and y take on the values 10 and 11, so the return becomes return 10 + 11; which is of course 21.

Just remember that the parameters x and y 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:

#include <stdio.h>

int sum ( int x, int y );

main ()
{
   /* We declare our variables */
   int a;
   int b;

   /* We assign values to them */
   a = 10;
   b = 11;

   /* We pass them as parameters to your sum function */
   int theSum = sum (a, b);
   /* And we use them as parameters again, in a call to the printf function */
   printf ( "Sum of %i and %i is: %i\n", a, b, theSum );
}

int sum ( int x, int y )
{
   return x + y;
}
一梦浮鱼 2024-12-29 06:55:26

在调用 sum 之前声明 x 和 y:

main ()
{
int x = 10;
int y = 11;
int theSum = sum (x, y);
 printf ( "Sum of %i and %i is: %i\n", x, y, theSum );
 }

declare x and y right before the call to sum:

main ()
{
int x = 10;
int y = 11;
int theSum = sum (x, y);
 printf ( "Sum of %i and %i is: %i\n", x, y, theSum );
 }
燕归巢 2024-12-29 06:55:26

您必须在 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.

月朦胧 2024-12-29 06:55:26

不,你没有。您刚刚告诉编译器函数 sum 需要两个整数。您可以将其写为

 int sum ( int , int );

因此您应该写:

 #include <stdio.h>

 int sum ( int , int );

main ()
 {
     int x = 10;
     int y = 11;
     int theSum = sum (x, y);
     printf ( "Sum of %i and %i is: %i\n", x, y, theSum );
 }

 int sum ( int x, int y )
 {
 return x + y;
 }

No, you haven't. You just told the compiler that the function sum takes two ints. You could have written that as

 int sum ( int , int );

Therefore you should write:

 #include <stdio.h>

 int sum ( int , int );

main ()
 {
     int x = 10;
     int y = 11;
     int theSum = sum (x, y);
     printf ( "Sum of %i and %i is: %i\n", x, y, theSum );
 }

 int sum ( int x, int y )
 {
 return x + y;
 }
倾听心声的旋律 2024-12-29 06:55:26

x 和 y 变量仅作为参数传递给函数。您将无法在主函数中引用这些内容。如果你愿意的话,它会是这样的:

#include <stdio.h>

int sum ( int x, int y );

main ()
{
    int x = 10;
    int y = 11;
    int theSum = sum (x, y);
    printf ( "Sum of %i and %i is: %i\n", x, y, theSum );
}

int sum ( int x, int y )
{
    return 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:

#include <stdio.h>

int sum ( int x, int y );

main ()
{
    int x = 10;
    int y = 11;
    int theSum = sum (x, y);
    printf ( "Sum of %i and %i is: %i\n", x, y, theSum );
}

int sum ( int x, int y )
{
    return x + y;
}

This should work. I hope this helps!

§普罗旺斯的薰衣草 2024-12-29 06:55:26

在您的 main 中,您既没有声明 x 也没有声明 y — 您只是传入文字值 10 和 11。 sum 方法,这两个名称都没有任何含义。快速修复:

main()
{
    int x = 10;
    int y = 11;
    int theSum = sum(x, y);
    // etc.

In your main you’re declaring neither x nor y—you’re just passing in the literal values 10 and 11. Outside the scope of the sum method, neither name has any meaning. Quick fix:

main()
{
    int x = 10;
    int y = 11;
    int theSum = sum(x, y);
    // etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文