MariaDB C library thinks that connection character was set to utf8mb4, but in reality not. As connection charset affects functions like mysql_real_escape_string() it is a problem.
It happen only when connection charset is configured via mysql_options() call with MYSQL_SET_CHARSET_NAME prior to calling mysql_real_connect().
When charset is configured via function mysql_set_character_set() after mysql_real_connect() everything is OK.
In attachment is example which demonstrate this problem. MariaDB C library thinks that charset is utf8mb4 (and based on this fact is doing escaping and other operations). But in reality only 3byte utf8 charset is used (retrieved via SELECT query), which e.g. cannot transfer new 8byte emoji.
If you add mysql_set_character_set(mysql, "utf8mb4"); after mysql_real_connect() then output is correct:
Just use last MariaDB Connnector/C 3.0.5 and try to connect to MariaDB server 5.5.52 (version which is available on the CentOS 7.3).
Important part is to configure server with:
(Or latin1 or whatever different from utf8mb4)
It looks like that server option skip-character-set-client-handshake cause this problem and makes client defective. But client library should not be affected by any such server configuration.
This problem was discovered while developing Perl DBI driver DBD::MariaDB: https://github.com/gooddata/DBD-MariaDB. As a workaround DBD::MariaDB always change charset after mysql_real_connect() was called.