why the code is printing an abnormal value [duplicate] - c++

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)
What (actually) happens, when a function with the warning "control reaches end of non-void function" is called?
(7 answers)
Closed 4 days ago.
the code is printing abnormal value `// C++ Program to demonstrate working of a function
#include <iostream>
using namespace std;
int max(int x, int y)
{
if (x > y)
cout<<x;
else
cout<<y;
}
// main function that doesn't receive any parameter and
// returns integer
int main(){
int a = 10, b = 20;
// Calling above function to find max of 'a' and 'b'
cout<<max(a, b);
return 0;
}
i am expecting the answer to be 20 can explain why this is happening

Related

the cpp return value without defining [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)
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
warning as Control reaches at the end of a non-void function when implementing binary search
(2 answers)
Closed 3 months ago.
enter image description here
i don't know that why this cpp program is returning the value without defining the return value
i was expecting that this program will return will print the error value
#include <iostream>
using namespace std;
int sum(int a)
{
int c = a + 1; // this fucntion is returning value of c without defining
}
int main()
{
cout << sum(25);
return 0;
}
Welcome to the world of undefined behavior if you declare a function to return a value, but do not explicitly do so. The function may appear to work, or it may do something else.
This also applies if a function has a return, but control flow means it isn't reached. For instance, in the following simple function.
int foo(bool b) {
if (b) {
return 42;
}
}
Turn on and pay attention to compiler warnings.

Function calling and return statement [duplicate]

This question already has answers here:
Omitting return statement in C++
(3 answers)
Why does this C++ snippet compile (non-void function does not return a value) [duplicate]
(7 answers)
Closed 2 years ago.
In the given C++ code when the statement n isn't present the function abc returns 10 .I.e def(10) acts as if it is a return statement but it isn't.
The output of the function is always 10 and doesn't lead to undefined behaviour when statement n as given in code isn't present in the code.
I am using sublime text 3.
So why it is behaving in such a manner or am I missing any concept
#include<bits/stdc++.h>
using namespace std;
int def(int x){
return x;
}
int abc(int x){
def(10);
return x;//statement: n
}
int main(){
cout<<abc(5);
}

Getting different results when dereferencing a pointer to an integer [duplicate]

This question already has answers here:
How to increment a pointer address and pointer's value?
(5 answers)
Closed 3 years ago.
#include<stdlib.h>
#include<iostream>
using namespace std;
void fun(int* a){
int b=*a++;
cout<<b<<endl;
}
void fun1(int *a){
int b=*a+1;
cout<<b<<endl;
}
int main(){
int n=5;
fun(&n);//output remains 5
fun1(&n);//output incremented by 1
}
In the function fun the value of n does not get incremented when done as shown in the above code, on the other hand in function fun1 the value of n gets incremented by 1.What is the problem with the first approach to increment n?
The behavior you observe is expected by the operator precendence rules.
In the first case it is a++, the previous value of a dereferenced.
In the second: the value of a dereferenced plus 1.

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).

Why does this pointer still work in Visual Studio 2012 professional? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
here is my code
#include<iostream>
using namespace std;
int *f1(int x) {
return &x;
}
void f2(int a, int b, int c){
cout<<a<<endl;
}
int main() {
cout<<"hari Hari"<<endl;
int *x;
x = f1(90);
f2(3,45,2);
cout<<*x<<endl;
system("PAUSE");
}
now as f1 return pointer to stack, which was suppose to overwritten by f2, and expected output should be looks something like this
"
hari Hari
3
3
"
but actually I get right answer, and my actual output is,
"
hari Hari
3
90
"
I can't understand why it is not overwrite the value of f1 function's 'x'.
You're relying on undefined behavior, which means you can't rely on it behaving in any particular way. In your case, you just got "lucky".