format-specifiers

A format specifier such as `%15@` works in NSLog but not with NSString stringWithFormat

♀尐吖头ヾ 提交于 2019-12-06 05:24:57
问题 I found something strange when trying to use width specifiers with %@ . They work fine in NSLog but not with NSString stringWithFormat: . Example: NSString *rightAligned = @"foo"; NSString *leftAligned = @"1"; NSLog(@"| %15@ | %-15@ |", rightAligned, leftAligned); And you get the expected output of: | foo | 1 | But replace the NSLog with stringWithFormat: : NSString *test = [NSString stringWithFormat:@"| %15@ | %-15@ |", rightAligned, leftAligned]; And the value of test is incorrectly: | foo

what does the % % mean in java?

我只是一个虾纸丫 提交于 2019-12-05 23:32:19
Im a PHP-programmer and wonder what this line means. System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x)) what does %.3f, %.3f%n and the comma x means? It is similar to C's printf : http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax PHP has a similar function: http://php.net/printf The Documentation of the Java version can be found here: http://java.sun.com/javase/6/docs/api/java/util/Formatter.html The % character is a format specifier which controls how the corresponding variables are formatted. In this particular case, the two argumnents x and Math.exp(x) are

confusion about short data type format specifier in C

给你一囗甜甜゛ 提交于 2019-12-05 22:54:30
Consider following program: #include <stdio.h> int main() { short a=9; //printf("%hi\n",a); printf("%d",a); // LINE 6 } According to this the format specifier for short type (signed) is %hi Is the short type variable always gets promoted automatically to int before performing any operation on it? Is it undefined behavior , If I use %d format specifier to print the value of variable in this program? I compiled it using gcc -Wall -Wextra -WFormat options but still compiler isn't showing any single warning. Why? printf("%hi\n", a); a is promoted to int as per the rules of default argument

What is the need of hh and h format specifiers?

。_饼干妹妹 提交于 2019-12-05 22:11:32
问题 In the code below mac_str is char pointer and mac is a uint8_t array : sscanf(mac_str,"%x:%x:%x:%x:%x:%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]); When I try the above code it gives me a warning: warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 8 has type ‘uint8_t *’ [-Wformat] but I saw in some code they specified sscanf(str,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]); which doesn't give any warning but both are

What is the purpose of format specifier “%qd” in `printf()`?

青春壹個敷衍的年華 提交于 2019-12-05 09:58:56
问题 I saw format specifier %qd when browsing github code. Then I checked in GCC compiler, it's working fine. #include <stdio.h> int main() { long long num = 1; printf("%qd\n", num); return 0; } What is the purpose of format specifier %qd in printf() ? 回答1: Though only a few articles come about %qd in a normal google search, for future reference, this answer is the compilation of my own research, rsp's answer and little discussions here in the comments section by Jonathan Leffler and StoryTeller.

Why doesn't gcc -Wformat warn about printf %d on an unsigned int?

孤者浪人 提交于 2019-12-05 02:27:31
The following program has undefined behavior: #include <stdio.h> int main(void) { unsigned int x = -100; // This is fine, becomes UINT_MAX - 100 printf("%d\n", x); // This is undefined behavior. return 0; } C99 7.19.6.1p8 states %d expects an int argument. C99 7.19.6.1p9 states "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined ." However, gcc -Wformat (which is included with -Wall ) will not complain about the above program, why? Is this a bug, or a deliberate omission? From the gcc manpage: -Wformat Check calls to "printf" and

how can I remove zeros from exponent notation

喜欢而已 提交于 2019-12-05 00:57:40
I'm using exponential formatting to format a decimal number in C#. For example if the number is 0.0001234567 Formatting with (0.0000123456).ToString("E4"); Shows 1.2345E-004 How can I remove leading zero from exponent so it read as below? 1.2345E-4 Assuming you need to always show 4 digits after decimal point, try "0.0000E+0" so it will show (0.0000123456).ToString("0.0000E+0"); //1.2345E-5 (0.0000120000).ToString("0.#E+0"); //1.2000E-5 if you don't need to show 4 digits after decimal points use "0.#E+0" so it will show (0.0000123456).ToString("0.#E+0"); //1.2E-5 (0.0000120000).ToString("0.#E

Typedefs and printf format specifiers

混江龙づ霸主 提交于 2019-12-05 00:29:35
A common use of typedefs is to enable the 'type' of a variable to convey a better idea of a variable's purpose without redefining the storage structure behind it. However, I'm also seeing typedefs as a way to change the storage structure for a class of variables in one go. For example, if I define typedef uint32_t my_offset_t and have variables of the type my_offset_t , switching the code-base from uint32_t to char or uint64_t is as simple as changing one line and recompiling (assuming I've used sizeof rather than hard-coded sizes), except in the case of printf / scanf . Is there a way to swap

Format specifiers for data type BYTE, WORD and DWORD in c-language?

心不动则不痛 提交于 2019-12-04 19:08:59
In C-language, what are the most appropriate format specifiers for data type BYTE, WORD and DWORD to be used with printf and scanf functions? I am having a hard time displaying BPB field's values over console. For example, if I am trying to display BPB_BytsPerSec using "%lu", I am getting unusual figures.. printf("Bytes per Sector: %lu", b->BPB_BytsPerSec); I am getting a value of "514", which I believe, is wrong interpretation.. Please suggest the way out. Thanks. (I am using gcc 5.1 via MinGW, over a 64bit Windows) Here is the Structure We are talking about: /* BPB Structure Collected from

A format specifier such as `%15@` works in NSLog but not with NSString stringWithFormat

别等时光非礼了梦想. 提交于 2019-12-04 10:13:34
I found something strange when trying to use width specifiers with %@ . They work fine in NSLog but not with NSString stringWithFormat: . Example: NSString *rightAligned = @"foo"; NSString *leftAligned = @"1"; NSLog(@"| %15@ | %-15@ |", rightAligned, leftAligned); And you get the expected output of: | foo | 1 | But replace the NSLog with stringWithFormat: : NSString *test = [NSString stringWithFormat:@"| %15@ | %-15@ |", rightAligned, leftAligned]; And the value of test is incorrectly: | foo | 1 | If I change this to use %s and cStringUsingEncoding: then it works: NSString *test2 = [NSString