#define process in C++ [duplicate] - c++

This question already has answers here:
Macro Expansion
(7 answers)
How #define works in Programming when define has value with operator?
(4 answers)
How does #define work in C++ [duplicate]
(1 answer)
Closed 1 year ago.
I am trying C++, There is a point about can not understanding about #define addition. The example code below.
#include <iostream>
using namespace std;
#define A 0
#define B A+1
#define C 3-B
int main(){
cout << A << endl;
cout << B << endl;
cout << C;
return 0;
}
The result gives A -> 0, B -> 1, C-> 4. How C equal 4 ?

#define performs simple textual substitution. When you expand B out, you get 0+1 in source code, which is not necessarily identical to "an integer with the value 1".
So, in your example code, if we substitute the values in:
int main(){
cout << 0 << endl;
cout << 0+1 << endl;
cout << 3-0+1;
return 0;
}
3 - 0 + 1 is 4.

Do the text substitution. B expands to 0+1. C expands to 3-0+1 rather than 3-1.
cout << C;
Becomes:
cout << 3-0+1;
Because of order of operations this displays 4 rather than 2.
An easy way to see this would be to try something like:
cout << C * 50;
If we operate under the faulty assumption that C is actually 4, we'd expect to see 200. But because the above is equivalent to:
cout << 3-0+1 * 50;
Then we correctly see 53.

Related

Swap two numbers without a third in C/C++ [duplicate]

This question already has answers here:
Swapping two variable value without using third variable
(31 answers)
Closed 6 years ago.
we usually use the
a=a+b;
b=a-b;
a=a-b;
logic to solve this code, however, if we work with int, then after say 30000 the code fails, if we take long, it fails after say 1000000 or so. My objective is, not to increase the length of the code yet, do the same operation. I have already tried using a BIT wise XOR,
a = a ^ b;
b = a ^ b;
a = a ^ b;
Still it didn't help, any ideas?
To swap a variable a and a variable b: std::swap(a, b);
Example:
int a = 10;
int b = 20;
std::cout << "Before swap\n";
std::cout << "Value of a: " << a << '\n';
std::cout << "Value of b: " << b << '\n';
std::swap(a, b);
std::cout << "After swap\n";
std::cout << "Value of a: " << a << '\n';
std::cout << "Value of b: " << b << '\n';
Output using GCC 4.9.2:
Before swap
Value of a: 10
Value of b: 20
After swap
Value of a: 20
Value of b: 10
This way of doing it uses rvalues internally, so it has next to zero overhead for other use cases and won't overflow for any primitive type ever

C++ rand() why give me the same number How can i have different [duplicate]

This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 7 years ago.
if (ii == 3){//车辆只停了一个小时,有3/4的概率离场
srand((unsigned)time(NULL)*100);
int a = 1+rand() % 4;
int b = 1+rand() % 4;
int c = 1+rand() % 4;
cout << "++++3 " << a << "++++" << endl;//用作测试判断是否离场函数
cout << "++++3 " << b << "++++" << endl;
cout << "++++3 " << c << "++++" << endl;
cout << "*************" << endl;
if ((a == 1 && b == 2 && c == 3)||(a==2&&b==1&&c==3)||(a==3&&b==2&&c==1)||(a==3&&b==2&&c==1))
return true;
else
return false;
}
Just like this picture, the program gives me the same number ,but I need different numbers , how can I do it.
#ifndef Head_H
#define Head_H
#include<stdlib.h>
#include<iostream>
#include<time.h>
#define PathSize 5
#define ParkSize 10
#define Price 5
using namespace std;
srand((unsigned)time(NULL));
Use srand once, at the start of your program.
If you repeatedly re-seed the generator then you ruin its statistical properties.
Moving on:
Avoid using % to rescale the generator. This will introduce statistical bias (unless by pure luck the modulo is a multiple of RAND_MAX).
Look at the C++11 random number library. This contains better-specified generators than plain old rand, which is awful on some platforms.

Why do I get different outputs from different compilers for this code? [duplicate]

This question already has answers here:
Order of execution in operator <<
(4 answers)
Closed 8 years ago.
The code is simple:
int Change(int& a)
{
a = 4;
return a;
}
int main()
{
int a = 10;
cout << Change(a) << a;
}
In C-Free : the output : 4 4
In VS2008 : the output : 4 10
Why? As I have learned, I think 4 4 is right.
Simply put, there is no rule that guarantees that "4 4" is right. Same goes for "4 10".
As others have mentioned in the comments you have a case of undefined behaviour here. Even if this would be defined behaviour, code like this is difficult to understand. So I recommend to do
cout << Change(a);
cout << a;
or
int b = a;
cout << Change(a);
cout << b;
whatever you really want to do.

Calculating modulus in a function gives different answer from using % operator directly [duplicate]

This question already has answers here:
Modulo operator with negative values [duplicate]
(3 answers)
Closed 9 years ago.
my_mod.cpp:
#include <iostream>
using namespace std;
unsigned int my_mod(int a, unsigned int b)
{
int result;
cout << "\ta\t=\t" << a << endl;
cout << "\tb\t=\t" << b << endl;
cout << "\ta%b\t=\t" << a%b << endl;
result = a%b;
if (result < 0) {result += b;}
return result;
}
int main()
{
cout << "-1%5 = " << -1%5 << endl;
cout << "my_mod(-1,5) = " << my_mod(-1,5) << endl;
return 0;
}
compiled via:
g++ ./my_mod.cpp
results in:
-1%5 = -1
a = -1
b = 5
a%b = 0
my_mod(-1,5) = 0
What the actual hell is happening here I just can't understand what possibly could go on?! This can't be due to the global scope, right?!
I mean it is exactly the same %-expression ... how can they yield 0 and -1?! (Instead of the desired 4, by the way.)
Please, if anybody can, explain this to me ... it just took me days to narrow down an error in a wider context to this. Seriously, I'm about to cry.
How can I have my (global) own modulus returning 4 in the example above??
It's because you're using an unsigned int, the signed int (-1) gets promoted to -1 % UINT_MAX so your operation becomes (-1 % UINT_MAX) % 5 = 0 (thanks to jrok for this more detailed reason)
try
cout << "\ta%b\t=\t" << a%(int)b << endl;
result = a%(int)b;
with function signature of: int my_mod(int a, unsigned int b)
Or just use a function signature of: int my_mod(int a, int b)

order of print in c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the correct answer for cout << c++ << c;?
I have following code -
int a= 7;
const int &b = a;
int &c = a;
if I use
cout << endl << ++c << '\t' << a << '\t' << b << '\t' << c;
it prints
"8 7 7 8"
however if I use
cout << endl << a << '\t' << b << '\t' << ++c << '\t' << a << '\t' << b << '\t' << c;
it prints
"8 8 8 8 8 8"
How exactly this happens ? Is it something related to optimization ?? If yes, how can i switch it off in ideone.com ???
Effectively the operator<< is a function call, c++ is allowed to evaluate the arguments passed to a function in any order it likes, hence the ++c inc is done first, quite legally, by your compiler - mine does something different.
Interstingly my compiler prints
8 8 8 7 7
Some compilers provide switches for order of evaluation of function params, but if you really need to use it I would question myself on the reasons for this as there is something much more wrong with the code and instead write it in a portable way.
a, b, and c are all the same object. The order in which the arguments to functions are evaluate is undefined, however. So, you whatever the compiler chooses to evaluate first is OK. It seems, in your second expression it evaluates ++c first. The way to avoid problems is not to fold the modification with the rest of the expression, i.e., to increment c either before or after the output.