What will be the output of the following C code?
#include <stdio.h>
int main()
{
printf("Ram\rShyam\n");
return 0;
}
#include <stdio.h>
int main()
{
printf("Ram\rShyam\n");
return 0;
}A. RamShyam
B. Ram
Shyam
C. ShyamRam
D. Ram
Answer: Option B
Solution (By Examveda Team)
The correct answer is Option B: Ram Shyam.Here's why:
* `printf("Ram\rShyam\n");` is the core of the problem. Let's break it down:
* `Ram`: This part is initially printed to the console buffer.
* `\r`: This is the carriage return character. It moves the cursor to the beginning of the current line.
* `Shyam`: Because of `\r`, this overwrites the beginning of the existing line. So "Ram" is replaced by "Shyam". If "Shyam" has fewer characters than "Ram", some parts of "Ram" may remain.
* `\n`: This is the newline character. It moves the cursor to the beginning of the next line.
Because `\r` brings the cursor to the start of the line *before* "Shyam" is printed, "Shyam" will overwrite "Ram", and then `\n` will put the cursor on a newline for subsequent outputs (if any).
Join The Discussion
Comments (1)
Related Questions on C Fundamentals
What is the primary purpose of a function prototype in C?
A. Declare a variable
B. Declare a function
C. Define a function
D. Assign a value
What is the correct syntax for declaring a variable in C?
A. int variable_name;
B. variable_name = 5;
C. variable_name int;
D. int = variable_name;

wrong answer .How it works:
"Ram" is printed first.
brings the cursor to the beginning of the line.
"Shyam" then overwrites "Ram":
'S' overwrites 'R'
'h' overwrites 'a'
'y' overwrites 'm'
'a' and 'm' are printed afterward
So "Ram" is erased. Only "Shyam" is shown.