Multiplication macro gives the wrong answer [duplicate] - c++

This question already has answers here:
C macros and use of arguments in parentheses
(2 answers)
C Programming Macros multiplication [duplicate]
(2 answers)
Closed 2 years ago.
#include <iostream>
using namespace std;
#define MULTIPLY(a, b) a*b
int main(){
cout << MULTIPLY(2+3, 3+5);
return 0;
}
I expected this to print 40 since five times eight is forty. Why does it print 16?

Because C++ macros are not functions. They are text copies, so that means:
cout << 2+3*3+5;
Which is 2 + (3*3) + 5

Related

what is for(auto& x : s) in C++ [duplicate]

This question already has answers here:
C++11 range based loop: How does it really work
(3 answers)
Range based for-loop with &
(3 answers)
How to correctly implement custom iterators and const_iterators?
(9 answers)
How to make my custom type to work with "range-based for loops"?
(10 answers)
Closed 21 days ago.
Since I am new to C++, I do not know what the expression means?
and how can i co-relate this expression with C language.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// string to be converted to lowercase
string s = "GEEKSFORGEEKS";
for (auto& x : s) {
x = tolower(x);
}
cout << s;
return 0;
}

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?

c++ what is the difference between initializing an object with round or curly braces [duplicate]

This question already has answers here:
What are the differences between C-like, constructor, and uniform initialization?
(2 answers)
Initialization difference with or without curly braces in C++
(3 answers)
What are the advantages of list initialization (using curly braces)?
(5 answers)
Closed 4 years ago.
In c++ what is the difference between initializing an object with round or angled braces like in this example?
#include <iostream>
#include <string>
int main()
{
std::string x = "abc";
std::string c(x);
//std::string c{x};
std::cout << "String c " << c;
return 0;
}

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 ?

What's the Difference Between These Two Casts in C++? [duplicate]

This question already has answers here:
Regular cast vs. static_cast vs. dynamic_cast [duplicate]
(8 answers)
What is the difference between static_cast<> and C style casting?
(7 answers)
Closed 9 years ago.
Code sample one:
float i=1.1f;
int j=static_cast<int>(i);
cout << j<< endl;
Code sample Two:
float i=1.1f;
int j=(int)i;
cout << j<< endl;
what's the difference between these code samples about converting in c++?