Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why printf works on non-terminated string

Tags:

c

I am wondering how does printf() figure out when to stop printing a string, even I haven't put a termination character at the end of the string? I did an experiment by malloc a 10 bytes memory and put exactly 10 characters in it, somehow, the printf could still print out these characters without running out of bound, why?

like image 370
Rn2dy Avatar asked Feb 15 '11 04:02

Rn2dy


1 Answers

There's a good chance that one of the characters after the string is NULL, so printf stops there, furthermore, characters that are not NULL after the memory that you malloced might not be printable characters so you won't notice them in the terminal.

like image 177
Charles Ma Avatar answered Sep 21 '22 03:09

Charles Ma