How many parameters can be passed to main() [duplicate] - 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.

Related

How can we declare short int as the return type in main function of C++? [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Return type of main function [duplicate]
(5 answers)
Closed 11 months ago.
I am always returning a value of 0 from the main function. I was wondering if we could change the return type of the C++ main method to short int.
#include <iostream>
using namespace std;
short int main() {
cout << "Hello World" << endl;
return 0;
}
Something like this, but the above code returns an error saying that the return type should be 'int'. Can someone please guide me?

Why two different outputs? [duplicate]

This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 5 years ago.
I am novice in programming. I have written a program and confused in concepts of pointers.
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c[]="hello";
char *a=c;
cout<<a<<endl;
int arr[]={1,2,3,5};
int *p=arr;
cout<<p<<endl;
return 0;
}
When I print a, it prints hello but when I print p it print the address. Why?
std::ostream has overload for const char* to display C-string.
int* would use the void* one which print the address.

What is different between int main() and int main(void)? [duplicate]

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.

Why does int vs bool typecheck fail [duplicate]

This question already has answers here:
bool operator ++ and --
(4 answers)
Closed 6 years ago.
I recently wrongly declared an int as a bool and got no type error from the g++ compiler. Then I tried it again for testing and it compiles fine. Can someone explain why this is acceptable behaviour? Shouldn't the compiler give me a warning at least when i try to ++ a boolean or when i assign a bool as integer.
int main(int argc, char** argv)
{
bool x = 0;
x++;
x++;
cout << x << "\n";
return 0;
}
With the old standards (C++98) it is not an error.
With the new standards incrementing a boolean is deprecated. (C++11)
You can use incrementation on a boolean until C++17.

Example of binary operators in C++? [duplicate]

This question already has answers here:
Real world use cases of bitwise operators [closed]
(41 answers)
Closed 7 years ago.
I am currently a beginner in C++ and learning about operators.
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
unsigned int a=195;
unsigned int b=87;
unsigned int c;
c=a&b;
cout << c;
}
The Output of the above program is : 67
This is explanation
But What is the practical use of this?
This is not a boolean operator. It is a binary AND operator. The practical use of this comes from Boolean algebra, which should be studied before any programming attempt is made.