Can anyone explain this code behaviour in c++? [duplicate] - c++

This question already has answers here:
Is subtracting larger unsigned value from smaller in C++ undefined behaviour?
(2 answers)
Is unsigned integer subtraction defined behavior?
(6 answers)
Closed 3 years ago.
vector<int> a(10);
vector<int> b(20);
if ((a.size() - b.size()) > 1) {
cout << "yes";
}
Can anyone explain why this code prints "yes". If I try storing a.size() - b.size() in an int variable, it is working as expected.

Related

looks like there is something wrong with the pre-increment operator in c++ [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 19 days ago.
I was just messing with a piece of cpp code that I came across something I couldn't find an explanation for.
when I run this in c++:
int a = 5;
cout << (--a) * (--a);
9 is printed in the console. but when you get the variable (a) from the input, and then give it the same value (5), the result is different:
int a;
cin >> a;
cout << (--a) * (--a);
when you enter 5 as the input, 12 is printed in the console. Why is it so?

Why does the expression X["Y"] behave as "Y"[X]? [duplicate]

This question already has answers here:
integer indexed with a string in c++ [duplicate]
(3 answers)
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 4 years ago.
Consider the below syntax :
#include <iostream>
int main() {
std::cout << 1["ABC"] << std::endl;
}
The program outputs B which leads me to believe that it behaves as "ABC"[1]. Is that possible, what is the reason behind this ?

Post Increment in C++ [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I know the concept of post increment but how does it apply to the follow? The output of t is 10. How to explain the undefined behavior?
int a = 2;
int b = 3;
int t;
t = a++ * (a+b);
That is undefined behavior you have there.

Indexing array incorrectly in c++ [duplicate]

This question already has answers here:
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 9 years ago.
Today when i was coding inside my visual studio i unintentionally did following
for(int i=0;i<10;i++)
{
cout<<"Value is"<<[i]arr<<endl;
}
instead of arr[i] and it worked.why it worked?
Because [i]arr == *(i + arr) == arr[i]
Note: + operator holds commutative property

Weird behavior while printing character array in file in C [duplicate]

This question already has answers here:
Returning a reference to a local or temporary variable [duplicate]
(5 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Return address of local variable in C
(5 answers)
Closed 9 years ago.
I saw some weird behavior with my C++ function whose work is to simply put 'n' ones(1).
char *getSpaces(int n) {
char s[50];
int i = 0;
for(i=0; i<n; i++) {
s[i] = '1';
}
s[i] = 0;
return s;
}
When I do fout<< getSpaces(20), I get following output in the file :-
1111111111SOME_WEIRD_CHARACTERS_HERE
Can anybody explain this?
P.S. I am using codeblocks IDE on windows platform.