1

It always displays "hello world". Why?

#include <stdio.h>

int main(void)
{
    printf("..... world\rhello\n");
    return 0;
}
5
  • 5
    You do know what the character '\r' (also known as carriage return) does? Commented Aug 28, 2013 at 9:01
  • en.wikipedia.org/wiki/Carriage_return Commented Aug 28, 2013 at 9:02
  • 1
    @user2693578 Did you not forget to recompile your code, by any chance ? Commented Aug 28, 2013 at 9:05
  • Upvoted for the cuteness of this post :D Commented Aug 28, 2013 at 9:12
  • This isn't so much about C as it is about understanding how a terminal works. Could have asked this question in 1948 on an IBM type writer... Commented Aug 28, 2013 at 9:17

5 Answers 5

10

This is because \r is a carriage return (CR). It returns the caret to the start of the line. Afterwards you write hello there, effectively overwriting the dots.

\n (line feed, LF) on the other hand used to move the caret just one line downwards, which is why teletypewriters had the sequence CR-LF, or carriage return followed by a line feed to position the caret at the start of the next line. Unix did away with this and LF now does this on its own. CR still exists with its old semantics, though.

Sign up to request clarification or add additional context in comments.

2 Comments

Next question is: Is this guaranteed by any standard, or is it an UB?
Neither \r nor \n are standardised to map to a specific character (e.g. U+000A and U+000D are a convention, but not required). \n is transparently converted to the system's line break sequence when writing and the reverse is done when reading – in text mode respectively.
4

Using \r you are returning to the beginning of the current line and're overwriting the dots ".....":

printf("..... world\rhello\n");
        ^^^^^        vvvvv
        hello <----- hello

How it works:

..... world
           ^

then returning to the beginning of the current line:

..... world
^

and then printing a word after \r. The result is:

hello world
           ^

Comments

2

Because the lone \r (carriage return) character is causing your terminal to go back to the beginning of the line, without changing lines. Thus, the characters to the left of the \r are overwritten by "hello".

Comments

0

Check it again, it will give out put like

..... world
hello

and what so over you write inside printf() , it will return that as output

Comments

0
#include<stdio.h>
#include<conio.h>
int main(void) 

{   
    // You will hear Audible tone 3 times.
    printf("The Audible Bell --->           \a\a\a\n");
    // \b (backspace) Moves the active position to the 
    // previous position on the current line. 
    printf("The Backspace --->              ___ \b\b\b\b\b\b\b\b\b\bTesting\n");
    //\n (new line) Moves the active position to the initial 
    // position of the next line.
    printf("The newline ---> \n\n");
    //\r (carriage return) Moves the active position to the 
    // initial position of the current line.
    printf("The carriage return --->        \rTesting\rThis program is for testing\n");
    // Moves the current position to a tab space position
    printf("The horizontal tab --->         \tTesting\t\n");

    getch();
    return 0;
}

/***************************OUTPUT************************
The Audible Bell --->
The Backspace --->                        Testing__
The newline --->

This program is for testing
The horizontal tab --->                         Testing
***************************OUTPUT************************/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.