+ 1
[SOLVED] Why is the output not "x*x" but "f(x)"?
1 #include <iostream> 2 #define f(a) a*a 3 #define s(b) #b 4 5 int main() { 6 std::cout << s(f(x)); 7 return 0; 8 } As per my understanding, first f(x) in line 6 gets replaced by x*x and then s(x*x) gets replaced by "x*x" but it seems to be not so. Even changing the order of the preprocessor directives yields the same output.
14 Respostas
+ 2
My assumption of the way preprocessing is done was incorrect. I had thought that first every f(sth) in the code will get converted to sth*sth and then every s(sth) to "sth".
The only way I can explain the output is by assuming that preprocessing is done in one go.
The preprocessor started from the first line and went down line by line. After reading lines 2 and 3, it had instructions to change any occurrence of f(..) and s(..) accordingly. In line 6 it got one suitable replacement to do when it found s(sth) i.e., to change s(f(x)) to "f(x)". That's it.
We know that preprocessing is not done in string so it skipped "f(x)".
+ 6
Is like this:
on line 3, '#' is called the stringizing operator. Thus, the 'b' parameter of s() will be a string.
on line 2 is a simple Macro definition, where the parameter of f() will be multiplied.
on line 6, the parameter of s() will be converted to string, thus "f(x)".
PS: You shouldn't change the pre-processor directive order, but the order of the expression on line 6, as f(s(x)). By the way, where did you define and declared the variable x?
Pre-processor directives are executed in the first stage of the program building (pre-processing) when the included libraries are attached to your source code, the defined Macros are replaced in your source code and the conditional compilation directives (#ifdef, #ifndef, #if, #else, #elif, #endif, #undef) are validated. In other words, there is no compilation or interpretation on #define pre-processor directive, but just a simple replace in the source code.
LE: You also have "##" or the token pasting operator. Learn what it does.
+ 6
(previous message continues) I didn't finish the C++ course, so I don't know for sure if the stringizing and token pasting operators are covered, but I know that in C course they are presented very well.
+ 4
abhinav Doesn't answer to your question?! Seriously?!
Who do you think you are? God?
Listen, you already replayed earlier to somebody in a not respectful manner, and now you do it again. Even that you delete your replay, maybe the admins can find it.
In other words I will report you! Take care!
LE: I reported your profile and this post.
+ 3
Romeo Cojocaru you've edited your response and added the line:
..on line 6, the parameter of s() will be converted to string, thus "f(x)".
Yet again, it doesn't answer correctly. Why do you think f(x) in s(f(x)) should not get converted to s(x*x) first?
+ 1
Glad to hear that abol
Thank you !
0
Romeo Cojocaru you are becoming over arrogant here. After reading your answers one can only think of what I have already written in the description. Take time to understand what really I have asked and don't just play around words.
About my reply to an earlier answer, it was unpleasant but not disrespectful; I only requested him to not answer blindly.
I found his response disrespectful for it was under estimating my awareness about my question and so is your case. Also, I deleted my reply because he deleted his so no point in keeping mine.
I have clearly stated that why the result is "f(x)" and not "x*x", have you countered that? You've written to change s(f(x)) to f(s(x)) which shows you yourself have not done your homework. An experienced C/C++ programmer would quickly get the crux of the question.
A͢J
Louis
Any suggestion for the situation?
0
Romeo Cojocaru It's like I've asked why 2+5×3 is not 21 but 17 and you telling me all about addition, multiplication and other operators (additional stuff) but not saying that there exists a precedence rule; BODMAS.
0
abhinav
It's a mistake that I answer to you one more time, but I will take my chances:
First. Yeah, I edited my response. And I edited many times. As long as I wanted to give correct information. Initially, I said that '#' is the token pasting operator, and '##' is the stringizing operator. But, I checked the C course, and the operator are reversed: '#' is stringizing and '##' token pasting. Do you have a problem with people that are extra careful? 😅 Also, I'm a teaching assistant and I teach programming to infatuated children like you, so let's advance to:
Second. "Yet again, it doesn't answer correctly. Why do you think f(x) in s(f(x)) should not get converted to s(x*x) first?" 😅
Do you write from right to left? As you, the C/C++ pre-processor evaluates the expression from left to right, so the first Macro is stringizing operator. Why put x*x when the s() parameter is a string as "f(x)", as a string? Why? Again, see what does these operators, '#' and '##'.
0
abhinav
Also, you contradict yourself saying in the initial post that "as per my understanding..." implying that you are not so sure, then responding to me that "I already knew all of these". Clearly you don't know what does those 2 operators do.
By the way, you edited your post, "It looks like you're new to it." was initially negated, but now I am a noob at programming. I have 36 years old and at least 10 years of programming. You?! 18 years old and 18 years of programming or what?! I checked, you are in highschool, so as long as you didn't do programming from kindergarten, you have the right to shut up! 😉
Please don't write any comments to me anymore. As I tried to give a detailed response to your post, I deserve to have the last word and to be left alone!
0
Romeo Cojocaru
Thank you so much for giving your time. My last request to you is to come back to this post when you're calm, perhaps after 5-10 years. Thank you once again. And sorry that I made your mood worse. 🙏🏻🙏🏻
- 1
Hii
- 2
Romeo Cojocaru It looks like you're new to it. Since s(..) will convert anything inside it to string I need not declare x for s(f(x)).
Also, your explanations don't answer my question and about the information, I already knew all of these. Thank you for giving your time.