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

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++?

Related

How does int[int*] work in cpp? what is it's output? [duplicate]

This question already has answers here:
C++ array[index] vs index[array] [duplicate]
(4 answers)
is int[pointer-to-array] in the C++ - standard? [duplicate]
(3 answers)
Closed 2 years ago.
int a[5]={1,2,3,4,5};
int*p=a;
cout<<4[p];
return 0;
can anyone please tell me how it is giving output 5. i know it is 5 because it is the 4th element in the array. but why is 4[p] giving the output!

Multiplication macro gives the wrong answer [duplicate]

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

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

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.

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 ?

Why --(-val) is not working? [duplicate]

This question already has an answer here:
Feeling confused with -(--a) vs --(-a) in c
(1 answer)
Closed 7 years ago.
int val = -10;
cout << ++(-val) << endl;
Output should be 11. But it gives me an error.
The error is "lvalue required as increment operand". This means that it's just a value; it does not represent a particular object like "val" anymore so trying to change it makes no sense to the compiler.