What does “%.*s” mean in printf?
You can use an asterisk (*) to pass the width specifier/precision to printf(), rather than hard coding it into the format string, i.e. void f(const char *str, int str_len) { printf(“%.*s\n”, str_len, str); }
You can use an asterisk (*) to pass the width specifier/precision to printf(), rather than hard coding it into the format string, i.e. void f(const char *str, int str_len) { printf(“%.*s\n”, str_len, str); }
The basic way is: printf (“Here are the first 8 chars: %.8s\n”, “A string that is more than 8 chars”); The other, often more useful, way is: printf (“Here are the first %d chars: %.*s\n”, 8, 8, “A string that is more than 8 chars”); Here, you specify the length as an int argument to … Read more
People (and especially beginners) should never use scanf(“%s”) or gets() or any other functions that do not have buffer overflow protection, unless you know for certain that the input will always be of a specific format (and perhaps not even then). Remember than scanf stands for “scan formatted” and there’s precious little less formatted than … Read more
The syntax is almost the same as printf. With printf you give the string format and its contents ie: printf(“my %s has %d chars\n”, “string format”, 30); With fprintf it is the same, except now you are also specifying the place to print to: FILE *myFile; … fprintf( myFile, “my %s has %d chars\n”, “string … Read more
It’s compiling because printf isn’t type safe, since it uses variable arguments in the C sense1. printf has no option for std::string, only a C-style string. Using something else in place of what it expects definitely won’t give you the results you want. It’s actually undefined behaviour, so anything at all could happen. The easiest … Read more
puts is simpler than printf but be aware that the former automatically appends a newline. If that’s not what you want, you can fputs your string to stdout or use printf.
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
They are the same when used for output, e.g. with printf. However, these are different when used as input specifier e.g. with scanf, where %d scans an integer as a signed decimal number, but %i defaults to decimal but also allows hexadecimal (if preceded by 0x) and octal (if preceded by 0). So 033 would … Read more
Escape it with another %: $stringWithVariables=”About to deduct 50%% of %s %s from your Top-Up account.”;
You can use the printf method, like so: System.out.printf(“%.2f”, val); In short, the %.2f syntax tells Java to return your variable (val) with 2 decimal places (.2) in decimal representation of a floating-point number (f) from the start of the format specifier (%). There are other conversion characters you can use besides f: d: decimal … Read more