what is the output of this code and how?? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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); }

3rd Jan 2022, 6:21 AM
Nariman Tajari
Nariman Tajari - avatar
18 Antworten
+ 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
3rd Jan 2022, 6:57 AM
saurabh
saurabh - avatar
+ 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)
3rd Jan 2022, 6:57 AM
A͢J
A͢J - avatar
+ 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
3rd Jan 2022, 6:44 AM
A͢J
A͢J - avatar
+ 4
A͢J yeah thanks 👌🙌
3rd Jan 2022, 6:59 AM
Shino
Shino - avatar
+ 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
3rd Jan 2022, 6:48 AM
Shino
Shino - avatar
+ 2
thanks A͢J and ROOKIE for insighitful points and also Inumaki .
3rd Jan 2022, 9:32 AM
Nariman Tajari
Nariman Tajari - avatar
+ 1
Nariman Tajari yeah to get the expected o/p , Look at A͢J comment. Also keep in mind that i have suggested. Thanks...
3rd Jan 2022, 7:02 AM
saurabh
saurabh - avatar
+ 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.
4th Jan 2022, 3:48 AM
Muhammad Rizwan
Muhammad Rizwan - avatar
+ 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
4th Jan 2022, 10:50 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
nayoR Notepad++ is used to edit html and don't ask question in others question.
5th Jan 2022, 6:20 AM
A͢J
A͢J - avatar
+ 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
14th Jan 2023, 10:15 AM
Darshan Jain
Darshan Jain - avatar
+ 1
BODMAS - Brackets Off Division Multiplication Addition Subtraction.. Answer is 28 / 4 * 4 = 7 * 4 = 28 (L - R)
14th Jan 2023, 3:49 PM
Sanjay Kamath
Sanjay Kamath - avatar
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
3rd Jan 2022, 6:35 AM
Shino
Shino - avatar
- 1
1
4th Jan 2022, 7:31 PM
Nandan Kumar
Nandan Kumar - avatar
- 2
28...!!!
4th Jan 2022, 1:16 AM
Kolli. Sri Lakshmi
- 2
I'm new here so I'm just going to watch and learn
4th Jan 2022, 9:13 PM
Sadiq Abdulhamid
- 2
what type of editor is used to html code
5th Jan 2022, 6:16 AM
nayoR
- 3
square(4) =4*4=16 from define i=28/16 Ans is 1
4th Jan 2022, 11:37 AM
Vishal Gupta
Vishal Gupta - avatar