【问题标题】:How do i fix this warning?我该如何解决这个警告?
【发布时间】:2020-10-17 11:48:25
【问题描述】:

当我使用 gcc -Wall 编译时,它会显示以下警告:

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

#include <stdio.h>

int concatenate(int y, int a)
{
  if (a<y)
  {
    a = a * 10;
    concatenate(y,a);
  }
  else 
    return a;
}

int main(void)
{
  int x;
  int y;
  int a = 10;
  scanf("%d%d", &x, &y);
  int z = concatenate(y,a);
  int result = x*z + y;
  
  printf("%d\n", result);
  
  return 0;
}

【问题讨论】:

标签: c


【解决方案1】:

在这个if语句中

   if (a<y)
   {
    a= a * 10;
    concatenate(y,a);
   }

该函数不返回任何内容。也许你的意思是

   if (a<y)
   {
    a= a * 10;
    return concatenate(y,a);
   }

【讨论】:

  • 谢谢。你能解释一下为什么它必须在那里
  • @newguyhere 该函数应返回一个值。但是您的函数不会返回任何 if 语句。
【解决方案2】:

a&lt;y 为真时,concatenate 函数中不会执行return 语句。

让它执行return 语句,即使a&lt;y 修复警告。

我猜你想要

    return concatenate(y,a);

而不是

    concatenate(y,a);

【讨论】:

  • 谢谢。你能解释一下为什么它必须在那里
  • @newguyhere 需要退货。 C 函数不会自动返回事物(C99 或更高版本中的 main 函数除外)
猜你喜欢
  • 1970-01-01
  • 2021-07-01
  • 2020-10-24
  • 1970-01-01
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多