This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 1 year ago.
#include <iostream>
#include <conio.h>
using namespace std;
#define COUNT 10
void main()
{
void print_NUM(void);
int add_values(void);
void print_out(int);
int SUM;
print_NUM();
SUM=add_values();
print_out(SUM);
}
void print_NUM()
{
cout << "This program adds " << COUNT << " integers\n";
cout << "Please enter " << COUNT << " integers to be added\n";
}
I can't run it because it said that the main should be int but this is a direct copy from my lecture notes and the only way to get the answer that I need is when I use int main instead. Why is that?
C++ standard says that main function has to be of form
int main() or int main(int, char**) (of course auto with trailing return types works as well). Any other form is implementation defined http://eel.is/c++draft/basic.start.main#2. Lectures tend to be full of errors and schools teach bad practices/not standard compliant code all the time.
The C langs (C and C++) return an integer from the main function. This is mainly for exit codes. Try changing "void" to "int".
Why can't i use the void main?
Because the rules of the language say that main must return int. Violating that rule makes your program ill-formed, which means that compilers are required to issue a diagnostic message, and are allowed to refuse to compile the program. Hence, the error.
but this is a direct copy from my lecture notes
Your lecturer didn't teach you standard C++.
This used to be allowed in some old C++ dialects that pre date the standardisation. That was a long time ago.
Related
I have written c++ code for understanding union concept.In the code I have assigned some character to char array and print the int value ,but I don't understand the its logic behind.need help?
below code I have written.
#include <iostream>
union float_u {
int f;
char a[sizeof(int)];
} data1;
using namespace std;
int main()
{
cout << "Size of union :" << sizeof(data1)<<endl;
data1.a[0] = '#';
data1.a[1]= '#';
cout << "value of f :" << data1.f << endl;
return 0;
}
Output coming like this:
Size of union :4
value of f :16448
You cannot understand C++ by writing code and studying its output. Thats because there are just too many ways to write C++ code that compiles without error, but has undefined bahvior.
I am quoting from https://en.cppreference.com/w/cpp/language/union.
It is undefined behavior to read from the member of the union that wasn't most recently written.
You are writing to a. a is the active member of the union. Reading from f is undefined. You could in principle get any output.
Many compilers implement, as a non-standard language extension, the ability to read inactive members of a union.
As far as I know type punning via unions is allowed in C, so its not too difficult for compilers to also allow it in C++. Though, when you make use of a compiler extension you need to be aware that it isnt standard portable C++, and you need to read your compilers manual.
This question already has answers here:
(C++) INT_MAX and INT_MIN could not be resolved?
(4 answers)
How come INT_MAX and INT_MIN resolve in C++ without <climits> [duplicate]
(1 answer)
Why it seems not necessary to include some STL headers
(2 answers)
C++ code runs with missing header, why?
(1 answer)
Closed 3 months ago.
I was watching a tutorial on YouTube and I seen her coding this to print min and max value of data type. You can see in screenshot below. Her code was working perfectly but mine is not.
Here's My code and It's giving error.
#include <iostream>
using namespace std;
int main(){
// Print Minimum & Maximum Value Of Data Types
cout << "Minimum Value Of Int Is " << INT_MAX << endl;
system("pause>0");
}
The macro INT_MAX is defined in the <climits> header file. You need to include it if you want to use the macro.
If the Youtube video doesn't say anything about that, then perhaps it's not that good source for learning, and you should find another way (and it's much too easy to find bad videos than good, so I recommend you stay away from Youtube).
For a more C++-ish way instead include <limits> and use the std::numeric_limits class template.
More specifically its max static member function:
std::cout << "Max int value is " << std::numeric_limits<int>::max() << '\n';
This question already has answers here:
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 5 months ago.
I try to help people learn to code in C++ and I received an answer for an easy task to write a code that gets three numbers as input and outputs a sum of them. I received a wrong-written code with no return statement which... suprisingly works. It prints a good answer to the console, which (as far as know) shouldn't happen without return. I know it is written wrong and I will reply how it should be done but I want to be precise and include an answer why it worked. Here is the code:
#include <iostream>
using namespace std;
int add ()
{
int a,b,c, result;
cin >> a;
cin >> b;
cin >> c;
result=a+b+c;
}
int main()
{
cout << add();
return 0;
}
I would be grateful for an answer.
In some smart compilers it can run the code. but warning will be still there that function is expected to return integer but it is not returning anything.
but in environment like visual studio it will give the error and program will not be build.
This question already has answers here:
What's the behavior of an uninitialized variable used as its own initializer?
(3 answers)
Closed 4 years ago.
Consider some code:
#include <iostream>
int main()
{
using std::cout;
int a=3;
cout << "a="<<a<<"\n";
{
int a=a;
cout << "new a = " << a << "\n";
a=5;
cout << "a = " << a << "\n";
}
cout << "old a = " << a << "\n";
}
I'd expect it to print
a=3
new a = 3
changed a = 5
old a = 3
But what I get actually appears to say new a = 0 in the second line. I thought that it would work like initialization list in a class' constructor, where one can write like
C::C(int a) : a(a) {}
But for some reason this is different. First, removing the outer code completely doesn't result in a compilation error. So I assume that int a=a; is valid. Turning on all the compiler warnings leads to this:
test.cpp: In function ‘int main()’:
test.cpp:10:15: warning: ‘a’ is used uninitialized in this function
int a=a;
So my question now: why is this syntax valid at all? Why doesn't the compiler say something like "undefined variable a"?
It's syntactically valid, since the variable's point of declaration comes before its initialiser, and the name is available anywhere after that point. This allows less dodgy initialisations like
void *p = &p;
which legitimately uses the name (but not the value) of the variable being initialised.
It's behaviourally invalid, since using the value of an uninitialised object gives undefined behaviour. That's not an error that requires diagnosis (since, in general, it can be difficult or impossible to analyse the program flow to see whether an object has been initialised), but as you note, many compilers will give a warning for straightforward cases like this.
This question already has answers here:
Does "std::size_t" make sense in C++?
(8 answers)
Closed 5 years ago.
I've noticed that my C++ programs compile fine whether I use ::size_t or std::size_t. I can use them interchangeably with no issues at all, so it seems like one of them is a typedef for the other.
As an example, consider the following code which uses the global size_t (this is the whole file, no usings and other stuff):
#include <iostream>
int main() {
::size_t x = 100;
std::cout << x << std::endl;
}
The next code uses the size_t in std:
#include <iostream>
int main() {
std::size_t x = 100;
std::cout << x << std::endl;
}
Both compile fine and outputs 100 as expected.
I was under the impression that everything in the standard library is put in namespace std, but clearly this isn't the case. Why is this so?
Note: the same goes for ptrdiff_t, intN_t and uintN_t too.
According to what I've understood,::size_t and std::size_t are slightly different, but essentially the same, with a similar function.
There's a much better answer here: link
Hopes this helps!