i expect the output (or value of silly) to be 36. but what i get is 14. this is fixed when i add parentheses: #define THING (4+2).
but i still want to know what is happening when there are no parentheses and why im getting an output of 14
.
the following is my code:
#include <stdio.h>
#include <stdlib.h>
#define THING 4+2
int main(void)
{
int silly = THING * THING;
printf("%d", silly);
return EXIT_SUCCESS;
}
THING*THING = 4+2*4+2 = 4+(2*4)+2 // because of higher precedence of * than +
= 4+8+2 = 14.
Do remember that MACROs are exactly replaced(substituted) into the code.
The macro is literally inserted in place of THING.
THING * THING is 4+2 * 4+2 is 4 + (2 * 4) + 2 which is 14.
If you want the result to be 36 than you need to define your macro wrapped in parenthesis:
#define THING (4 + 2)
Because macros are (almost) the same as text replacement. int silly = THING * THING; is converted by the preprocessor to int silly = 4+2 * 4+2;, and order of operations means that's processed as 4 + (2 * 4) + 2, not (4 + 2) * (4+ 2).
You need parentheses because order of operations is interfering. Macros just replace text, and so without parentheses, the operations are not being evaluated in the order expected.
THING * THING ----> 4 + 2 * 4 + 2 = 14
So, once you use parentheses, this fixes it because it becomes:
THING * THING ----> (4 + 2) * (4 + 2) = 36
This is explained in this preprocessor tutorial.
Related
I have three variables: a, b, c. I would like the program to display the results of six math and logic operations on these variables.
List of operations: +, -, *, &, |, ^,
For example:
a + b + c;
a + b - c;
a + b * c;
a + b & c;
a + b | c;
a + b ^ c;
a - b + c;
a - b - c:
a - b * c;
e.t.c.
Is there any other way to shorten the program code instead of writing all possible combinations of operations on three variables line by line?
For example, is it possible to put operators in an array and replace them in a loop?
oper_tab [6] = {'+', '-', '*', '&', '|', '^'}; // ????????
Or maybe there is another way?
I will be grateful for any comments.
You solve pretty much every problem in C by throwing enough ugly macros at it :)
In this case it would be possible to use and X-macro list. Here is an example with 2 operands.
#include <stdio.h>
// X macro list of operators supported:
#define OP_LIST(X) \
X(+) \
X(-) \
X(*) \
X(&) \
X(|) \
X(^) \
int main(void)
{
int a=1;
int b=2;
#define EXEC(op) printf("%d %s %d = %d\n", a, #op, b, (a op b));
OP_LIST(EXEC)
return 0;
}
Output:
1 + 2 = 3
1 - 2 = -1
1 * 2 = 2
1 & 2 = 0
1 | 2 = 3
1 ^ 2 = 3
Since you haven't really demonstrated any attempt to solve the problem yourself, expanding this code to use 3 operands instead is purposely left as an exercise to the reader :)
Hint: you can count the number of operators in the X macro list using stringinizing macro tricks and a compound literal:
#define STRINGINIZE(op) #op
#define COUNT_OPS(op) STRINGINIZE(op),
const size_t OP_N = sizeof (char*[]){ OP_LIST(COUNT_OPS) } / sizeof(char*);
// OP_N is now 6 for the above specified OP_LIST
I was just going through certain code which are frequently asked in interviews. I came up with certain questions, if anyone can help me regarding this?
I am totally confused on this now,
#include <stdio.h>
#include <conio.h>
#define square(x) x*x
main()
{
int i, j;
i = 4/square(4);
j = 64/square(4);
printf("\n %d", i);
printf("\n %d", j);
printf("\n %d", square(4));
getch();
}
The output is:
4
64
16
I am wondering, why did square(4) return 1 when I divided it? I mean, how can I get the value 4 and 64 when I divide it, but when used directly I get 16!!?
square is under-parenthesized: it expands textually, so
#define square(x) x*x
...
i=4/square(4);
means
i=4/4*4;
which groups as (4/4) * 4. To fix, add parentheses:
#define square(x) ((x)*(x))
Still a very iffy #define as it evaluates x twice, so square(somefun()) calls the function twice and does not therefore necessarily compute a square but rather the product of the two successive calls, of course;-).
When you write i=4/square(4), the preprocessor expands that to i = 4 / 4 * 4.
Because C groups operations from left to right, the compiler interprets that as i = (4 / 4) * 4, which is equivalent to 1 * 4.
You need to add parentheses, like this:
#define square(x) ((x)*(x))
This way, i=4/square(4) turns into i = 4 / ((4) * (4)).
You need the additional parentheses around x in case you write square(1 + 1), which would otherwise turn into 1 + 1 * 1 + 1, which is evaluated as 1 + (1 * 1) + 1, or 3.
i=4/square(4);
expands to
i=4/4*4;
which equivalent to
i=(4/4)*4;
Operator precedence is hurting you.
The macro is expanded by the pre-processor such that
i=4/4*4;
j=64/4*4;
which is equivalent to:
i=(4/4)*4;
j=(64/4)*4;
That's because the compiler replaces it with:
i=4/4*4;
j=64/4*4;
i = (4/4)*4 = 1*4 = 4.
j = (64/4)*4 = 16*4 = 64.
j = 4/square(4) == 4/4*4 == 1*4 == 4
Manually expand the macro in the code, and it will be clear. That is, replace all the square(x) with exactly x*x, in particular don't add any parentheses.
define is just a text macro
main()
{
int i,j;
i=4/ 4 * 4; // 1 * 4
j=64/4 * 4; // 16 * 4
printf("\n %d",i);
printf("\n %d",j);
printf("\n %d",square(4));
getch();
}
It's a macro! So it returns exactly what it substitutes.
i = 4/4*4; Which is 4...
j = 64/4*4; Which is 16...
Try this for your macro:
#define square(x) ((x)*(x))
Because of operator precedence in the expression after the preprocessor - you'll need to write
#define square(x) (x*x)
As the other answers say, you're getting burned by operator precedence. Change your square macro to this:
#define square(x) (x*x)
and it'll work the way you expect.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I got this code as an exercise in my school:
#include <stdio.h>
main()
{
int unknown(int a, int b)
{
if (b == 1)
return a;
else
return a + unknown(a, b - 1);
}
printf("Value = %i", unknown(3, 4));
}
The outcome is "Value=12". I can't seem to understand why.
AFAIK it should be a=3, b=4 and then 3+4, right?
The key to this is that unless b==1, unknown() calls unknown() - this is called recursion.
For brevity, I'm going to call the function f instead of unknown:
Some languages present functions like this in a clearer way with pattern matching; the equivalent in an imaginary pattern matching language might be:
f(x,1) := x
f(x,y) := x + f(x,y-1)
And so...
f(3,4) = 3 + f(3, 4-1)
= 3 + f(3, 3)
= 3 + ( 3 + f(3, 3-1))
= 3 + ( 3 + f(3, 2))
= 3 + ( 3 + ( 3 + f(3, 2 - 1)))
= 3 + ( 3 + ( 3 + f(3, 1)))
= 3 + ( 3 + ( 3 + (3)))
= 12
I guess your homework is to decide what a better name for the function is than "unknown". Once you've decided, note that recursion is not the best way to implement that function unless your language has specific support for a feature called tail recursion optimisation (this might be a topic you want to shelve for later).
Also, others have noted that nested functions are not allowed in C -- even though your particular compiler might handle them. That means that although your program does this:
int function1() {
int function2(int x) {
...
}
int x = function2(3);
}
... a standard C compiler would not allow it. The normal way is:
int function1() {
int x = function2(3);
}
int function2(int x) {
...
}
Each time a is added to a for b times
a = 3
b = 4
So, four times a gets added which means 4 * 3 = 12
Edit: a added to a, b times. -Dabo
Its the multiplication using recursive addition . Output should be 12. Finally function will return to main
3 + 3 + 3 + 3 = 12
Note: Although you used nested function here and its working, but it is not allowed by C standard. Its a compiler extension.
This question already has answers here:
C macros and use of arguments in parentheses
(2 answers)
Closed 4 years ago.
I am new to c language. I just wanted to know why is my macro not working properly. It is giving me output as 13 where as my expected output is 24.?
#include<stdio.h>
#define mult(a,b) a*b
int main()
{
int x=4,y=5;
printf("%d",mult(x+2,y-1));
return 0;
}
mult(x+2,y-1) expands to x +2 * y -1 that is equals to 4 + 2 * 5 -1 gives output: 13.
You might be expecting answer (4 + 2) * (5 -1) = 6 * 4 = 24. To make it expand like this you should write parenthesize macro as #H2Co3 also suggesting:
#define mult(a,b) ((a)*(b))
Read aslo: So, what's wrong with using macros? by Bjarne Stroustrup.
This is because C macros are simple textual substitutions, the macro writer must be sure to insert parentheses around every macro variable when it is substituted, and around the macro expansion itself, to prevent the resulting expansion from taking on new meanings.
If you observe your program: mult(a, b) is defined as a * b
mult(x + 2, y - 1) = x + 2 * y - 1 = 4 + 2 * 5 - 1 = 4 + 10 - 1 = 13
The Correct way would be:
mult(a, b) ((a) * (b))
Use parentheses in the macro definition
#include<stdio.h>
#define mult(a,b) ((a)*(b))
int main()
{
int x=4,y=5;
printf("%d",mult(x+2,y-1));
return 0;
}
This is because different arithmetic operators have different precedence levels. Hence always use parentheses while defining the macro.
Because it replaces the arguments literally:
mult(x+2,y-1) --> mult(4+2,5-1) --> 4 + 2*5 - 1 --> 13
Try changing the define to:
#define mult(a,b) (a)*(b)
In this case the result after pre-processing is this:
int main()
{
int x=4,y=5;
printf("%d",(x+2)*(y-1));
return 0;
}
This will solve the problem but it's still not the best way to do it.
#define mult(a,b) ((a)*(b))
This version is considered as good practice because in other types of situation the first one would fail. See the bellow example:
#include<stdio.h>
#define add(a,b) (a)+(b)
int main()
{
int x=4,y=5;
printf("%d",add(x+2,y-1)*add(x+2,y-1));
return 0;
}
In this case it would give an incorrect answer because it is translated by the pre-processor to the fallowing:
int main()
{
int x=4,y=5;
printf("%d",(x+2)+(y-1)*(x+2)+(y-1));
return 0;
}
printing 34 instead of 100.
For the ((a)+(b)) version it would translate to:
int main()
{
int x=4,y=5;
printf("%d",((x+2)+(y-1))*((x+2)+(y-1)));
return 0;
}
giving a correct answer.
Why is the answer for the below code 16? Can anybody explain the working of this program?
#define SQUARE(n) n*n
void main()
{
int j;
j =16/SQUARE(2);
printf("\n j=%d",j);
getch();
}
If we write the same code like below, then the answer is 4:
//the ans is 4 why?
#include<stdio.h>
#include<conio.h>
#define SQUARE(n) n*n
void main()
{
int j;
j =16/(SQUARE(2));
printf("\n j=%d",j);
getch();
}
The preprocessor just replaces the text, exactly as written.
So, the macro call SQUARE(2) becomes literally 2*2.
In your case, that means the whole expression becomes 16/2*2, which because of C's precedence rules evaluates to (16/2)*2, i.e. 16.
Macros should always be enclosed in parenthesis, and have each argument enclosed as well.
If we do that, we get:
#define SQUARE(n) ((n) * (n))
which replaces to 16/((2) * (2)), which evaluates as 16/4, i.e. 4.
The parens around each argument makes things like SQUARE(1+1) work as expected, without them a call such as 16/SQUARE(1+1) would become 16/(1+1*1+1) which is 16/3, i.e. not at all what you'd want.
Order of operations. Your expression is evaluating to:
j = 16 / 2 * 2
which equals 16. Make it:
#define SQUARE(n) (n*n)
which will force the square to be evaluated first.
You need to define your macro with insulating parentheses, like so:
#define SQUARE(n) ((n)*(n))
Otherwise
j = 16/SQUARE(2);
expands to
j = 16 / 2 * 2; which is equivalent to (16 / 2) * 2
When what you want is
j = 16 / (2 * 2);
1. When using macros that are to be used as expressions, you should parenthesise the whole macro body.
This prevents erroneous expansions like:
#define SQUARE(x) x*x
-SQUARE(5,5)
// becomes -5 * 5
2. If the macro arguments are expreessions, you should parenthesise them too.
This prevents a different type of problems:
#define SQUARE(x) x*x
SQUARE(5+2)
// becomes 5 + 2*5 + 2
Hence the correct way is to write it like this:
#define square(n) ((n)*(n))
-SQUARE(5+2)
// becomes -((5+2)*(5+2))
Using macros as functions is discouraged though (guess why), so use a function instead. For instance:
inline double square(n) { return n*n; }
The Expansion of macro will be like:
j = 16/SQUARE(2);
j = 16/2*2;
Which is equal to : j = (16/2)*2; Means j = 16;
and :
j = 16/(SQUARE(2));
j = 16/(2*2);
Which is equal to : j = 16/4; Means j = 4;
Because the macro will be expanded as:
j = 16/2*2;
The pre-compiler does not do any processing on the expansion. It places the expanded macro in your code as it is. Since you have not parenthesized the replacement text it wont do it for you in the main code as well. Make it :
#define SQUARE(n) ((n)*(n))
The first example is evaluated as:
16 / 2 * 2
(16 / 2) * 2
8 * 2
16
The second example is evaluated as:
16 / (2 * 2)
16 / 4
4
Add parenthesis to you preprocessor statement to control the order of operations:
#define SQUARE(n) ((n)*(n))
The outer parenthesis in ((n)*(n)) ensure that n is squared before any outside operation is performed. The inner parenthesis (n) ensure that n is correctly evaluated in cases where you pass an expression to SQUARE like so:
16 / SQUARE(2 * 2)
16 / ((2 * 2)*(2 * 2))
16 / (4 * 4)
16 / 16
1
you'll get
j =16/2*2; // (16 / 2) * 2 = 16
Its because Whenever macro name is used, it is replaced by the contents of the macro.its simple rule of working of macro.
Case 1 : result 16
define SQUARE(n) n*n
void main()
{
int j;
j =16/SQUARE(2);
printf("\n j=%d",j);
getch();
}
its get expand as below
j =16/SQUARE(2);
so in place of SQUARE(2) it will replace 2*2 because Macro is SQUARE(n) n*n
j = 16/2*2
j = (16/2)*2
j = 8*2
j =16
Case 2 : result 4
define SQUARE(n) n*n
void main()
{
int j;
j =16/(SQUARE(2));
printf("\n j=%d",j);
getch();
}
its get expand as below
j =16/(SQUARE(2));
so in place of SQUARE(2) it will replace 2*2 because Macro is SQUARE(n) n*n
j = 16/(2*2)
j = 16/(4)
j = 4
Hope this will help