This question already has answers here:
Difference between int main() and int main(void)?
(10 answers)
Closed 6 years ago.
Can anyone help me with what is different between int main() and int main(void)?
Using (void) as the single parameter in a C++ function is exactly equivalent to using ().
Although, stylistically (void) is to be discouraged.
In C++, there is no difference. The difference only arises in C, where not explicitly mentioning void in the parameter list allows the function to be called with any number of parameters, while the second version only allows exactly 0 parameters.
Related
This question already has answers here:
Usefulness of const (C++)
(6 answers)
Closed 6 years ago.
Hi guys I'm new with C++, whats the difference between using const int n=1 and int n=1 , I dont understant what does const do. Can you give me examples or something?
With const int n=1 you cannot modify the value of n in your code, for example if you try to do n= 4 then it would cause an error,
but with simple int n=1; you can always modify the value of n in your code like this
n= 4;
n=7;
This question already has answers here:
Why would one use nested classes in C++?
(6 answers)
Closed 7 years ago.
so I've been playing around with C++. I was trying to figure out if it's possible to define a structure in the definition of another structure. And here's my code.
#include <iostream>
using namespace std;
int main(){
struct structure1{
int integer1;
struct structure2{
int integer2;
}struct2;
}struct1;
structure1 *s1 = &struct1;
s1->integer1 = 50;
cout<<"STRUCTURE ONE'S INTEGER: "<<s1->integer1<<endl;
cout<<"STRUCTURE ONE'S STRUCTURE2.integer2: "<<s1->struct2.integer2;
}
OUTPUT:
$ ./a.out
STRUCTURE ONE'S INTEGER: 50
STRUCTURE ONE'S STRUCTURE2.integer2: 0
From what I saw in the output it had seemed to be working. But I just don't understand why or how it worked. Is it good practice? Is there any application to this?
Thanks!
But I just don't understand why or how it worked.
Why wouldn't it work? It's perfectly legal C++.
Is it good practice?
Depends on how it's used.
Is there any application to this?
Definitely. For example when you only need structure2 inside structure1 and nowhere else. It's good to make its scope as small as possible.
For more information: Why would one use nested classes in C++?
This question already has answers here:
Can the arguments of main's signature in C++ have the unsigned and const qualifiers? [duplicate]
(7 answers)
Closed 8 years ago.
Is there a limit for the number of parameters main can have?
Here is a sample code which runs perfectly … and I am not aware if it is allowed.
int main( char* argv[], int argc, int arv, bool test)
{
cout<<"Hello"<<endl;
}
Output:
Hello
I am using
gcc-4.1.2_20070115-0.32.53
gcc-c++-4.1.2_20070115-0.32.53
libgcc-4.1.2_20070115-0.32.53
gcc-objc-4.1.2_20070115-0.32.53
The C++ standard does not explicitly forbid these signatures, but it does not require them to work either. All it says is that the two following signatures must work on any compiler:
int main()
int main(int, char**)
And that the return type must be int.
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 9 years ago.
In most cases int main() does not return anything, it doesn't even have to since no return would not give an error. So why does main have to return an int? Why is void main not possible?
EDIT: I meant, why is int main() the standard if there usually is no return?
Other programs may use the return code to determine whether the application executed successfully. Zero typically means successful execution.
void is possible but non-standard. The returned int is meant to signify something for the caller.
The same way as a function in your program may returns values to indicate its result, main returns to indicate the result of execution of your program.
Also in case int main() does return explicit, compiler will put return 0 automatically
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What should main() return in C/C++?
why do we give int main in c++ and not void main?
I've started learning C++ and the following question came up to my mind: main() always return int? Cannot I declare void main() instead of int main()?
Thanks you!
Yes, main() must return int. The return value is passed back to the operating system, to indicate whether the program ran successfully: zero means success.
However, you can leave the return statement out of main (and only main) if you like; in that case, it will return zero.
It has to return an integer value. The returned value tells the computer what, if any, error codes there were. Returning 0 will tell it that there were no errors in the program.