Difference between void and non void functions [duplicate] - c

This question already has answers here:
Is it better to use C void arguments "void foo(void)" or not "void foo()"? [duplicate]
(6 answers)
Closed 6 months ago.
So I get that a void function won't return a value, while a int one for example will return an integer. So void main(){} doesnt return, but int main(){return 0;} will return.
My question is, what is the difference between these 3 functions. I know the first one wont return a value, the second one will return an integer. But how about the third one? I know it returns an integer because I've tested it, so what does that (void) does? Why is is there?
void main(){}
int main(){return 0;};
int main(void){return 0;};
I'm a begginer so sorry if it sounded confusing... Thanks in advance!!
(The question is about the programming language C)

Between the parenthesis are the parameters to the function.
In a function definition, the proper way to designate a function that takes no parameters is to simply specify void for the parameter list.
An empty parameter list (in a definition) is also a way of saying the function takes no parameters, however this syntax is considered deprecated and should not be used in new programs.

Related

How to understand that function / pointer declaration? [duplicate]

This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 10 months ago.
In C, one can declare pointers to functions like that:
void (*Func1)(int)
I believe that I've understood what this means (in this case, a pointer to a function which returns void and takes an int as parameter) and how to declare and use such pointers.
However, I now have come across declarations like the following:
void (*Func2(int, int))(int)
I am struggling with understanding this syntax. What exactly is declared here? Probably it is a pointer to a function, but I always thought that the closing round parenthesis after the pointer's name is necessary then, so I am completely unsure now.
Could anybody explain, step by step, what the above declaration means?
From https://cdecl.org/?q=void+%28*Func2%28int%2C+int%29%29%28int%29
void (*Func2(int, int))(int)
declare Func2 as function (int, int) returning pointer to function (int) returning void

Can someone explain what does this mean? [duplicate]

This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 3 years ago.
While browsing the sqlite documentation for C I've found this as a parameter to a function, what does it mean?
int (*callback)(void*,int,char**,char**);
This is the prototype of a function pointer to a callback function.
Because it is used as an argument in another function, it implies that that actual function needs to be declared and defined somewhere in your code before it is used in the function as an argument. I.e. something like this:
//declaration - possibly defined in a header file, or at top of .c file where it is used
int __cdecl handlerFunction(void*,int,char**,char**);
//definition
int __cdecl handlerFunction(void *db,int element,char **data1,char **data2)
{
//code to handle some event that invokes this callback
return 0
}

What does it mean when a C function pointer has a void parameter? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C void arguments
I am looking at some OpenGL graphics code and it has the following:
glutIdleFunc(void(*func)(void));
When does it mean to have a function pointer with a void argument in C? Does this mean the function can take in any arguments or is not allow to take in any arguments, or something else?
It means you have to pass a pointer to a function that has no parameters and returns nothing.
void func(void)
is a function that takes no parameters and does not return anything.
This is not to be confused with:
void func()
which in C (not C++) is a function that has no parameter checking, and does not return anything.
This is not to be confused with:
func(void)
which is a function which takes no parameters and returns an int, by default.

why (void) is used while invoking function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
casting unused return values to void
Need for prefixing a function with (void)
Casting function returns to void
Have seen in quite few places that while invoking the function why do we explicitly the return type of the function ? ex:
(void) myhostnames ( char * something);
What is use of this (void), and how it differs from not using the same ?
Kindly clarify.
Perhaps the function returns something and to prevent warnings from the compiler / errors from lint, the caller explicitly "throws" away the return.

in c: func(void) vs. func() [duplicate]

This question already has answers here:
Is it better to use C void arguments "void foo(void)" or not "void foo()"? [duplicate]
(6 answers)
Closed 5 years ago.
When a C function does not accept any arguments, does it have to be declared/defined with a "void" parameter by the language rules?
PC-Lint seems to have problems when there's nothing at all in the argument-list, and I was wondering if it's something in the language syntax that I don't know about.
Edit: I just found a duplicate (back-dupe? it came first) question, C void arguments, which has more answers and explanations.
void means the function does not take any parameters. For example,
int init (void)
{
return 1;
}
This is not the same as defining
int init ()
{
return 1;
}
because in the second case the compiler will not check whether the function is really called with no arguments at all; instead, a function call with arbitrary number of arguments will be accepted without any warnings (this is implemented only for the compatibility with the old-style function definition syntax, pre-ANSI).
IIRC func(void) in C will declare a function that takes no parameters whereas func() declares a function that will take any number of parameters. I believe the latter is an artifact coming from pre-ANSI C.
According to Wikipedia here, the declaration func() does basically declare the function "without information about the parameters".

Resources