This question already has answers here:
Is f(void) deprecated in modern C and C++? [duplicate]
(6 answers)
Closed 6 years ago.
I have seen in C++ program, during function declaration if there is no parameter for the function void is declared as parameter like this:
int F1(void)
How is it different than:
int F1()
There is no difference. Using void is just a more explicit way to declare the same thing. Personally, I never use that syntax and rarely see anyone else use it either.
It's the same thing in C++, and is a holdover from C.
Here's an excerpt from the C++ 2003 standard (C.1.6):
Change: In C++, a function declared with an empty parameter list takes no arguments.
In C, an empty parameter list means that the number and type of the function arguments are unknown"
Example:
int f(); // means int f(void) in C++
// intf(unknown) in C
Rationale: This is to avoid erroneous function calls (i.e. function calls with the wrong number or type of arguments).
Effect on original feature: Change to semantics of well-defined feature. This feature was marked as “obsolescent” in C.
Both of them are exactly the same, leaving the argument field empty as () is the one I prefer, some prefer writing (void) just so that someone editing the code may be ensured that no arguments are required. Makes no difference though, just a readability thing.
Related
This question already has answers here:
Is there a difference between foo(void) and foo() in C++ or C?
(4 answers)
Closed 8 years ago.
I have noticed that when I create a new C++ class in Visual Studio, it writes down Foo(void) and ~Foo(void) as constructor and destructor templates instead of Foo() and ~Foo(). However, both versions seem to work. Is there a difference between both or does it not matter at all which version I use?
In C++, both are technically the same, as in "a function that takes no arguments"
For more info:
In C, however, Foo(void) is "a function that takes no arguments", but Foo() is "a function that takes unknown amount of arguments." These arguments are of an unspecified type.
Foo(void) is used across both languages with the same meaning.
Both are equivalent, so this is merely a choice of style. I prefer Foo() because it involves less typing and it's what I've seen more often.
This question has actually been covered before on SO.
Apparently foo() in C means "a function foo taking an unspecified number of arguments of unspecified type" where in C++ it means "a function foo taking no arguments."
Only when the constructor and destructor do not need any arguments, there are the same. Also Foo(void) seems less frequently used.
This is code compiled with GCC(with -std=c99) and G++(with -std=c++98):
void fun()
{}
int main()
{
fun(1,2,3);
return 0;
}
GCC compiled the code successfully,but the G++ came up with this:
error: too many arguments to function ‘int fun()’
I am totally confused.
I knew that the C++ should be compatible with C by design,but this case shows me incompatibility.This case happens by design?
In C an empty parameter list means that you don't specify how many arguments the function takes. To define a function with 0 parameters, you'd use (void) as the parameter list. In C++ an empty parameter list means the function takes 0 parameters, so yes, C and C++ are not compatible in this instance.
I knew that the C++ should be compatible with C by design,but this case shows me incompatibility.This case happens by design?
While C++ is compatible to C in many instances, this is not the case all the time. Other examples are implicit casts from void* (allowed in C, but not in C++) and keywords (it's perfectly valid to use something like class as a variable name in C, but obviously not in C++ where it is a keyword). And yes, that's by design.
In c++
void fun();
means a function taking no arguments.
To communicate that to C write
void fun(void); // also works in c++ but it's frowned upon
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Question about a function definition (three dots in parameters..)
I'm new to c++, what is the meaning of the ... (triple dot) inside the method or function
sample
void strArr::set(const char *value, ...) {
// some code here
}
It's a way to express a variable number of arguments, used to be called va_args or variadic function.
Take a look here to learn about them in C++, they do exist in other programming languages though, since it's just a generic concept.
That's a variadic function, it can take any number of parameters (more than 1 in this case).
Same principle as printf and others:
printf("%d", 1);
printf("%d%d%d", 1,1,1);
printf("%d%d%d%d%d%d", 1,1,1,1,1,1);
These aren't overloads, but the same function taking a variable number of arguments.
This question already has answers here:
Use of 'const' for function parameters
(31 answers)
Closed 7 years ago.
I was reading my C++ book (Deitel) when I came across a function to calculate the volume of a cube. The code is the following:
double cube (const double side){
return side * side * side;
}
The explanation for using the "const" qualifier was this one: "The const qualified should be used to enforce the principle of least privilege, telling the compiler that the function does not modify variable side".
My question: isn't the use of "const" redundant/unnecessary here since the variable is being passed by value, so the function can't modify it anyway?
The const qualifier prevents code inside the function from modifying the parameter itself. When a function is larger than trivial size, such an assurance helps you to quickly read and understand a function. If you know that the value of side won't change, then you don't have to worry about keeping track of its value over time as you read. Under some circumstances, this might even help the compiler generate better code.
A non-trivial number of people do this as a matter of course, considering it generally good style.
You can do something like this:
int f(int x)
{
x = 3; //with "const int x" it would be forbidden
// now x doesn't have initial value
// which can be misleading in big functions
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In a C function declaration, what does “…” as the last parameter do?
What does this mean ,...); it is written at the end of a function in a code i am debuging.
like this void abc( int a, int b, ...);
It means the function can take any number of extra arguments. For example, consider printf; the first argument is the format string, and then there can be any number of arguments after that for all of the modifiers. This would be represented by using ... after the first argument when defining the function.
That specifies a variable number of arguments which can be accessed using the macros in the cstdarg header.