我认为区别在于declaration没有参数类型......
为什么这样做:
int fuc();
int fuc(int i) {
printf("%d", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这无法编译:
int fuc();
int fuc(float f) {
printf("%f", f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
随着消息:
错误:'fuc'的冲突类型.注意:具有默认促销的参数类型不能与空参数名称列表声明匹配
可能重复:
C void参数
刚开始用C而我找不到答案...
两者之间有什么区别吗?
int foo() { }
int foo(void) { }
Run Code Online (Sandbox Code Playgroud)
我应该选择哪个?为什么?
请注意,这个问题也适用于:int main.它应该是:int main或者int main(void)当我不想要任何命令行参数时.
我了解到,使用空白参数列表定义函数与使用参数列表定义函数不同void.请参阅(使用C void参数"void foo(void)"或"void foo()"是否更好?).
这种误解似乎对我来说是一个常见的错误,我很惊讶,即使我通过,也gcc没有clang发出任何警告-Wall -Wextra -pedantic.
为什么是这样?
我想知道为什么以下代码:
void foo(void);
void foo()
{
}
Run Code Online (Sandbox Code Playgroud)
在gcc中有效.在C中,没有重载和上面的声明(事实上,其中一个是定义)声明两个不同的函数(第一个不接受任何参数,第二个可以接受任何数量的参数)类型).
但是,如果我们为第一个函数提供定义:
void foo(void)
{
}
void foo()
{
}
Run Code Online (Sandbox Code Playgroud)
由于重新定义,此次编译失败.但是,第一个代码仍然是正确的,可能会令人困惑,如下所示:
void foo(void);
int main(void)
{
foo(); //OK
//foo(5); //Wrong, despite ->the definition<- allows it
}
void foo()
{
}
Run Code Online (Sandbox Code Playgroud)
另一方面,这样的事情是无效的:
void foo(int);
void foo() //error: number of arguments doesn't match prototype
{
}
Run Code Online (Sandbox Code Playgroud)
我认为与我的第一个前面的代码相比,编译器的行为有点奇怪.int是不等于(/*empty list*/),也不是void.
有谁能解释一下?
6.7.6.3 Function declarators (including prototypes)
Run Code Online (Sandbox Code Playgroud)
这部分标准涉及'Identifier list'和'Parameter type list'.
首先,函数声明(非定义)与函数原型相同.我对么?如果这是对的,那么为什么标准会这样'including prototypes'说呢?
我无法理解函数声明'Identifier list'和'Parameter type list'函数声明之间的区别.
int fun(); // Declaration
int fun(int x)// Definition, but the signature doesn't match and it works.
{ return x; }
Run Code Online (Sandbox Code Playgroud)
有人可以解释,我很困惑?
我试图在C中构建一个非常简单的程序,它从函数返回一个浮点值,但由于某种原因我得到了一个错误.
#include<stdio.h>
int main(){
double returning;
returning = regre();
printf("%f", returning);
return 0;
}
double regre(){
double re = 14.35;
return re;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误说:
'regre'的冲突类型
先前隐含的regre声明就在这里
我在C++中看到了两种定义转换运算符重载的样式,
operator int* (void) constoperator int*() const问题1.我认为两种风格(无论是否添加void)都具有相同的功能,对吗?
问题2.任何偏好哪个更好?
我刚才读到:"C void arguments"关于C中这些函数定义之间的差异:
int f(void)
Run Code Online (Sandbox Code Playgroud)
和
int f()
Run Code Online (Sandbox Code Playgroud)
理解第二种形式意味着一个函数返回带有任意数量参数的整数,我想知道我们如何实际访问和使用这些未知参数?
我很想得到示例代码和解释.
另外,我知道的可变参数的在C机构(与va_arg,va_end,va_start函数)和将高兴地听到这个机构和之间的差异f()的形式如上所述.
非常感谢!
例:
void Function(int Number)
{
process.....
**return;**
}
Run Code Online (Sandbox Code Playgroud)
是否必须在每个功能结束时使用"返回"?
例2:
void Function(**void**)
{
process...
}
Run Code Online (Sandbox Code Playgroud)
如果我没有收到任何值,是否需要在参数列表中使用"void"?
有人说不,有人说是.什么是完全理解C中的编译器和最佳实践的正确理由?
我正在尝试编写一些功能,我需要保存不同的函数,然后提取它们的参数类型.所以我使用函数签名作为模板参数.但我得到一些意想不到的结果.这是代码:
#include <functional>
#include <iostream>
template <class T>
struct foo
{
foo()
{
std::cout << "class T" << std::endl;
}
};
template <class Ret, class Arg>
struct foo<Ret(Arg)>
{
foo()
{
std::cout << "Ret(Arg)" << std::endl;
}
};
template <class T>
void save(std::function<T>)
{
new foo<T>();
}
int main(int argc, char* argv[])
{
std::function<void(void)> someFoo;
save(someFoo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果变量someFoo是具有类型的函数void(void),则它实例化第一个模板,foo<T>.但是,如果我将其更改为void(int),则会实现所需的专用模板.这是为什么?