format-specifiers

How do I work around the “unknown conversion type character `z' in format” compiler-specific warning?

情到浓时终转凉″ 提交于 2019-12-12 12:51:04
问题 I'm working on code that is cross-compiled to several target architectures. I looked at the handful of hits from searching Stack Overflow for "printf size_t unknown conversion type character" warning, however those posts all seem to be related to minGW , so those answers, essentially ifdef ing against _WIN32 , do not apply to my instance of essentially the same problem, i.e. printf not recognizing " %zu " as the format-specifier for size_t , but with a mips cross compiler. Is there an

Typedefs and printf format specifiers

ぐ巨炮叔叔 提交于 2019-12-12 09:35:28
问题 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

Name PHP specifiers in printf() strings

别来无恙 提交于 2019-12-12 08:25:09
问题 Is there a way in PHP to name my specifiers like in Python? I want this in PHP: $foo = array('name' => 24); printf("%(name)d", $foo); I couldn't find nothing related on google or in the php manual. 回答1: Nice question! You can roll your own without too much trouble by working with regexes. I based the implementation on the idea of calling vsprintf, which is closest to the stated goal among the built-in printf family of functions: function vsprintf_named($format, $args) { $names = preg_match

How to print 1 byte with printf?

纵饮孤独 提交于 2019-12-12 07:18:24
问题 I know that when using %x with printf() we are printing 4 bytes (an int in hexadecimal) from the stack. But I would like to print only 1 byte. Is there a way to do this ? 回答1: Assumption:You want to print the value of a variable of 1 byte width, i.e., char . In case you have a char variable say, char x = 0; and want to print the value, use %hhx format specifier with printf() . Something like printf("%hhx", x); Otherwise, due to default argument promotion, a statement like printf("%x", x);

If my computer is a 32 bit system, it has a 32 bit address right? But when I print any memory address in C why do I get an address <32bit?

↘锁芯ラ 提交于 2019-12-12 04:59:30
问题 For example printf("%u",&a); gives me output 65524 which is a 16 bit address. 回答1: Because you used wrong format specifier which invokes undefined behavior. To print a pointer, you should use %p format specifier and cast the argument to void* . Something like printf("%p",(void *)&a); will do the job. That said, you should look into the concepts of virtual memory the first thing. 回答2: You can also simply answer your assumption about address size by checking the size of any pointer instead of a

int x; scanf() with %d and printf() with %c

a 夏天 提交于 2019-12-12 04:58:29
问题 int x; So there will be 2 bytes memory for the variable. Now, if I entered 66 and because scanf() with %d, 66 will be stored in 2 bytes memory because the variable is declared int. Now in printf() with %c, should collect data from only one byte memory to display. But %c displayed correctly B by getting correct data 66 from memory to display. Why it %c has not just get data from one byte? 回答1: %c expects an int argument, due to the default argument promotions for vararg functions. In other

sscanf(“0X80.9”, “%f”, &value) sets value to a hex float not a float

穿精又带淫゛_ 提交于 2019-12-11 06:43:28
问题 I have some code that reads letter value pairs. It can be simplified as: float value = 0.0; sscanf("0X80.9", "%f", &value); In Visual Studio 2013, value = 0 In Visual Studio 2015 and above, value = 128.5625 (i.e. 0x80 in hex = 128 in decimal) Anyone know why this has changed? More importantly, anyone know how to make it work like it used to? 回答1: C99 added a new feature, hexadecimal floating-point constants. It also added new formats to the *scanf() functions. (A hexadecimal floating-point

output of negative integer to %u format specifier

独自空忆成欢 提交于 2019-12-11 05:17:55
问题 Consider the following code char c=125; c+=10; printf("%d",c); //outputs -121 which is understood. printf("%u",c); // outputs 4294967175. printf("%u",-121); // outputs 4294967175 %d accepts negative numbers therefore output is -121 in first case. output in case 2 and case 3 is 4294967175. I don't understand why? 回答1: Do 2 32 - 121 = 4294967175 printf interprets data you provide thanks to the % values %d signed integer, value from -2 31 to 2 31 -1 %u unsigned integer, value from 0 to 2 32 -1

Printing int type with %lu - C+XINU

不想你离开。 提交于 2019-12-11 04:16:23
问题 I have a given code, in my opinion there is something wrong with that code: I compile under XINU. The next variables are relevant : unsigned long ularray[]; int num; char str[100]; There is a function returns int: int func(int i) { return ularray[i]; } now the code is like this: num = func(i); sprintf(str, "number = %lu\n", num); printf(str); The problem is I get big numbers while printing with %lu, which is not correct. If i change the %lu to %d , i get the correct number. For example: with

Width Specifier for scanf() - Length of characters to consume is not fixed at compilation and only determined at run-time. How to make it variable?

会有一股神秘感。 提交于 2019-12-11 03:55:56
问题 I want to apply the field-width specifier to the scanf()-operation for reading a string due to clearly specify the amount of characters to read/consume and not make the scanf() -operation susceptible for causing buffer overflow. As well as the destination argument points to an already matched char array, which has exactly the size of elements, the desired value of the field width has to be, + 1 for the \0 . The size of this char array is also determined at run-time before. The problem now is