Novell Interview Question
Software Engineer / DevelopersCountry: India
i think this will print 3 'characters that correspond to 0' and then some garbage or not
Should it instead print garbage value? Because the null is assigned to b, b gets incremented and then the while loop fails. So it now points to memory location "after" the null.
It is C, you can assign, the compiler just shows a warning... Don't compile C code with C++ compiler.
axecapone is right on it, b is advanced after the terminating 0-character! So the output is garbage or even a segmentation fault (reading memory that does not belong to the program)
An assignment expression has the value of the left operand after the assignment therefore
*b++=*a++
returns the value of *b++ which takes the value of *a++.
Thus the loop breaks when 'a' reach the null value.
however the code will print nothing since 'b' is advanced to reach the null value.
there will be compilation error in it.
while loop doesn't have a condition but an assignment in it..
also the loop is terminated there onli so if it compiles..acc. to me the printf will execute just once printing a garbage value
empty string i.e nothing will be printed as pointer b is also advanced to point to end of string i.e null
- Anonymous August 25, 2012