C语言 C编程:如何返回main并调用另一个函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14843452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:20:39  来源:igfitidea点击:

C programming: How to return to main and call another function?

creturnreturn-value

提问by James

#include <stdio.h>
#include <stdlib.h>

int get_num(int num);
void read_num(int num);

int main()
{
  int num;
  do
  {
    get_num(num);
    if(num == 1)
      read_num(num);

   }while(num != 0);
 }

 int get_num(int num)
 {
   printf("Please enter an integer from 0 and 2\n");
   scanf("%d", &num);
   if (num == 1)
     return num;
 }

 void read_num(int num);
 {
   printf("Hello.\n");
 }

When the user enters 1, the read_num function never gets called? I don't understand why? I thought return num; returns what num back to main. And if num == 0, the program terminates, and if num == 1, then the read_num function should be called.

当用户输入 1 时, read_num 函数永远不会被调用?我不明白为什么?我以为 return num; 将什么 num 返回给 main。如果 num == 0,程序终止,如果 num == 1,则应调用 read_num 函数。

回答by Sebastian

There is quite a lot wrong with your code: main()and getNum()are both declared as returning an int, yet main()never returns anything and getNum()returns only if the user enters 1. If you have a function returning anything other than void, make sure that every possible control flow has an appropriate returnstatement.

您的代码有很多错误: main()并且getNum()都声明为返回 int,但从main()不返回任何内容并且getNum()仅在用户输入时返回1。如果您有一个函数返回除 之外的任何内容void,请确保每个可能的控制流都有一个适当的return语句。

Then there is the issue with call by value vs call by reference. You should really look that up, because this is not the right place to explain and many people have written about better than I can.

然后是按值调用与按引用调用的问题。你真的应该查一下,因为这不是解释的正确地方,很多人写得比我写得更好。

read_num(num);num is undefined here, you might understand that better once you know how C passes parameters.

read_num(num);num 在这里未定义,一旦您知道 C 如何传递参数,您可能会更好地理解这一点。

The same goes for returning values. Functions in C return values (declared by the return type as in int get_num(). When you call a function like that you can assign the returnvalue to a variable. In main you could write int number = get_num()

返回值也是如此。C 中的函数返回值(由 中的返回类型声明int get_num()。当您调用这样的函数时,您可以将return值分配给变量。在 main 中,您可以编写int number = get_num()

Your code could look a little bit like that, but I'm not sure if that's what you really want it to do:

您的代码可能看起来有点像,但我不确定这是否是您真正想要它做的:

int main()
{
    int num;
    do {
        num = get_num();
        if(num == 1) {
            printf("%d\n", num);
        }   
    } while(num != 0);
    return 0;
}

int get_num(int num)
{
    printf("Please enter an integer from 0 and 2\n");
    scanf("%d", &num);
    if (num == 1)
        return num;
    return 0;
}

回答by mikyra

C uses pass by value not pass by reference, as such your are just working with a COPY of the value stored in the variable "num" of your main function, when you enter get_num. Manipulation the value of the variable "num" in get_num won't do any change to the value of the variable "num" in your main function.

C 使用按值传递而不是按引用传递,因此当您输入 get_num 时,您只是在处理存储在主函数的变量“num”中的值的 COPY。操作 get_num 中变量“num”的值不会对主函数中变量“num”的值进行任何更改。

To use the "calculated" value in your main function you have to assign the return value of your get_num function to the variable "num" in your main function. Just replace the line

要在主函数中使用“计算”​​值,您必须将 get_num 函数的返回值分配给主函数中的变量“num”。只需更换线路

get_num(num);

with

num = get_num(num);

and everything should be fine.

一切都应该没问题。

EDIT: To return more than one value you would have to pass pointers to the desired storage location of the return values to a function. As an example here a variation of your program using a pointer instead.

编辑:要返回多个值,您必须将指向返回值的所需存储位置的指针传递给函数。作为示例,这里使用指针代替您的程序的变体。

#include <stdio.h>
#include <stdlib.h>

int get_num(int num);
void read_num(int num);

int main()
{
  int num = 0; /* better give it a defined value here */
  int n_args_read;
  do
  {
    n_args_read = get_num(&num);
    if(n_args_read == 1)
      read_num(num);

   }while(num != 0);
 }

 int get_num(int *num)
 {
   printf("Please enter an integer from 0 and 2\n");
   return scanf("%d", num);
 }

 void read_num(int num);
 {
   printf("Hello.\n");
 }