How to use printf with std::string

C++23 Update We now finally have std::print as a way to use std::format for output directly: #include <print> #include <string> int main() { // … std::print(“Follow this command: {}”, myString); // … } This combines the best of both approaches. Original Answer It’s compiling because printf isn’t type safe, since it uses variable arguments in … Read more

bash printf with new line

printf‘s %b format specifier was meant specifically to replace echo -e (actually, the XSI extension to echo which calls for special interpretation of the arguments by default. -e was never specified and is disallowed by POSIX.), and is identical in virtually every way including a few differences from $’…’ and the format string argument to … Read more

Unsigned values in C

Assign a int -1 to an unsigned: As -1 does not fit in the range [0…UINT_MAX], multiples of UINT_MAX+1 are added until the answer is in range. Evidently UINT_MAX is pow(2,32)-1 or 429496725 on OP’s machine so a has the value of 4294967295. unsigned int a = -1; The “%x”, “%u” specifier expects a matching … Read more

How to hide leading zero in printf

The C standard says that for the f and F floating point format specifiers: If a decimal-point character appears, at least one digit appears before it. I think that if you don’t want a zero to appear before the decimal point, you’ll probably have to do something like use snprintf() to format the number into … Read more

Printing a char with printf

%d prints an integer: it will print the ascii representation of your character. What you need is %c: printf(“%c”, ch); printf(“%d”, ‘\0’); prints the ascii representation of ‘\0’, which is 0 (by escaping 0 you tell the compiler to use the ascii value 0. printf(“%d”, sizeof(‘\n’)); prints 4 because a character literal is an int, … Read more

tech