help with understading this quiz code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

help with understading this quiz code

int print(int x) { cout<<"x\0"; return x; } int main() { int y=3; cout<<print(y); }

10th May 2020, 5:39 PM
Sumer Fattoum
Sumer Fattoum - avatar
14 Answers
+ 2
There are two cout statements in the program. When main() executes its cout statement cout << print(y); this is what happens: 0. main() gets the value of y, which is 3 1. main() calls print(3) 2. The print(int x) function copies 3 into its local parameter x 3. In print(), cout sends to the console out stream the string "x\0", which was compiled as two characters 'x' and NUL, and it appears on the screen as just "x". 4. The print() function returns the value that is currently stored in its local parameter x, which is still 3. 5. main() receives the return value 3 from print(). (Remember, so far there is only "x" on the screen). The cout in main() can now complete its execution, so cout << print(3) becomes like cout << 3. 6. The cout in main() converts the value 3 into the printable string "3" and inserts the "3" into the console out stream. Now there is "x3" on the screen.
12th May 2020, 3:36 PM
Brian
Brian - avatar
+ 4
When 3 will be pass in function print(y) then it pass to print function and cout<<"x\n0"; \0 represent null so string will be end here and x will be print first then value will be return 3 . So answer is 3
11th May 2020, 9:16 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
Effectively main() calls print(3). Inside the print() function, "x\0" is a literal string of two characters: the letter 'x' [not the variable x] followed by the ASCII NUL character, which is denoted by the metacharacter '\' and '0'. Both 'x' and NUL get sent to the console using cout. Since NUL has no screen representation, you only see "x". Next, the print() function returns its value to main() and then main() prints to the console the return value. Notice that the print() function receives the value 3 in the local parameter x and simply returns the same value that was passed in to it. So main() just prints "3".
10th May 2020, 7:20 PM
Brian
Brian - avatar
+ 3
Sumer Fattoum I owe you an apology because I called them metacharacters, which is what they are called in other contexts/languages. In C and C++ they are called escape sequences. (In other contexts an escape sequence means that it starts with the literal ASCII ESC character). Also I think SoloLearn owes you an apology for not teaching about C escape sequences in their string lessons. Here is a list of escape sequences available in C/C++: https://en.cppreference.com/w/cpp/language/escape
10th May 2020, 8:53 PM
Brian
Brian - avatar
+ 2
Both \0 and \n are metacharacter codes, and they represent different characters.
10th May 2020, 7:38 PM
Brian
Brian - avatar
+ 2
The code in this question is only useful for the quiz and is not an example of good code. Don't look too deeply for its meaning. When you call a function, the function returns a value. Then the return value may be used by the code that called it. For example, int n = print(y); // y is 3 n receives the return value. It would call the print function and pass in the value that was stored inside y. Then print() would return a value that would get stored in n. It behaves as if print()'s return value gets substituted in the place of the function name, print(). Now this particular print() function happens to be coded to return the same value that it receives, int print(int x) { .... return x; } so the above call would be logically the same result as just y. [think: int n = y; // now n is 3] Then if we output n: cout << n; // shows 3 Let us remove the intermediate variable, n, and simply output the return value instead of storing it: cout << print(y); // shows 3
12th May 2020, 7:45 AM
Brian
Brian - avatar
+ 1
Pretend you are the computer executing the program. Look at each line and do what it says, recording the results on paper. Then you might understand. If you find that it is still unclear, go back and review the lessons.
10th May 2020, 6:29 PM
Brian
Brian - avatar
+ 1
ok..cool..thank you so much
10th May 2020, 9:27 PM
Sumer Fattoum
Sumer Fattoum - avatar
+ 1
nice... now it is clear..thank you so much
13th May 2020, 5:19 AM
Sumer Fattoum
Sumer Fattoum - avatar
0
the function part mostly, how a function that takes x... where the insertion here ? then cout this "x/3"..but the answer is x3 so the cout part in the function doesn't appear... then return x ... we didn't insert anything and there is no Operation here so what x could it return ? of course I tried the code line by line but I still don't understand the giving back und taking in mechanisim in functions especially here with cout<<"x/3"; which is a string that doesn't appear in output
10th May 2020, 6:39 PM
Sumer Fattoum
Sumer Fattoum - avatar
0
so do you mean that \0 is like \n ?
10th May 2020, 7:33 PM
Sumer Fattoum
Sumer Fattoum - avatar
0
ok..that was level2 quiz..I believe I didn't hear about \0 before although my level is 4..what does it do ? is it empty and does nothing ?
10th May 2020, 8:11 PM
Sumer Fattoum
Sumer Fattoum - avatar
0
hi, so print alone in main will output x...but please this combination cout<<print how does it work I still didn't work it out
12th May 2020, 6:21 AM
Sumer Fattoum
Sumer Fattoum - avatar
0
sorry if there was missunderstanding but the Output of the Programm is( x3)..i.e cout<<print(y) prints out x3 ..while if we deleted cout and let print(y) alone then will print out the charachter x..that what caused confusion for me
12th May 2020, 10:39 AM
Sumer Fattoum
Sumer Fattoum - avatar