#define SQR(x) x*x. Unexpected Answer
The approach of squaring with this macro has two problems:
First, for the argument ++p
, the increment operation is performed twice. That's certainly not intended. (As a general rule of thumb, just don't do several things in "one line". Separate them into more statements.). It doesn't even stop at incrementing twice: The order of these increments isn't defined, so there is no guaranteed outcome of this operation!
Second, even if you don't have ++p
as the argument, there is still a bug in your macro! Consider the input 1 + 1
. Expected output is 4
. 1+1
has no side-effect, so it should be fine, shouldn't it? No, because SQR(1 + 1)
translates to 1 + 1 * 1 + 1
which evaluates to 3
.
To at least partially fix this macro, use parentheses:
#define SQR(x) (x) * (x)
Altogether, you should simply replace it by a function (to add type-safety!)
int sqr(int x)
{
return x * x;
}
You can think of making it a template
template <typename Type>
Type sqr(Type x)
{
return x * x; // will only work on types for which there is the * operator.
}
and you may add a constexpr
(C++11), which is useful if you ever plan on using a square in a template:
constexpr int sqr(int x)
{
return x * x;
}
Macroprocessor in c++
The macro expands to a literal
(b + 1 * b + 1)
So your result is:
3 + (1 * 3) + 1.
Change your macro to:
#define SQR(x)((x)*(x))
And it should work.
Confused by squaring macro SQR in c
SQR(1+1)
expands to 1+1*1+1
which is 3, not 4, correct?
A correct definition of the macro would be
#define SQR(x) ((x)*(x))
which expands to (1+1)*(1+1)
and, more important, shows you one of the reasons you shouldn't use macros where they aren't needed. The following is better:
inline int SQR(int x)
{
return x*x;
}
Furthermore: SQR(i++)
would be undefined behavior if SQR
is a macro, and completely correct if SQR
is a function.
Unexpected behavior of macro
Macros are a simple text substitution mechanism. In your example, you have the following macro:
#define f(a,b) a*b
When you use that macro, the text a*b
is substituted for the macro call, replacing a
and b
with whatever you used as parameters.
In your first example, f(2*2,3*2)
becomes 2*2*3*2
, which does what you expect and outputs 24. However, in your second example, f(2+1,3+1)
becomes 2+1*3+1
, which is interpreted as 2+(1*3)+1
and thus outputs 6.
You can fix this by defining the macro differently:
#define f(a,b) ((a)*(b))
Notice the extra set of parentheses. This ensures that the macro is evaluated before anything else in the expression. The extra parentheses around a
and b
ensure that the parameters are evaluated first.
Preprocessors in C
In C, macros are filled into your code upon compilation. Let's consider what happens in your case:
Your macro is:
#define sqr(x) x*x
When it gets filled into this:
int x = 16/sqr(4);
You would get:
int x = 16/4*4;
Operator precedence being equal for /
and *
, this gets evaluated as (16/4)*4 = 16
, that is, from left to right.
However, if your macro were this:
#define sqr(x) (x*x)
then you would get:
int x = 16/(4*4);
... and that reduces to 1
.
However, when the argument for your macro is more complex than a simple number, e.g. 2+3
, it would still go wrong. A better macro is:
#define sqr(x) ((x)*(x))
C++ weird long long division (not common type conversion error)
Macros are just simple text replacements. UPPER / second
will be replaced with (ll)1e18 + 1 / second
which is always equal to (ll)1e18
unless second is 1
You must use parentheses around that
#define UPPER ((ll)1e18 + 1)
#define directive in C Giving ambiguous answer
Macros do substitution only(done before compilation of the code).
Therefore the following line
printf("%d",225/SQR(15));
after substitution will become:
printf("%d",225/15*15);
now this expression evaluates to 225 (basic maths : divide first -> 15*15, then multiply -> 225)
using brackets solves your problem(then it becomes 225/(15*15)).
:)
Trying to define a function in R but it turns out to be object instead
power <- function(x, n) x^n
square <- function(x) power(x, 2)
cube <- function(x) power(x, 3)
square(3) # 9
cube(3) # 27
Given the following definition of x
:
x = c(1,2,3,4,5)
You can run
> square(x)
[1] 1 4 9 16 25
And you will receive a numeric vector. If you do
square = power(x, 2)
You receive the same result. In the first example, square
is a function. In the second example, square
is a resulting vector of squares.
Related Topics
Better Variable Exploring When Debugging C++ Code with Eclipse/Cdt
How to Use Other C++ Compilers with Cuda on Windows
C++11 Std::Async Doesn't Work in Mingw
What Is the Purpose of Std::Make_Pair VS the Constructor of Std::Pair
Why Is a C++ Reference Considered Safer Than a Pointer
What Does "-Wall" in "G++ -Wall Test.Cpp -O Test" Do
Format Curly Braces on Same Line in C++ VScode
Static Member Variable in a Class
How to Send Keystrokes to a Window
C++ Gdi::Bitmap to Png Image in Memory
Problems with C++ Set Container
Compiler Cannot Recognize My Class in C++ - Cyclic Dependency
Create 2D Array Using Size from Parameters in C++
Is Substitution Performed on a Variadic Parameter Pack Type If the Pack Is Empty
In C++ Why Can't I Write a For() Loop Like This: For( Int I = 1, Double I2 = 0;