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 long long int in C with GCC?

If you are on windows and using mingw, gcc uses the win32 runtime, where printf needs %I64d for a 64 bit integer. (and %I64u for an unsinged 64 bit integer) For most other platforms you’d use %lld for printing a long long. (and %llu if it’s unsigned). This is standarized in C99. gcc doesn’t come … Read more

Is it possible to print out only a certain section of a C-string, without making a separate substring?

Is it possible to print out only the last 5 bytes of this string? Yes, just pass a pointer to the fifth-to-the-last character. You can determine this by string + strlen(string) – 5. What about the first 5 bytes only? Use a precision specifier: %.5s #include <stdio.h> #include <string.h> char* string = “Hello, how are … Read more

How does this program work?

That’s because %d expects an int but you’ve provided a float. Use %e/%f/%g to print the float. On why 0 is printed: The floating point number is converted to double before sending to printf. The number 1234.5 in double representation in little endian is 00 00 00 00 00 4A 93 40 A %d consumes … Read more

Padding characters in printf

Pure Bash, no external utilities This demonstration does full justification, but you can just omit subtracting the length of the second string if you want ragged-right lines. pad=$(printf ‘%0.1s’ “-“{1..60}) padlength=40 string2=’bbbbbbb’ for string1 in a aa aaaa aaaaaaaa do printf ‘%s’ “$string1” printf ‘%*.*s’ 0 $((padlength – ${#string1} – ${#string2} )) “$pad” printf ‘%s\n’ … Read more

Using colors with printf

Rather than using archaic terminal codes, may I suggest the following alternative. Not only does it provide more readable code, but it also allows you to keep the color information separate from the format specifiers just as you originally intended. blue=$(tput setaf 4) normal=$(tput sgr0) printf “%40s\n” “${blue}This text is blue${normal}” See my answer HERE … Read more

Why use sprintf function in PHP?

sprintf has all the formatting capabilities of the original printf which means you can do much more than just inserting variable values in strings. For instance, specify number format (hex, decimal, octal), number of decimals, padding and more. Google for printf and you’ll find plenty of examples. The wikipedia article on printf should get you … Read more

printf() formatting for hexadecimal

The # part gives you a 0x in the output string. The 0 and the x count against your “8” characters listed in the 08 part. You need to ask for 10 characters if you want it to be the same. int i = 7; printf(“%#010x\n”, i); // gives 0x00000007 printf(“0x%08x\n”, i); // gives 0x00000007 … Read more

printf with “%d” of numbers starting with 0 (ex “0102”) giving unexpected answer (ex ‘”66″)

This is because when the first digit of a number (integer constant) is 0 (and second must not be x or X), the compiler interprets it as an octal number. Printing it with %d will give you a decimal value. To print octal value you should use %o specifier printf(“%o”, n); 6.4.4.1 Integer constants: An … Read more