C语言 警告:控制到达非空函数的结尾 [-Wreturn-type]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22742581/
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 10:56:44  来源:igfitidea点击:

warning: control reaches end of non-void function [-Wreturn-type]

c

提问by stuart194

I am having a slight is regarding functions. I believe it is likely because I am not using them. My code is as follows:

我有一点关于功能。我相信这很可能是因为我没有使用它们。我的代码如下:

/*date difference calculator*/

#include <stdio.h>

int main()
{
  int Date1 = 0, Date2 = 0, Dif, F, L, D1, D2, M1, M2, Y1, Y2;
  int x[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  int y[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  char A1, A2, B1, B2;

/*input first date*/

 fprintf (stderr , "Enter first date,in the form <day>/<month>/<year>  or <day>-<month>-<year>.\nWhere <day>,<month> and <year> are integers:\n");
 (scanf("%i%c%i%c%i", &D1, &A1, &M1, &B1, &Y1));

/*check first date*/

 if (!(Y1 % 4)) x[2]=29;
 while ((Y1 < 1 || (Y1 > 9999)) || (M1 < 1 || M1 > 12) || (D1 < 1 || D1 > x[M1]) || (A1 != B1) || ((A1 != '/') && (A1 != '-'))) 
     {
       fprintf (stderr, "Incorrect format, re-enter date:\n");
       scanf("%i%c%i%c%i", &D1, &A1, &M1, &B1, &Y1);
       if (!(Y1 % 4)) x[2]=29;
     }

/*print first date*/

 fprintf (stderr, "First date = %i%c%i%c%i\n", D1 , A1 , M1 , B1 , Y1);

/*input second date*/

 fprintf (stderr , "Enter second date,in the form <day>/<month>/<year> or <day>-<month>-<year>.\nWhere <day>,<month> and <year> are integers:\n");
 (scanf("%i%c%i%c%i", &D2, &A2, &M2, &B2, &Y2));

/*check second date*/

 if (!(Y2 % 4)) y[2]=29;
 while ((Y2 < 1 || (Y2 > 9999)) || (M2 < 1 || M2 > 12) || (D2 < 1 || D2 > y[M2]) || (A2 != B2) || ((A2 != '/') && (A2 != '-'))) 
     {
       fprintf (stderr, "Incorrect format, re-enter date:\n");
       scanf("%i%c%i%c%i", &D2, &A2, &M2, &B2, &Y2);
       if (!(Y2 % 4)) y[2]=29;
     }

/*print second date*/

 fprintf (stderr, "Second date = %i%c%i%c%i\n", D2 , A2 , M2 , B2 , Y2);

/*convert first date into days*/

 for (F = 1; Y1 > F ; F++) 
    {
     if (F % 4 == 0) (Date1 = Date1 + 366);
     else (Date1 = Date1 + 365);
    }
 for (L = 1; M1 > L ; L++) 
     Date1 = Date1 + x[L];
 Date1 = Date1 + D1;

/*convert second date into days*/

 for (F = 1; Y2 > F ; F++) 
    {
     if (F % 4 == 0) (Date2 = Date2 + 366);
     else (Date2 = Date2 + 365);
    }
 for (L = 1; M2 > L ; L++) 
     Date2 = Date2 + y[L];
 Date2 = Date2 + D2;

/*standard output*/

 Dif = Date2 - Date1;
 printf("\n%i\n\n" , Dif);

/*text output*/

 if (Date2 > Date1)  
    {Dif = Date2 - Date1;
    fprintf (stderr , "Indicating that the first date is %i days before second date.\n" , Dif);}
 if (Date1 > Date2)  
    {Dif = Date1 - Date2;
    fprintf (stderr , "Indicating that the second date is %i days before first date.\n" , Dif);} 
 if (Date1 == Date2)  
    fprintf (stderr , "Indicating that the first date is equal to second date.\n"); 
}

When compiling using this: gcc -Wall -ansi date1.c -o date1This occurs:

使用此编译gcc -Wall -ansi date1.c -o date1时:会发生这种情况:

date1.c: In function ‘main':
date1.c:70:1: warning: control reaches end of non-void function [-Wreturn-type]

Is there a simple fix for this or do I have to write my program to use functions propely? I am unable to change how I compile the code as it has to follow a set spec.

是否有一个简单的解决方法,或者我是否必须编写我的程序才能正确使用函数?我无法更改编译代码的方式,因为它必须遵循既定规范。

Apologies for the poor formatting of my question but this is my first time here and I was hoping to be able to do it alone.

为我的问题格式不佳而道歉,但这是我第一次来到这里,我希望能够独自完成。

Hope someone can help!

希望有人能帮忙!

回答by perreal

You just need to returnfrom the main function at some point. The error message says that the function is defined to return a value but you are not returning anything.

您只需要return在某个时候从 main 函数中调用。错误消息表明该函数被定义为返回一个值,但您没有返回任何内容。

  /* .... */
  if (Date1 == Date2)  
     fprintf (stderr , "Indicating that the first date is equal to second date.\n"); 

  return 0;
}

回答by Michael Andersson

You can also use EXIT_SUCCESSinstead of return 0;. The macro EXIT_SUCCESSis actually defined as zero, but makes your program more readable.

您也可以使用EXIT_SUCCESS代替return 0;。宏EXIT_SUCCESS实际上定义为零,但使您的程序更具可读性。