{"id":318635,"date":"2021-02-26T03:00:12","date_gmt":"2021-02-26T03:00:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=318635"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=318635","title":{"rendered":"\u0423\u043c\u043d\u044b\u0439 print \u0434\u043b\u044f C"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">#include \"print.h\"  int main() {   print(\"number:\", 25,     \"fractional number:\", 1.2345,     \"expression:\", (2.0 + 5) \/ 3   ); }<\/code><\/pre>\n<pre><code class=\"javascript\">number: 25 fractional number: 1.2345 expression: 2.33333<\/code><\/pre>\n<p>\u0414\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u0432\u044b\u0437\u043e\u0432 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0449\u0435 \u043d\u0430\u0431\u0438\u0440\u0430\u0442\u044c, \u0447\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 <code>printf()<\/code>, \u043d\u043e \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430, \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0441\u0438\u043c\u0432\u043e\u043b \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u043f\u043e\u0441\u043b\u0435 &#171;<code>%\"<\/code> \u043d\u0435\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430. <\/p>\n<p>\u0414\u0436\u0435\u043d\u0435\u0440\u0438\u043a <code>print<\/code>, \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u044f\u0437\u044b\u043a\u0430 \u0421\u0438, \u0446\u0435\u043b\u044b\u0435, \u0441\u043e \u0437\u043d\u0430\u043a\u043e\u043c \u0438 \u0431\u0435\u0437, \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438:<\/p>\n<pre><code class=\"cpp\">char *s = \"abc\"; void *p = main; long l = 1234567890123456789; unsigned char byte = 222; char ch = 'A'; print(\"string:\", s, \"pointer:\", p, \"long:\", l); print(byte, ch)<\/code><\/pre>\n<pre><code class=\"javascript\">string: \"abc\" pointer: 0x402330 long: 1234567890123456789 222&lt;0xDE&gt; 'A'65<\/code><\/pre>\n<p>\u0420\u0430\u0437\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u044e\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c, \u043f\u0430\u043b\u0438\u0442\u0440\u0443 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c, \u043b\u0438\u0431\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0446\u0432\u0435\u0442. <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b:<\/p>\n<pre><code class=\"cpp\">int x[] = { 1, 2, 3 }; char *args[] = { \"gcc\", \"hello.c\", \"-o\", \"hello\" }; print(x, args);<\/code><\/pre>\n<pre><code class=\"javascript\">[1 2 3] [\"gcc\" \"hello.c\" \"-o\" \"hello\"]<\/code><\/pre>\n<p>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442? \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 print \u044d\u0442\u043e \u043c\u0430\u043a\u0440\u043e\u0441, \u0442\u043e\u0447\u043d\u0435\u0435 \u0433\u043e\u0432\u043e\u0440\u044f variadic macro, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0430\u043a\u0440\u043e\u0441 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0432\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u0442\u0440\u044e\u043a:<\/p>\n<pre><code class=\"cpp\">void __print_func(int count, ...); #define count_arg(q,w,e,r,t,y,...) y #define print(a...) __print_func(count_arg(a,5,4,3,2,1,0), a);<\/code><\/pre>\n<p>\u042d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u0431\u043b\u0435\u0449\u0435\u0442, \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c \u043f\u0440\u0435\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430, \u043a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 <code>6<\/code>, (\u0432 \u043c\u043e\u0435\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0441\u0435\u0439\u0447\u0430\u0441 <code>26<\/code> ).<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 spread operator <code>...<\/code> , \u044d\u0442\u043e \u0441\u0430\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 __print_func<code>()<\/code>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0439 <code>stdarg.h<\/code> \u0434\u043b\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u044d\u0442\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430:<\/p>\n<pre><code class=\"cpp\">void prn(int count, ...) {   va_list v;   va_start(v, types);   for (int i = 0; i &lt; count; i++) {     ...     printf(\"%'li\", va_arg(v, unsigned long));     ...   }   va_end(v); }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a \u0443\u0437\u043d\u0430\u0442\u044c \u0442\u0438\u043f\u044b? \u0412\u0435\u0434\u044c <code>va_arg<\/code>\u043d\u0435 \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u0438\u043a, \u043c\u044b \u0435\u043c\u0443 \u0434\u0436\u043e\u043b\u0436\u043d\u044b \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0438\u043f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 &#8212; \u044d\u0442\u043e <code>unsigned long<\/code>, \u043d\u043e, \u0447\u0442\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0441\u0442, \u043c\u044b \u0435\u0449\u0451 \u043d\u0435 \u0437\u043d\u0430\u0435\u043c.<\/p>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u0432 \u0421\u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442 \u0442\u0430\u043a\u0443\u044e \u0432\u0435\u0449\u044c:<\/p>\n<pre><code class=\"cpp\">int x; int y = __builtin_types_compatible_p(typeof(x), int);<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0442\u0438\u043f\u044b, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0431\u0443\u043b\u0435\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 <code>y<\/code> \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d <code>1<\/code> \u0438\u043b\u0438 <code>true<\/code> \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>int == int<\/code>.<\/p>\n<p>\u0415\u0449\u0451, \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0437\u043e\u0432, \u043a\u0430\u043a <code>__builtin_choose_expr(a, b, \u0441).<\/code> \u042d\u0442\u043e \u0430\u043d\u0430\u043b\u043e\u0433 <code>a ? b : c<\/code> \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u0438\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c, \u0447\u0442\u043e-\u0442\u043e \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u0441\u0432\u0438\u0442\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432 \u0432\u0438\u0434\u0435 \u0447\u0438\u0441\u043b\u0430, <code>3<\/code> \u0434\u043b\u044f <code>int<\/code>, <code>2<\/code> \u0434\u043b\u044f <code>double<\/code> \u0438 \u0442.\u0434.:<\/p>\n<pre><code class=\"cpp\">#define __get_type(x) \\   __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), double), 1, \\   __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), char), 2, \\   __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), int), 3, \\   __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), void*), 4, \\   ....... \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0442\u0440\u044e\u043a\u0438 \u0441 variadic macro, \u0442\u043e \u0435\u0441\u0442\u044c, \u043f\u0438\u0448\u0435\u043c <code>__get_type()<\/code>, \u043c\u043d\u043e\u0433\u043e \u0438\u043b\u0438, \u0442\u043e\u0447\u043d\u0435\u0435, <code>count<\/code> \u0440\u0430\u0437 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u044f\u0442\u0443\u044e, \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043c\u0430\u0441\u0441\u0438\u0432 <code>char types[],<\/code> \u0438 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432\u0442\u043e\u0440\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0432 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0435\u0447\u0430\u0442\u0438, \u0435\u0451 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0441\u0442\u0430\u043d\u0435\u0442 \u0442\u0430\u043a\u0438\u043c:<\/p>\n<pre><code class=\"cpp\">void __print_func (int count, char types[], ...) {<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043c\u0435\u043b\u043e \u0431\u0440\u0430\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>va_arg,<\/code> \u043f\u043e\u0434\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u044f \u0438\u0445 \u0442\u0438\u043f\u044b \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430:<\/p>\n<pre><code class=\"cpp\">for (int i = 0; i &lt; count; i++) {   if (types[i] == 'd') {     double d = va_arg(v, double);     printf(\"%'G\", d);   }   else if (types[i] == 'i') {     double d = va_arg(v, int);     printf(\"%'i\", d);   }   ... } <\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b, \u043d\u0430\u0434\u043e \u0435\u0449\u0451 \u043f\u0435\u0440\u0434\u0430\u0432\u0430\u0442\u044c <code>sizeof()<\/code>, \u0447\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442, \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">(short[])(sizeof(a), sizeof(b), sizeof(c),.........)<\/code><\/pre>\n<p>\u0414\u043b\u044f \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0442\u0438\u043f \u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u0443\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 <code>unsigned short<\/code>: <code>__get_type(x) + sizeof(x) &lt;&lt; 5<\/code>.<\/p>\n<p>\u0412\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u0440\u0435\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 \u0438 <em>builtins<\/em> \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e, \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0437\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">print(42, 42);<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f <code>gcc -O1<\/code>\u0432 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">xor     eax, eax mov     ecx, 42 mov     edx, 42 lea     rsi, [rsp+12] mov     edi, 2 mov     DWORD PTR [rsp+12], 0x00840084 call    __print_func<\/code><\/pre>\n<p>\u0412\u044b\u0448\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u044b GCC <code>5.1+<\/code>, Clang<code>3.4.+1<\/code>, Intel C  <code>17.0.0+<\/code>, \u0438 TinyC. \u041d\u0430 MSVC \u0438\u0445 \u043d\u0435\u0442, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0435\u0441\u0442\u044c \u043f\u043e\u0445\u043e\u0436\u0438\u0435, \u043d\u043e \u043c\u043d\u0435 \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043d\u0430\u0439\u0442\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<\/p>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u0440\u0438\u0441\u0443\u0435\u0442\u0441\u044f \u0446\u0432\u0435\u0442:<\/p>\n<pre><code class=\"cpp\">void __print_color(int a) {   if (!__print_enable_color) return;   if (a == -1) printf(\"\\x1b(B\\x1b[m\");   else printf(\"\\x1b[38;5;%im\", a); }<\/code><\/pre>\n<p>\u041f\u043e\u043c\u0435\u043d\u044f\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>__print_enable_color<\/code> \u043d\u0430 <code>0<\/code>\u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0446\u0432\u0435\u0442\u043d\u043e\u0439 \u0432\u044b\u0432\u043e\u0434. \u0410 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>__print_setup_colors()<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u0430\u043b\u0438\u0442\u0440\u0443:<\/p>\n<pre><code class=\"cpp\">void __print_setup_colors(int normal, int number, int string, int hex, int fractional) {<\/code><\/pre>\n<p>\u041d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u0435\u0449\u0451 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0446\u0432\u0435\u0442\u0430 \u0435\u0441\u043b\u0438 <code>stdout<\/code> \u043d\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u044c, \u0430 \u0444\u0430\u0439\u043b \u0438\u043b\u0438 pipe.<\/p>\n<p>\u0415\u0441\u0442\u044c <code>fprint(fd...)<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <code>stderr<\/code> \u0438 \u043b\u044e\u0431\u044b\u043c\u0438 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u0430\u043c\u0438.<\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0443 \u0432\u0430\u0441 \u0432\u043e\u043f\u0440\u043e\u0441, \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 <code>_Generic<\/code>, \u0430 <code>__builtin_types_compatible_p<\/code>? \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>_Generic<\/code> \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043e\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <code>int*<\/code> \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 <code>int[]<\/code>\u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441 <code>_Generic<\/code> \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0431\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c.<\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 <strong>github<\/strong>: <a href=\"https:\/\/github.com\/exebook\/generic-print\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/exebook\/generic-print<\/a><\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/544038\/\"> https:\/\/habr.com\/ru\/post\/544038\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">#include \"print.h\"  int main() {   print(\"number:\", 25,     \"fractional number:\", 1.2345,     \"expression:\", (2.0 + 5) \/ 3   ); }<\/code><\/pre>\n<pre><code class=\"javascript\">number: 25 fractional number: 1.2345 expression: 2.33333<\/code><\/pre>\n<p>\u0414\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u0432\u044b\u0437\u043e\u0432 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0449\u0435 \u043d\u0430\u0431\u0438\u0440\u0430\u0442\u044c, \u0447\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 <code>printf()<\/code>, \u043d\u043e \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430, \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0441\u0438\u043c\u0432\u043e\u043b \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u043f\u043e\u0441\u043b\u0435 &#171;<code>%\"<\/code> \u043d\u0435\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430. <\/p>\n<p>\u0414\u0436\u0435\u043d\u0435\u0440\u0438\u043a <code>print<\/code>, \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u044f\u0437\u044b\u043a\u0430 \u0421\u0438, \u0446\u0435\u043b\u044b\u0435, \u0441\u043e \u0437\u043d\u0430\u043a\u043e\u043c \u0438 \u0431\u0435\u0437, \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438:<\/p>\n<pre><code class=\"cpp\">char *s = \"abc\"; void *p = main; long l = 1234567890123456789; unsigned char byte = 222; char ch = 'A'; print(\"string:\", s, \"pointer:\", p, \"long:\", l); print(byte, ch)<\/code><\/pre>\n<pre><code class=\"javascript\">string: \"abc\" pointer: 0x402330 long: 1234567890123456789 222&lt;0xDE&gt; 'A'65<\/code><\/pre>\n<p>\u0420\u0430\u0437\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u044e\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c, \u043f\u0430\u043b\u0438\u0442\u0440\u0443 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c, \u043b\u0438\u0431\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0446\u0432\u0435\u0442. <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b:<\/p>\n<pre><code class=\"cpp\">int x[] = { 1, 2, 3 }; char *args[] = { \"gcc\", \"hello.c\", \"-o\", \"hello\" }; print(x, args);<\/code><\/pre>\n<pre><code class=\"javascript\">[1 2 3] [\"gcc\" \"hello.c\" \"-o\" \"hello\"]<\/code><\/pre>\n<p>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442? \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 print \u044d\u0442\u043e \u043c\u0430\u043a\u0440\u043e\u0441, \u0442\u043e\u0447\u043d\u0435\u0435 \u0433\u043e\u0432\u043e\u0440\u044f variadic macro, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0430\u043a\u0440\u043e\u0441 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0432\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u0442\u0440\u044e\u043a:<\/p>\n<pre><code class=\"cpp\">void __print_func(int count, ...); #define count_arg(q,w,e,r,t,y,...) y #define print(a...) __print_func(count_arg(a,5,4,3,2,1,0), a);<\/code><\/pre>\n<p>\u042d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u0431\u043b\u0435\u0449\u0435\u0442, \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c \u043f\u0440\u0435\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430, \u043a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 <code>6<\/code>, (\u0432 \u043c\u043e\u0435\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0441\u0435\u0439\u0447\u0430\u0441 <code>26<\/code> ).<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 spread operator <code>...<\/code> , \u044d\u0442\u043e \u0441\u0430\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 __print_func<code>()<\/code>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0439 <code>stdarg.h<\/code> \u0434\u043b\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u044d\u0442\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430:<\/p>\n<pre><code class=\"cpp\">void prn(int count, ...) {   va_list v;   va_start(v, types);   for (int i = 0; i &lt; count; i++) {     ...     printf(\"%'li\", va_arg(v, unsigned long));     ...   }   va_end(v); }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a \u0443\u0437\u043d\u0430\u0442\u044c \u0442\u0438\u043f\u044b? \u0412\u0435\u0434\u044c <code>va_arg<\/code>\u043d\u0435 \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u0438\u043a, \u043c\u044b \u0435\u043c\u0443 \u0434\u0436\u043e\u043b\u0436\u043d\u044b \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0438\u043f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 &#8212; \u044d\u0442\u043e <code>unsigned long<\/code>, \u043d\u043e, \u0447\u0442\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0441\u0442, \u043c\u044b \u0435\u0449\u0451 \u043d\u0435 \u0437\u043d\u0430\u0435\u043c.<\/p>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u0432 \u0421\u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442 \u0442\u0430\u043a\u0443\u044e \u0432\u0435\u0449\u044c:<\/p>\n<pre><code class=\"cpp\">int x; int y = __builtin_types_compatible_p(typeof(x), int);<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0442\u0438\u043f\u044b, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0431\u0443\u043b\u0435\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 <code>y<\/code> \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d <code>1<\/code> \u0438\u043b\u0438 <code>true<\/code> \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>int == int<\/code>.<\/p>\n<p>\u0415\u0449\u0451, \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0437\u043e\u0432, \u043a\u0430\u043a <code>__builtin_choose_expr(a, b, \u0441).<\/code> \u042d\u0442\u043e \u0430\u043d\u0430\u043b\u043e\u0433 <code>a ? b : c<\/code> \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u0438\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c, \u0447\u0442\u043e-\u0442\u043e \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u0441\u0432\u0438\u0442\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432 \u0432\u0438\u0434\u0435 \u0447\u0438\u0441\u043b\u0430, <code>3<\/code> \u0434\u043b\u044f <code>int<\/code>, <code>2<\/code> \u0434\u043b\u044f <code>double<\/code> \u0438 \u0442.\u0434.:<\/p>\n<pre><code class=\"cpp\">#define __get_type(x) \\   __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), double), 1, \\   __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), char), 2, \\   __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), int), 3, \\   __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), void*), 4, \\   ....... \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0442\u0440\u044e\u043a\u0438 \u0441 variadic macro, \u0442\u043e \u0435\u0441\u0442\u044c, \u043f\u0438\u0448\u0435\u043c <code>__get_type()<\/code>, \u043c\u043d\u043e\u0433\u043e \u0438\u043b\u0438, \u0442\u043e\u0447\u043d\u0435\u0435, <code>count<\/code> \u0440\u0430\u0437 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u044f\u0442\u0443\u044e, \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043c\u0430\u0441\u0441\u0438\u0432 <code>char types[],<\/code> \u0438 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432\u0442\u043e\u0440\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0432 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0435\u0447\u0430\u0442\u0438, \u0435\u0451 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0441\u0442\u0430\u043d\u0435\u0442 \u0442\u0430\u043a\u0438\u043c:<\/p>\n<pre><code class=\"cpp\">void __print_func (int count, char types[], ...) {<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043c\u0435\u043b\u043e \u0431\u0440\u0430\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>va_arg,<\/code> \u043f\u043e\u0434\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u044f \u0438\u0445 \u0442\u0438\u043f\u044b \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430:<\/p>\n<pre><code class=\"cpp\">for (int i = 0; i &lt; count; i++) {   if (types[i] == 'd') {     double d = va_arg(v, double);     printf(\"%'G\", d);   }   else if (types[i] == 'i') {     double d = va_arg(v, int);     printf(\"%'i\", d);   }   ... } <\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b, \u043d\u0430\u0434\u043e \u0435\u0449\u0451 \u043f\u0435\u0440\u0434\u0430\u0432\u0430\u0442\u044c <code>sizeof()<\/code>, \u0447\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442, \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">(short[])(sizeof(a), sizeof(b), sizeof(c),.........)<\/code><\/pre>\n<p>\u0414\u043b\u044f \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0442\u0438\u043f \u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u0443\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 <code>unsigned short<\/code>: <code>__get_type(x) + sizeof(x) &lt;&lt; 5<\/code>.<\/p>\n<p>\u0412\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u0440\u0435\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 \u0438 <em>builtins<\/em> \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e, \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0437\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">print(42, 42);<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f <code>gcc -O1<\/code>\u0432 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">xor     eax, eax mov     ecx, 42 mov     edx, 42 lea     rsi, [rsp+12] mov     edi, 2 mov     DWORD PTR [rsp+12], 0x00840084 call    __print_func<\/code><\/pre>\n<p>\u0412\u044b\u0448\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u044b GCC <code>5.1+<\/code>, Clang<code>3.4.+1<\/code>, Intel C  <code>17.0.0+<\/code>, \u0438 TinyC. \u041d\u0430 MSVC \u0438\u0445 \u043d\u0435\u0442, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0435\u0441\u0442\u044c \u043f\u043e\u0445\u043e\u0436\u0438\u0435, \u043d\u043e \u043c\u043d\u0435 \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043d\u0430\u0439\u0442\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<\/p>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u0440\u0438\u0441\u0443\u0435\u0442\u0441\u044f \u0446\u0432\u0435\u0442:<\/p>\n<pre><code class=\"cpp\">void __print_color(int a) {   if (!__print_enable_color) return;   if (a == -1) printf(\"\\x1b(B\\x1b[m\");   else printf(\"\\x1b[38;5;%im\", a); }<\/code><\/pre>\n<p>\u041f\u043e\u043c\u0435\u043d\u044f\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>__print_enable_color<\/code> \u043d\u0430 <code>0<\/code>\u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0446\u0432\u0435\u0442\u043d\u043e\u0439 \u0432\u044b\u0432\u043e\u0434. \u0410 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>__print_setup_colors()<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u0430\u043b\u0438\u0442\u0440\u0443:<\/p>\n<pre><code class=\"cpp\">void __print_setup_colors(int normal, int number, int string, int hex, int fractional) {<\/code><\/pre>\n<p>\u041d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u0435\u0449\u0451 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0446\u0432\u0435\u0442\u0430 \u0435\u0441\u043b\u0438 <code>stdout<\/code> \u043d\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u044c, \u0430 \u0444\u0430\u0439\u043b \u0438\u043b\u0438 pipe.<\/p>\n<p>\u0415\u0441\u0442\u044c <code>fprint(fd...)<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <code>stderr<\/code> \u0438 \u043b\u044e\u0431\u044b\u043c\u0438 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u0430\u043c\u0438.<\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0443 \u0432\u0430\u0441 \u0432\u043e\u043f\u0440\u043e\u0441, \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 <code>_Generic<\/code>, \u0430 <code>__builtin_types_compatible_p<\/code>? \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>_Generic<\/code> \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043e\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <code>int*<\/code> \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 <code>int[]<\/code>\u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441 <code>_Generic<\/code> \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0431\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c.<\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 <strong>github<\/strong>: <a href=\"https:\/\/github.com\/exebook\/generic-print\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/exebook\/generic-print<\/a><\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/544038\/\"> https:\/\/habr.com\/ru\/post\/544038\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-318635","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318635"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318635\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}