int main() { printf("He %c...">

Unexpected printf result

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

Exit: He A llo

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

Exit: llo . He does not print He.

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

thanks

+4
source share
3 answers

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

Other characters then destroy the previous text.

+10
source

Man ascii:

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

Character 13 is a carriage return so that it prints He , then returns to the beginning of the line and prints the entry llo .

+6
source

It just turns out to be strange due to the nature of the carriage return *. You can see the characters that are displayed by connecting to another tool, for example xxd :

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

* See Wikipedia :

For printers, teletypers, and computer terminals that were not capable of displaying graphics, carriage returns were used without going to the next line to allow characters to be placed on top of existing characters to create graphic characters, underscores, and crossed text.

+3
source

Source: https://habr.com/ru/post/1303884/


All Articles