【问题标题】:How to call main from external functions file如何从外部函数文件调用main
【发布时间】:2020-08-23 21:38:41
【问题描述】:

Gcc 给出错误“functions.c: In function 'retryop': functions.c:109:3:警告:函数'main'的隐式声明 [-Wimplicit-function-declaration] 主要的(); ^~~~”

我正在创建一个回调 main 以重新启动文件的函数,但我不知道如何让它返回,因为 main 是在我的函数文件之后声明的。对此的任何帮助将不胜感激。

Main.c

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include "functions.c"

int main()
{
setup();

char type;
double number1;
double number2;
int temp;
bool retry;

printf("Please choose what operation you would like to do  ( +, -, *,/, or R): \n");
scanf("%c", &type);

if (type == '+')
{
    setup();

    printf("Addition\n");
    printf("\n");

    printf("Please enter Addend 1:");
    scanf("%lf", &number1);

    printf("Please enter Addend 2:");
    scanf("%lf", &number2);

    double answer = number1 + number2;

    printf("The Sum is: %f", answer);

    retryop();
}
else if (type == '-')
{
    setup();

    printf("Subtraction\n");
    printf("\n");

    printf("Please enter Your Minuend:");
    scanf("%lf", &number1);

    printf("Please enter Your Subtrahend:");
    scanf("%lf", &number2);

    double answer = number1 - number2;

    printf("The Differnce is: %f", answer);

    retryop();
}
else if (type == '*')
{
    setup();

    printf("Muliplication\n");
    printf("\n");
    
    printf("Please enter Factor 1:");
    scanf("%lf", &number1);

    printf("Please enter Factor 2:");
    scanf("%lf", &number2);

    double answer = number1 * number2;

    printf("Your Product is: %f", answer);

    retryop();
}
else if (type == '/')
{
    setup();

    printf("Division\n");
    printf("\n");

    printf("Please enter your divadend:");
    scanf("%lf", &number1);

    printf("Please enter your divisor:");
    scanf("%lf", &number2);

    double answer = number1 / number2;

    printf("Your quotient is: %f", answer);

    retryop();
}
else if (type == 'R')
{
    setup();

    int maxValue;
    srand(time(NULL));
    printf("Please enter your max value:");
    scanf("%i", &maxValue);
    int random = (rand() % (maxValue + 1));
    printf("Here is your random number: %i", random);

    retryop();
}
else if (type == 'r')
{
    setup();

    int maxValue;
    srand(time(NULL));
    printf("Please enter your max value:");
    scanf("%i", &maxValue);
    int random = (rand() % (maxValue + 1));
    printf("Here is your random number: %i\n", random);

    retryop();
}
else
{
    setup();

    printf("Please Re-run your program and enter a valid Operator\n");
    system("pause");
    Main();
}
}

函数.c

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

char type;
double number1;
double number2;
double answer;
bool retry;
char temp;

void setup()
{
system("cls");
}

int typeselect()
{
printf("Please choose what operation you would like to do  ( +, -, *,/, or R): \n");
scanf("%c", &type);

return type;
}

int retryop()
{
printf("Would you like to do this again? [Y/N]");
scanf("%i", &temp);
if (temp == 'Y')
{
    system("pause");
    Main();
}
else if (temp == 'y')
{
    system("pause");
    Main();
}

else if(temp == 'N')
{
    system("pause");
    return 0;
}
else if(temp == 'n')
{
    system("pause");
    return 0;
}
else
{
    system("pause");
    return 0;
}
}

【问题讨论】:

  • 不要在 main 中编写所有代码。
  • main() 不同于 Main()
  • @Cid 我修复了它,它仍然给出完全相同的错误
  • 重复一遍,不要在main 中编码所有内容。为此编写单独的函数。更一般地说,函数需要在使用前声明,参见:warning: implicit declaration of function
  • 好吧,你还没有在functions.c中声明main()

标签: c


【解决方案1】:

函数应该在使用前声明,main 也不例外。旧版本的 C 会在调用函数时隐式声明函数,一些编译器仍然允许这样做,但这是不好的做法。

首先,在 main.c 中,正确定义 main,使用 int main(void)。避免使用int main();最好明确声明 main 不带参数使用,如果您是这样使用它的话。 (如果您使用传递给它的参数,您也可以使用int main(int argc, char *argv[])。)

然后,在 Functions.c 中,以相同的方式声明 mainint main(void);。您可以将此行直接放在 Functions.c 中,也可以将其放在头文件中,例如 main.h,该文件应包含在 main.c 和 Functions.c 中的 #include 语句中。

也就是说,让retryop 调用main 的方式是糟糕的设计。重试的代码应该设计成一个循环,如果需要重试就会重复,而不是递归调用main

【讨论】:

  • 递归调用main() 几乎总是一个(非常)坏主意,尽管C 允许这样做。请注意,C++ 不允许递归调用 main()
猜你喜欢
  • 1970-01-01
  • 2021-05-06
  • 2011-05-02
  • 2021-05-07
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
相关资源
最近更新 更多