format-specifiers

What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)

不打扰是莪最后的温柔 提交于 2019-11-26 06:12:56
What is the difference between %d and %i when used as format specifiers in printf ? Dipstick 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 be 27 with %i but 33 with %d . These are identical for printf but different for scanf . For printf , both %d and %i designate a signed decimal integer. For scanf , %d and %i also means a signed

What is the purpose of the h and hh modifiers for printf?

本秂侑毒 提交于 2019-11-26 06:06:15
问题 Aside from %hn and %hhn (where the h or hh specifies the size of the pointed-to object), what is the point of the h and hh modifiers for printf format specifiers? Due to default promotions which are required by the standard to be applied for variadic functions, it is impossible to pass arguments of type char or short (or any signed/unsigned variants thereof) to printf . According to 7.19.6.1(7), the h modifier: Specifies that a following d, i, o, u, x, or X conversion specifier applies to a

How should I print types like off_t and size_t?

 ̄綄美尐妖づ 提交于 2019-11-26 04:06:22
问题 I\'m trying to print types like off_t and size_t . What is the correct placeholder for printf() that is portable ? Or is there a completely different way to print those variables? 回答1: You can use z for size_t and t for ptrdiff_t like in printf("%zu %td", size, ptrdiff); But my manpage says some older library used a different character than z and discourages use of it. Nevertheless, it's standardized (by the C99 standard). For those intmax_t and int8_t of stdint.h and so on, there are macros

How do you format an unsigned long long int using printf?

久未见 提交于 2019-11-26 03:36:40
问题 #include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits in 29 bits int normalInt = 5; printf(\"My number is %d bytes wide and its value is %ul. A normal number is %d.\\n\", sizeof(num), num, normalInt); return 0; } Output: My number is 8 bytes wide and its value is 285212672l. A normal number is 0. I assume this unexpected result is from printing the unsigned long long int . How do you printf() an unsigned long long int ? 回答1: Use the ll (el-el) long-long modifier

What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)

ⅰ亾dé卋堺 提交于 2019-11-26 01:12:57
问题 What is the difference between %d and %i when used as format specifiers in printf ? 回答1: 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 be 27 with %i but 33 with %d . 回答2: These are identical for printf but different for scanf . For

Correct format specifier for double in printf

a 夏天 提交于 2019-11-25 22:43:03
问题 What is the correct format specifier for double in printf? Is it %f or is it %lf ? I believe it\'s %f , but I am not sure. Code sample #include <stdio.h> int main() { double d = 1.4; printf(\"%lf\", d); // Is this wrong? } 回答1: "%f" is the (or at least one) correct format for a double. There is no format for a float , because if you attempt to pass a float to printf , it'll be promoted to double before printf receives it 1 . "%lf" is also acceptable under the current standard -- the l is

What happens when I use the wrong format specifier?

别说谁变了你拦得住时间么 提交于 2019-11-25 22:29:41
问题 Just wondering what happens when I use the wrong format specifier in C? For example: x = \'A\'; printf(\"%c\\n\", x); printf(\"%d\\n\", x); x = 65; printf(\"%c\\n\", x); printf(\"%d\\n\", x); x = 128; printf(\"%d\\n\", x); 回答1: what happens when I use the wrong format specifier in C? Generally speaking, undefined behaviour. * However, recall that printf is a variadic function, and that the arguments to variadic functions undergo the default argument promotions . So for instance, a char is