不带参数的行为C函数 [英] C function with no parameters behavior

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

问题描述

有人可以解释我为什么以下code编译并没有警告或错误?

我希望编译器警告我说,功能测试不希望任何参数。
但是,code编译并运行递归功能测试。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;静态无效TEST1(INT A,INT B,INT C){}静态无效测试(){
    的printf(在这里\\ n);
    测试(1,2,3);
}无效的主要(){
  测试();
}


解决方案

在C ++中,无效测试()声明了一个函数,它没有paramerers(并且没有返回)。

在C,无效测试()声明了一个函数,它的参数未指定的(但不是变量)数量(并且没有返回)。因此,所有呼叫都有效(按原型)的温度。

在C,使用无效测试(无效)

Can somebody explain to me why the following code does compile without a warning or error ?

I would expect the compiler to warn me that the function test doesn't expect any arguments. But the code compiles and runs function test recursively.

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

static void test1(int a, int b, int c) {}

static void test() {
    printf("HERE\n");
    test(1,2,3);
}

void main() {
  test();
}

解决方案

In C++, void test() declares a function that takes no paramerers (and returns nothing).

In C, void test() declares a function that takes an unspecified (but not variable) number of parameters (and returns nothing). So all your calls are valid (according to the prototype) in C.

In C, use void test(void).

这篇关于不带参数的行为C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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