Why does wprintf transliterate Russian text in Unicode into Latin on Linux?

Because conversion of wide characters is done according to the currently set locale. By default a C program always starts with a “C” locale which only supports ASCII characters. You have to switch to any Russian or UTF-8 locale first: setlocale(LC_ALL, “ru_RU.utf8”); // Russian Unicode setlocale(LC_ALL, “en_US.utf8”); // English US Unicode Or to a current … Read more

Variable sized padding in printf

Yes, if you use * in your format string, it gets a number from the arguments: printf (“%0*d\n”, 3, 5); will print “005”. Keep in mind you can only pad with spaces or zeros. If you want to pad with something else, you can use something like: #include <stdio.h> #include <string.h> int main (void) { … Read more

Does printf(“%x”,1) invoke undefined behavior?

I believe it is technically undefined, because the “correct type” for %x is specified as unsigned int – and as you point out, there is no exception for signed/unsigned mismatch here. The rules for printf are for a more specific case and thus override the rules for the general case (for another example of the … Read more

cross-platform printing of 64-bit integers with printf

There are a couple of approaches. You could write your code in C99-conforming fashion, and then supply system-specific hacks when the compiler-writers let you down. (Sadly, that’s rather common in C99.) #include <stdint.h> #include <inttypes.h> printf(“My value is %10” PRId64 “\n”, some_64_bit_expression); If one of your target systems has neglected to implement <inttypes.h> or has … Read more

Printf example in bash does not create a newline

The backtick operator removes trailing new lines. See 3.4.5. Command substitution at http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html Note on edited question Compare: [alvaro@localhost ~]$ printf “\n” [alvaro@localhost ~]$ echo “\n” \n [alvaro@localhost ~]$ echo -e “\n” [alvaro@localhost ~]$ The echo command doesn’t treat \n as a newline unless you tell him to do so: NAME echo – display a … Read more