本文介绍了在C中声明一个void函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C,并且正在学习函数.因此,我读到实现自己的函数时,必须在main()之前声明它.如果我错过了声明,编译器将收到错误消息.

I am learning C and I am studying functions. So, I read that when I implement my own function I have to declare it before the main(). If I miss the declaration the compiler will get an error message.

在研究此示例时(发现数字是否为质数)

As I was studying this example (finds if the number is a prime number),

#include <stdio.h>

void prime();               // Function prototype(declaration)

int main()
{
   int num, i, flag;

   num = input();            // No argument is passed to input()

   for(i=2,flag=i; i<=num/2; ++i,flag=i)
   {
      flag = i;

      if(num%i==0)
      {
         printf("%d is not prime\n", num);
         ++flag;
         break;
      }
   }

  if(flag==i)
     printf("%d is prime\n", num);

  return 0;

}

int input()  /* Integer value is returned from input() to calling function */
{
    int n;
    printf("\nEnter positive enter to check: ");
    scanf("%d", &n);
    return n;
 }

我注意到已声明了函数prime(),但主要是调用了函数input(),并在底部实现了函数input().好的,我认为这是一个错误,因此我将名称从"prime"更改为"input".

I noticed that a function prime() is declared, but in the main, a function, input(), is called and also the function input() is implemented at the bottom. Ok, I thought it was a mistake and I change the name from prime to input.

但是,如果我删除了声明但未在其中放置任何内容,则该程序将正确编译,并且运行平稳. (我在Ubuntu上编译并运行它.)

However if I delete the declaration and I don’t put any there, the program is compiled without errors and it runs smoothly. (I compile and run it on Ubuntu.)

是否需要声明一个没有参数的void函数?

Is it necessary to declare a void function with not arguments?

推荐答案

如果在使用位置之前没有函数的前向声明,则编译器将为您创建隐式声明-签名为int input() .它将使用您调用的函数的名称,并假定该函数正在返回int,并且可以接受 any 参数(如评论中提到的Bartek .

If you don't have a forward declaration of your function before the place of usage, the compiler will create implicit declaration for you - with the signature int input(). It will take the name of the function you called, it will assume that the function is returning int, and it can accept any arguments (as Bartek noted in the comment).

对于此函数,隐式声明与真实声明匹配,因此您没有问题.但是,您应该对此始终保持谨慎,并且您应该始终偏向于前向声明,而不是隐式声明(无论它们是否相同).因此,不仅要具有void prime()函数的前向声明(假设您将在某处使用它),还应具有一个int input()的前向声明.

For this function, the implicit declaration matches the real declaration, so you don't have problems. However, you should always be careful about this, and you should always prefer forward declarations instead of implicit ones (no matter if they are same or not). So, instead of just having forward declaration of the void prime() function (assuming that you will use it somewhere), you should also have a forward declaration of int input().

要查看如何传递任意数量的参数,请考虑以下问题:

To see how can you pass any number of the arguments, consider this:

#include <stdio.h>

// Takes any number of the arguments
int foo();

// Doesn't takes any arguments
int bar(void)
{
    printf("Hello from bar()!\n");
    return 0;
}

int main()
{
    // Both works

    // However, this will print junk as you're not pushing
    // Any arguments on the stack - but the compiler will assume you are
    foo();

    // This will print 1, 2, 3
    foo(1, 2, 3);

    // Works
    bar();

    // Doesn't work
    // bar(1, 2, 3);

    return 0;
}

// Definition
int foo(int i, int j, int k)
{
    printf("%d %d %d\n", i, j, k);
    return 0;
}

因此,在函数的定义中,您正在描述函数参数.但是,该函数的声明告诉编译器不要对参数进行任何检查.

So, inside the definition of the function you're describing function arguments. However, declaration of the function is telling the compiler not to do any checks on the parameters.

这篇关于在C中声明一个void函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 15:23
查看更多