C Preprocessor Instructions (SQR-Funktion) [duplicate] - c

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 6 years ago.
Want to know how "11" is the answer of this c preprocessor instruction:
#define SQR(x) x*x
int main()
{
SQR(2+3);
}

Try expanding the macro manually.
It will be 2+3*2+3 and this is evaluated as 11.

Related

Computational error of pow function in calculation of mathematical expression in language c [duplicate]

This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Why does pow(5,2) become 24? [closed]
(3 answers)
Closed 2 years ago.
This code is part of an original code
Why the output of this code displays the number 24?
The output of this Code should be number 25!
Where's the problem with this code?
The compiler used is CodeBlocks
Thanks
int A=7;
A = pow((((A+1)/2)+1),2);
printf("%d", A);

usage of !!, __warned and __ret_warn_once as int in WARN_ON_ONCE macro definition [duplicate]

This question already has answers here:
What is "!!" in C? [duplicate]
(7 answers)
!! c operator, is a two NOT?
(4 answers)
Closed 3 years ago.
I was going through the WARN_ON_ONCE macro definition. I have doubt regarding the following line, what is the use of !! before condition. If we remove !! then also same will be stored in __ret_warn_once.
int __ret_warn_once = !!(condition);
What will happen when compiler executing the following source line.
static bool __section(.data.unlikely) __warned;

What does this " #define CONCAT_3(p1, p2, p3) p1##p2##p3" macro definition mean? [duplicate]

This question already has answers here:
The ## operator in C
(7 answers)
Closed 5 years ago.
I have seen a macros with arguments but this is quite new.
I'm unable to understand what it will return and how it works?
## is the preprocessor concatenation operator.
It joins the arguments together.
Thus CONCAT_3(foo, bar, foobar) will produce foobarfoobar.

what is the output of this according to the macro definition [duplicate]

This question already has answers here:
Strange behavior of Macro-expansion
(3 answers)
Why is the return value not what I expected in this C program with macro? [duplicate]
(2 answers)
Closed 7 years ago.
#define A 1+2
#define B 4+3
int main()
{
int val = A*B;
printf("%d",val);
}
Here A and B is defined, and val is (A*B). What is the correct output?
Let's trace the expansion and calculation manually.
A*B -> 1+2*4+3 -> 1+8+3 -> 12
As a result, the output will be 12
This is a common issue with Macros. When you define something even as x+y in a macro it is advisable to first rap them in () so as to "protect" the operation.
As such it would be better to define A as (1+2) etc. Otherwise you get the output 1+2*4+3=12 as people have stated above.

issue with the conditional operator [duplicate]

This question already has answers here:
C conditional operator ('?') with empty second parameter [duplicate]
(6 answers)
Closed 9 years ago.
#include<stdio.h>
int main()
{
printf("%d\n", 4 ?: 8);
}
According to the C standard this program is invalid because it is missing an expression between the ? and :.But The interesting thing is that there is when I compile the code it is printing 4.how come it will print 4 rather than showing any compile error
This is a gcc extension.
x ? : y
is equivalent to
x ? x : y
See here for detail.

Resources