声明在C void函数? [英] Declare a void function in C?

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

问题描述

我学习C和我学习功能。所以,我读到,当我实现自己的功能,我以前主要声明它()。如果我错过了声明,编译器会得到一个错误信息。
当我正在研究这个例子(发现如果数字是一个素数)

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;
 }

我注意到,一个函数素()被声明,但在主要的函数输入()被调用,也是功能输入()在底部实现。好吧,我认为这是一个错误,我从总理将名称更改为输入。
然而如果我删除了声明,我不把任何那里,程序没有错误编译而且运行流畅。 (我编译并在Ubuntu中运行它)

所以我的问题是,是否有必要宣布无效函数参数不?

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.
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 in Ubuntu)

So my question is, is it necessary to declare a void function with not arguments?

推荐答案

如果你没有使用的地方之前,你的函数的提前声明,编译器将创建隐含的声明为您服务 - 与签名 INT输入() - 它会带你调用的函数的名称,它会假设该函数返回 INT ,它可以接受的任何的参数(如鲍尔泰克在注释中说明)。

If you don't have forward declaration of your function before the place of usage, 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).

有关此功能,隐含的声明相匹配的真正的声明,这样你就不会有问题。然而,你应该时刻小心这一点,和你应该总是preFER前置声明,而不是那些隐(无论他们是相同的或没有)。因此,而不是仅仅有前进的无效素()函数的声明(假设你的地方使用它),你也应该有向前声明 INT输入()

For this function, 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 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();

// takes no 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函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆