Predict the output of the given below code int main() {…
2026
Predict the output of the given below code
int main()
{
printf("\new_c_question\by");
printf("\rTCS");
getchar();
return 0;
}
- A.
new_c_questioy
- B.
ew_c_questioy
- C.
TCSc_questioy
- D.
ew_c_questioy TCS
Attempted by 114 students.
Show answer & explanation
Correct answer: C
Concept
In C, several escape sequences are cursor-control codes rather than printable characters. \n starts a new line; \b (backspace) moves the cursor one column to the left without deleting, so the next character overwrites whatever was there; and \r (carriage return) moves the cursor to column 0 of the current line, again without erasing. The visible text on a line is whatever occupies each column after all overwrites have happened.
Applying it to this code
printf("\new_c_question\by");The leading\nopens a new line, then the remaining characters are written, so the line readsew_c_question(the backslash-n consumes the letter n).\bsteps the cursor back onto the final letter, and theythat follows overwrites it, so the line now readsew_c_questioy.printf("\rTCS");The\rreturns the cursor to column 0 andTCSoverwrites the first three columns, so the line becomesTCSc_questioy.getchar();waits for a keypress and prints nothing.
Cross-check
Column by column: columns 0-2 now hold T, C, S from the second printf; columns 3-11 keep c_questio unchanged; column 12 holds y from the backspace overwrite. Reading straight across gives TCSc_questioy.