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

Avoid trailing zeroes in printf()

This can’t be done with the normal printf format specifiers. The closest you could get would be: printf(“%.6g”, 359.013); // 359.013 printf(“%.6g”, 359.01); // 359.01 but the “.6” is the total numeric width so printf(“%.6g”, 3.01357); // 3.01357 breaks it. What you can do is to sprintf(“%.20g”) the number to a string buffer then manipulate … Read more

What’s up with Java’s “%n” in printf?

From a quick google: There is also one specifier that doesn’t correspond to an argument. It is “%n” which outputs a line break. A “\n” can also be used in some cases, but since “%n” always outputs the correct platform-specific line separator, it is portable across platforms whereas”\n” is not. Please refer https://docs.oracle.com/javase/tutorial/java/data/numberformat.html Original source

Left-pad printf with spaces

If you want the word “Hello” to print in a column that’s 40 characters wide, with spaces padding the left, use the following. char *ptr = “Hello”; printf(“%40s\n”, ptr); That will give you 35 spaces, then the word “Hello”. This is how you format stuff when you know how wide you want the column, but … Read more

Printf width specifier to maintain precision of floating-point value

I recommend @Jens Gustedt hexadecimal solution: use %a. OP wants “print with maximum precision (or at least to the most significant decimal)”. A simple example would be to print one seventh as in: #include <float.h> int Digs = DECIMAL_DIG; double OneSeventh = 1.0/7.0; printf(“%.*e\n”, Digs, OneSeventh); // 1.428571428571428492127e-01 But let’s dig deeper … Mathematically, the … Read more

Java: Literal percent sign in printf statement

The percent sign is escaped using a percent sign: System.out.printf(“%s\t%s\t%1.2f%%\t%1.2f%%\n”,ID,pattern,support,confidence); The complete syntax can be accessed in java docs. This particular information is in the section Conversions of the first link. The reason the compiler is generating an error is that only a limited amount of characters may follow a backslash. % is not a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)