Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does printf display only the first letter?

Tags:

c

printf

unicode

First of all, I have a function where I have signs stored in unsigned char* type. For example for Abcdef!? it is [65 0] [98 0] [99 0] [100 0] [101 0] [102 0] [33 0] [63 0] 2 bytes per sign in Unicode.

When I use for(unsigned char i=0; i<17; i++) printf("%c", pointer[i]); everything is ok, it shows Abcdef!?. But when I use printf("%s" pointer); it gives me only A and nothing else. Could you tell me why?

like image 207
pizzerman Avatar asked Sep 01 '25 10:09

pizzerman


1 Answers

Because printf("%s", pointer) literally means: Print every char starting at the one stored at pointer until '\0' is encountered.

There's a '\0' immediately after 'A', so only the first character is printed.

like image 52
Christian Hackl Avatar answered Sep 03 '25 05:09

Christian Hackl