4
#include<stdio.h> int main() { printf("He %c llo",65); } 

Output: He A llo

#include<stdio.h> int main() { printf("He %c llo",13); } 

Output: llo. It doesnt print He.

I can understand that 65 is ascii value for A and hence A is printed in first case but why llo in second case.

Thanks

3
  • Those two examples appear to be completely identical. Commented Mar 12, 2010 at 4:05
  • edited. the argument in second is 13. Commented Mar 12, 2010 at 4:07
  • 3
    Is the output of the first really just A or is it He A llo ? Commented Mar 12, 2010 at 4:08

3 Answers 3

10

ASCII 13 is carriage return, which on some systems simply moves the cursor to the beginning of the line you were just on.

Further characters then wipe out the earlier text.

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

Comments

5

Man ascii:

Oct Dec Hex Char 015 13 0D CR '\r' 

Character 13 is carriage return so it prints He then returns to the beginning of the line and prints the remining llo.

2 Comments

Note: it returns to the beginning of current line not to the beginning of next line, to return to the beginning of next line you would use \r\n - carriage return, line feed.
stefanB: Or just \n in the case of *NIX and OS X, or plain \r in MacOS before OS X.
3

It's just being rendered strangely due to the nature of a carriage return*. You can see the characters that are output, by piping to another tool such as xxd:

 $ gcc b.c && ./a.out | xxd 0000000: 4865 200d 206c 6c6f He . llo $ gcc c.c && ./a.out | xxd 0000000: 4865 2041 206c 6c6f He A llo 

* See Wikipedia:

On printers, teletypes, and computer terminals that were not capable of displaying graphics, the carriage return was used without moving to the next line to allow characters to be placed on top of existing characters to produce character graphics, underlines, and crossed out text.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.