Cout works well on wrong-written function [duplicate] - c++

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.

Related

Why can't i use the void main? [duplicate]

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.

How does the compiler determine what value to output during runtime when a function name is sent to cout without parenthesis? C++ [duplicate]

This question already has answers here:
g++ "calling" a function without parenthesis (not f() but f; ). Why does it always return 1?
(2 answers)
Closed 2 years ago.
#include <iostream>
int returnFive()
{
return 5;
}
int main()
{
std::cout << returnFive << '\n';
return 0;
}
Since this compiles without error, how does the system determine what value is actually sent and printed to console?
Imagine if the code written is something like
if(returnFive)
returnFive();
Here the expectation is that the compiler checks if the function pointer for returnFive is nullptr or not.
The compiler here is evaluating the function pointer as a boolean expression of whether it is NULL or not and printing the output.
https://godbolt.org/z/Psdc69. You can check that the cout is being passed a (bool).

Return value in C++ [duplicate]

This question already has answers here:
C++ return value without return statement
(6 answers)
Closed 6 years ago.
I am confused with the following output in C++
int add()
{
int c = 2+3;
}
int main()
{
int x = add();
cout << x;
return 0;
}
This prints 5.even if we do not write return statement.
How this is managed in C++.
Please help.
This is UB. You're right to be confused - this can work one day and fail the next. Don't rely on undefined behavior.
If you want to know why it works, it's because parameters & return values are passed on a data structure called stack (well - usually; sometimes passed in the same register). Similarly, most implementations use this same stack for locals. Therefore, the int in add will be located in the same place as where the return value is expected (by your specific implementation) and your implementation doesn't invalidate memory when your int there is destructed. But it's still destructed, it's still UB and it might break in any second.
As the comments wrote, you might turn on warnings to avoid this kind of error.

How does this function return a value when it shouldn't? [duplicate]

This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 6 years ago.
This function returns a value even when it shouldn't.
#include<iostream>
using namespace std;
int foo(int a,int b)
{
if(a>b)
return a;
else if(a<b)
return b;
}
int main()
{
int x=7,y=7;
cout<<foo(x,y);
return 0;
}
The output is:
7
Also it produces proper output only on a GCC compiler (I used Dev C++). Turbo C produced garbage value.
Can someone explain how this happens?
The behaviour on not returning a value on all program control paths is undefined.
The compiler is allowed to do anything.
Didn't your compiler warn you of this? (GCC ought too, Turbo C possibly not on account of its age).

Operator ++ with stdout giving unexpected result in C/C++ [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
During study in C++ at school, when we learn about operator ++ in C++, we know ++c and c++ are different.
When we test more about this with this kind of code:
#include <iostream>
using namespace std;
int main(){
int c=10;
cout<<++c<<" "<<c++<<endl;
return 0;
}
Why did above code gave output of 12 and 10 in C++?
The computer (we tests with both cout and printf, we also tried VC++ and g++) give this to me:
12 10
Both "cout" and "printf" gave the same result.
But when we test in calculation, the result is all right.
#include <iostream>
using namespace std;
int main(){
int c=10;
int r=++c^c++;
cout<<r<<endl;
return 0;
}
Above source code gave me 0 which means while above XOR operation executing, both left hand side (++c) and right hand side (c++) giving the same value to XOR operator which is 11 (We get the value 11 by replacing c++ with 11 and computer gives the same result 0).
This is really wired. Did anyone noticed this?
By the way, we test in both debug mode and release mode in both Windows and Lubuntu. So we think this is relating to the standard library. But we aren't expecting that we can read the stdlib as a NOOB. So hoping someone can find a reason or solution.
The code int r=++c^c++; is undefined behaviour and should not be used, not ever. You can't modify the variable twice before sequence point.