0

Quick question in language C... đŸ€”

#define x 10 + 5 int main() { printf("%d\n", x); // Print the value of x int a = x * x ; printf("%d\n", a); // Print the value of a return 0; } Why a = 65 ?

3rd Jun 2024, 8:45 PM
Artur Manuel
Artur Manuel - avatar
11 Answers
+ 2
Part or parts of code containing a macro name (here the macro name is <x>) will be replaced by the respective macro's value (here <x> is defined as 10 + 5) prior to code compilation. But be careful, the replacement will be performed as is. That means when preprocessor sees a part of code contains `x * x`, it will replace each presence of <x> there with '10 + 5' - and that part of code then becomes `10 + 5 * 10 + 5` And with operator precedence in mind, `10 + 5 * 10 + 5` will be treated as `10 + (5 * 10) + 5` - I used parentheses here to emphasize that multiplication is prioritized over addition. This explains why 65 was the result stored into variable <a>. How to overcome this? First option, make sure the macro value is an actual constant value (e.g. 15) rather than an evaluable expression (e.g. 10 + 5). Such that when the replacement takes place, each <x> will be replaced with a value 15 #define x 15 This way `x * x` will be replaced into `15 * 15` Second option, if the macro's value looks more like an evaluable expression rather than a constant value; wrap the expression in parentheses, to force use the expression evaluation result during replacement instead of the raw expression #define x ( 10 + 5 ) This way, macro <x> will have the evaluation result from expression 10 + 5 (15) rather than the expression itself. It was like telling the code processor to find macro name in code, and replace each of them with whatever was there following the macro name (in #define part). But this was an automated process done before compiler kicks in. Raw text replacement if I may say :)
4th Jun 2024, 6:29 AM
Ipang
+ 5
Artur Manuel your int a = x * x recognizing macro as 10 + 5 * 10 + 5 Observe the behavior in the following code https://sololearn.com/compiler-playground/c8497uQNCVIA/?ref=app https://www.sololearn.com/discuss/2983854/?ref=app https://www.sololearn.com/discuss/1646655/?ref=app
3rd Jun 2024, 11:48 PM
BroFar
BroFar - avatar
+ 2
BroFar when you use the word 'store' looks like you meaning that stored in memory that's wrong. We can tell it temporary hold something. This 'something' is not always a constant value like '#define PI 3.14', can be a inline code like we have here. '10 + 5' is an inline code, not a constant value, not a variable, not a function. In your own example the calculation performed after the preprocessor resolve all the defines and more specific way much later at runtime and only if need to (for example if is inside an if case that never run, will never perform the calculation) (*). So the define itself not perform any calculation. The calculation is executed exactly when you about to call printf. Its the same as doing this: printf("%d", 10 + 5); A simple test to see that the x is not stored in memory like variables do, is to try print it's memory address. You will see compiler will not let you do it. (*) Note here, is these normally happened only if all compiler optimizations are turned off.
4th Jun 2024, 1:48 PM
john ds
john ds - avatar
+ 1
john ds c language deals frequently with ascii characters ... even in mathematics
4th Jun 2024, 12:42 AM
BroFar
BroFar - avatar
+ 1
https://www.sololearn.com/Discuss/1646655/?ref=app The best answer why is 65 instead of 225, given here, at the 1st position but is half. What he didn't said, is a #define is not a constant variable that will be stored somewhere, this is wrong. When the compiler see a define, is simply copy-paste what you have there, without perform any calculation. You can imagine it like when you have a essay and you replace all words 'ofc' with 'of cource' before do anything else with it. Define do the same, will replace temporary in your code the word 'x' with '10 + 5' (except in string values) before compiler do anything else
4th Jun 2024, 1:12 AM
john ds
john ds - avatar
+ 1
john ds define is more or less an equational Macros declared using #define are used to store constants but macros are not constants themselves and cannot be changed. As to no calculations being performed I find this error as proof that x as define as x 10 + 5 respectfully identified as line 11 on my code not as 10 + 5 but as 15.
4th Jun 2024, 1:42 AM
BroFar
BroFar - avatar
+ 1
Yes I see. Math priority calculations. Thank you !
4th Jun 2024, 5:15 AM
Artur Manuel
Artur Manuel - avatar
+ 1
Go on
6th Jun 2024, 11:13 AM
AI Tech, Network.
AI Tech, Network. - avatar
0
BroFar what do you mean 'recognized a character value for the letter A'? I see only numbers and the variable a is integer type and noone from the questions or answers have attempted to print it as character except you, i cannot understand why, and most important how is the relevant to his question. Its only math operations and integers here. Is it a specific challenge or course of sololearn that do this?
4th Jun 2024, 12:32 AM
john ds
john ds - avatar
0
What is x
5th Jun 2024, 7:45 AM
AI Tech, Network.
AI Tech, Network. - avatar
0
It's a variable. There is a lesson called " Tech for everyone" in Sololearn They explain it really well.
5th Jun 2024, 11:33 AM
Artur Manuel
Artur Manuel - avatar