+ 2
what is the output of this code and how??
#include<studio.h> #define square (x) x*x void main() {int i=28/square(4)); printf("%d",i); }
19 Answers
+ 6
Nariman Tajari
♤Correction...................
▪︎#include <stdio.h>
♤Important Suggestions......
-- be aware with the use of macros , The preprocessor doesn't know C/C++ ,it does just a simple substitution.
For ex.
#define MAX 100
The preprocessor will first remove the #define statement from the code, then it goes through the program and replace the all MAX with 100,
Now the processed file is sent to the compiler.
For ex.
#define SQUARE(a) a*a
.........
result = SQUARE(5); // Expect 25
result = 5*5; // Get 25
result = 100/SQUARE(5); // Expect 4
result = 100/5*5; //Get 100, not expec
//simple subs👆titution
So now you see, you get the idea , why should we avoid to use macros in our code.
*use const instead of #define
+ 5
Inumaki
It is simple Macro is a piece of code which is replaced by the value of macro.
So if square (x) = x * x
Then square (4) = 4 * 4
If square (x) = (x * x)
Then square (4) = (4 * 4)
+ 4
Nariman Tajari
int i = 28 / square(4)
Since there is no parenthesis in defined macros like this (x * x)
So calculation would be like this:
i = 28 / 4 * 4;
i = (28/4) * 4
i = 28
If there is (x * x)
Then i = 28 / (4 * 4) = 1
+ 4
A͢J yeah thanks 👌🙌
+ 3
A͢J ah u are correct when i was running his code and fixing those typo and brackets i actually put parenthesis on macros i forgot that he didn't put them
looks like i have to downvote my own answer lol
thanks for clarifying
+ 1
Nariman Tajari yeah to get the expected o/p ,
Look at A͢J comment.
Also keep in mind that i have suggested.
Thanks...
+ 1
#include<stdio.h> // Not studio.h
#define square (x) x*x
void main()
{
int i=28/square(4);
/* Basically it works as:
i=28/4*4;
i=7*4;
i=28; */
printf("%d",i);
}
And the output of this code is 28.
+ 1
BODMAS RULE APPLIES :
==>28/7*4
==(28/7)*4 [Division precedes multiplication]
=4*4=16
if the definition were (4*4) with parentheses, then the results would be different
+ 1
nayoR
Notepad++ is used to edit html and don't ask question in others question.
+ 1
Answer is 28
square(4) = 4*4
i = 28 / 4 * 4
as division has higher precedence order therefore
i = (28/4)*4
i = 7*4
i = 28
+ 1
BODMAS - Brackets Off Division Multiplication Addition Subtraction..
Answer is 28 / 4 * 4 = 7 * 4 = 28 (L - R)
0
> "studio" i guess you got typo its "stdio"
> you got an extra bracket after (4)
After resolving them
> Output should be 1 with warning return type of 'main' is not `int'
> output [ redacted] #see A͢J 's answer I actually messed up here :'')
> Warning will be there because compiler telling you main() must return int
0
I
- 1
1
- 2
28...!!!
- 2
I'm new here so I'm just going to watch and learn
- 2
what type of editor is used to html code
- 3
square(4) =4*4=16 from define
i=28/16
Ans is 1