in c: func(void) vs. func() [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 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".

Related

Difference between void and non void functions [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 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.

Uncommon function definition in the 'less' program source [duplicate]

This question already has answers here:
C function syntax, parameter types declared after parameter list
(7 answers)
Closed 3 years ago.
My friend asked me could I add "frozen top line" feature to the standard less *NIX program. I started to view the source and was very surprised in function definition the filename.c:
public char *
last_component(name)
char *name;
{
....
}
I wrote some code at the C and usually I met:
return_type function_name arguments_list; if it is function prototype
return_type function_name arguments_list code-block if it is function definition.
But here I found something new: return_type function_name arguments list something_mysterious; code block.
What does this syntax means?
This is old C syntax (also known as K&R style), where a function definition could be written as where the argument are declared on the subsequent line:
int func(a,b)
int a;
char b;
{

Why declaring function arguments this way: void func(x) int x; {} [duplicate]

This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 3 years ago.
I stumbled upon these lines when looking into putchar.c
I'm wondering about why the arguments ptr and c are declared outside the arguments body ?
Is this some kind of "good old way" or does it have some actual use ?
int
_putchar_r (ptr, c)
struct _reent *ptr;
int c;
{
return __sputc (c, _stdout_r (ptr));
}
Indeed it is the "olden" way of declaring a function's parameters.
I kind of like it, because it serves as a constant reminder that all a function's parameters are local variables that exist only in the scope of the function and that any argument passed into any function ALWAYS is a value copy.
Its a K&R C style introduced in classic C Programming Book
It is a function definition with an identifier list. Each identifier in the identifier list is declared before the compound statement of the function.
So a function can be defined either with a parameter type list or using the old style with an identifier list.

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.

No previous prototype? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Error: No previous prototype for function. Why am I getting this error?
I have a function that I prototyped in the header file, however Xcode still gives me warning No previous prototype for the function 'printBind'. I have the function setBind prototyped in the same way but I do not get an warning for this function in my implementation.
CelGL.h
#ifndef Under_Siege_CelGL_h
#define Under_Siege_CelGL_h
void setBind(int input);
void printBind();
#endif
CelGL.c
#include <stdio.h>
#include "CelGL.h"
int bind;
void setBind(int bindin) { // No warning here?
bind = bindin;
}
void printBind() { // Warning here
printf("%i", bind);
}
In C, this:
void printBind();
is not a prototype. It declares a function that returns nothing (void) but takes an indeterminate list of arguments. (However, that list of arguments is not variable; all functions taking a variable length argument list must have a full prototype in scope to avoid undefined behaviour.)
void printBind(void);
That's a prototype for the function that takes no arguments.
The rules in C++ are different - the first declares a function with no arguments and is equivalent to the second.
The reason for the difference is historical (read 'dates back to the mid-1980s'). When prototypes were introduced into C (some years after they were added to C++), there was an enormous legacy of code that declared functions with no argument list (because that wasn't an option before prototypes were added), so backwards compatibility considerations meant that SomeType *SomeFunction(); had to continue meaning 'a function that returns a SomeType * but for which we know nothing about the argument list'. C++ eventually added the SomeType *SomeFunction(void); notation for compatibility with C, but didn't need it since type-safe linkage was added early and all functions needed a prototype in scope before they were defined or used.

Resources