+ 3

C programming question

What is the value of x after running the following C code? int x = 1, y = 2; if (x == y) y = x +10; x = y + 8; x++;

20th Oct 2021, 7:49 PM
Mr. E
Mr. E - avatar
28 Answers
+ 5
Mr. E , the code does not anything output. may be you want to show us the complete code?
20th Oct 2021, 7:57 PM
Lothar
Lothar - avatar
+ 4
Mr. E in that case better questions would be: "What's wrong with this code?" "Why does this output 0?" Although I don't see how it can output anything without any kind of output statement. ¯\_(ツ)_/¯
20th Oct 2021, 7:59 PM
Simon Sauter
Simon Sauter - avatar
+ 3
So my guess, as someone who knows virtually nothing about C, is that the value of x is 11 but the program has no output.
20th Oct 2021, 8:17 PM
Simon Sauter
Simon Sauter - avatar
+ 2
Run it and find out.
20th Oct 2021, 7:55 PM
Simon Sauter
Simon Sauter - avatar
+ 2
I thought someone would say otherwise, but apparently everyone on here agrees with me on that (you - directly and others - indirectly). If anyone has the correct answer to that, let me know by answering this question.
20th Oct 2021, 8:07 PM
Mr. E
Mr. E - avatar
+ 2
I don't know C, but I'm pretty sure that this is not a complete program. So I'm not sure if that's relevant. But I notice that you switch back and forth between "what is the value of x" (which seems to be their question) and "what is the output" (which seems to be the question you answered). Maybe that's the entire issue? I don't know.
20th Oct 2021, 8:14 PM
Simon Sauter
Simon Sauter - avatar
+ 2
@Martin Taylor: I aced all the other interview questions which were mainly writing various programs in C, so, obviously I do know quite a lot about the "printf("%d\n", x);" statement, and I also knew the correct value of x = 11, but just like @Simon Sauter pointed out, I must've thought about the output of the program, instead of the value of x (my mistake - at least I had the guts to admit I was wrong, unlike other people), that's why that was the only question that was marked as "Incorrect", all the other ones being "Correct", so there's no need to get all bent out of shape about it, @Martin Taylor. And, mind you & take note, @Martin Taylor: all the compilers in the world will always output 0 on that given code, on any day (you can even verify it, with any compiler you want, if you have any kind of doubts)! And IBM's standards are still the highest of all. P.S. I do have a Software Engineering university degree (B.A.) from CalTech (full-scholarship), by the way. Adios!
21st Oct 2021, 11:41 PM
Mr. E
Mr. E - avatar
+ 2
x=2 y=11 I am not sure what the objective of this code is but, the flow is as follows: Initialization x=1,y=2 Comparison ie if x !=y y = 1 + 10 = 11 There are no curly brackets so x = 0 + 8 = 8 (y is reinitialized) Since the value of x++ is not captured in the output stream, the final values are x=8, y=11
7th Sep 2023, 11:09 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
I already did that, but the point is, it outputs 0 in any compiler. So, what's the answer to my question, then, seeing as how the value of x can't be 0?
20th Oct 2021, 7:56 PM
Mr. E
Mr. E - avatar
+ 1
@Lothar, there are no other lines of code! That's what I was suprised about, myself. It would output 0. It's a programming question that was given by IBM in one of their interviews. The full question is (quote): What is the value of “x” after the execution of the code below? int x = 1, y = 2; if (x == y) y = x +10; x = y + 8; x++; No any other info provided.
20th Oct 2021, 8:01 PM
Mr. E
Mr. E - avatar
+ 1
Notice that they don't use the word "output".
20th Oct 2021, 8:03 PM
Simon Sauter
Simon Sauter - avatar
+ 1
@Lothar, this was the only question that I didn't get right in their interview, due to the lack of any other information: I said it outputs 0 (nothing), they said that's not the correct answer. I got all the other questions right (they were all correct), I got the job, but this question still haunts me today.
20th Oct 2021, 8:04 PM
Mr. E
Mr. E - avatar
+ 1
@Simon Sauter: well then, what's the correct answer?
20th Oct 2021, 8:09 PM
Mr. E
Mr. E - avatar
+ 1
@Simon Sauter: yes, but they still marked the only question I got wrong as "Incorrect". Is there a "correct" answer to their question?!
20th Oct 2021, 8:12 PM
Mr. E
Mr. E - avatar
+ 1
The value of x is 11 but there is no output. So you can add printf("%d\n", x) to have an output.
21st Oct 2021, 4:43 PM
Thierry Andriamihaja
Thierry Andriamihaja - avatar
+ 1
Sugandha Das Thierry Andriamihaja maybe in the future you could read the answers that have already been given before posting. Then instead of repeating information that has already been provided by others you could move on to questions that haven't been resolved yet.
21st Oct 2021, 5:50 PM
Simon Sauter
Simon Sauter - avatar
+ 1
int x = 1, y = 2; //x=1 and y=2 if (x == y) //x==y-->false and 0-->y=2 y = x +10; // not executed x = y + 8; // x is now 10 becuse y=2 and 2+8=10 x++; // x=10+1 the value of x is 11
22nd Oct 2021, 2:58 PM
MJARAB
MJARAB - avatar
+ 1
play some C chalanges in Sololearn to see it's common to ask value of a variable after some code😁
22nd Oct 2021, 6:56 PM
Mehran
Mehran - avatar
+ 1
Mr. E, you should expect all kinds of criticisms when asking a question. I think your question has gotten many useful feedbacks as to how to ask a question and how your program doesn't output the same as you expect, you didn't really have to give them negative feedbacks by verbally attacking and trying to compare your self-worth with others, Martin Taylor just has the guts to say what everyone else is thinking.
23rd Oct 2021, 5:21 AM
Tim
Tim - avatar
0
11
21st Oct 2021, 2:39 PM
Sugandha Das