format-specifiers

What's the difference between %ul and %lu C format specifiers?

泪湿孤枕 提交于 2019-12-04 09:37:11
问题 In an example of "C Primer Plus", the author has used %ul format specifier (in both scanf and printf) for unsigned long . When I try to generalize the problem, it seems that the %ul makes something wrong in my computer. But using %lu solved the issue. Actually, rather than focusing on the problem and the line of codes, I want to know about the difference between %ul and %lu . Maybe I could figure out what's wrong. Searching doesn't give me something useful (except that "they are different").

Compiler gives warning when printing the address of a variable

最后都变了- 提交于 2019-12-04 06:25:26
问题 I made a very simple program to print the address of two variables. #include<stdio.h> int main() { int a,b; printf("%u\n%u",&a,&b); return 0; } But, the Clang-3.7 compiler gives warning as: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]` But, when I compiled with GCC-5.x, it gave no warnings. Which of them is correct? One thing I know is that doing unsigned int num=&a; would be wrong as address can only be stored in a pointer. But, is it correct for

I get previous float value when I am printing new value

本小妞迷上赌 提交于 2019-12-04 05:22:37
问题 I am getting output 0.23 from second printf . But typecasting gives required output. If I am not using type casting previous value is printed. Compiler version is GCC 6.3 #include <stdio.h> int main() { printf("%f ", 0.23); printf("%f", 0); return 0; } LINK FOR IDE 回答1: in > printf("%f",0); You ask to print a double but you give an int , this is contradictory You are not in the case where the generated code makes a double from the int because printf is not int printf(const char *, double);

What is the need of hh and h format specifiers?

若如初见. 提交于 2019-12-04 04:18:39
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 working the same. What's the need of using hhx instead of just x ? &mac[0] is a pointer to an unsigned char .

Why is the format specifier for uint8_t and uint16_t the same (%u)? [duplicate]

烈酒焚心 提交于 2019-12-04 03:23:16
This question already has an answer here : Closed 5 years ago . What default promotions of types are there in the variadic arguments list? (1 answer) I only found pretty unrelated questions due to the tons of results searching for printf() . Why does uint8_t not specify its own format string but any other type does? As far as I understand printf() , it has to know the length of the supplied parameters to be able to parse the variable argument list. Since uint8_t and uint16_t use the same format specifier %u , how does printf() "know" how many bytes to process? Or is there somehow an implicit

Displaying floating point variable as a hex integer screws up neighbouring integer

半腔热情 提交于 2019-12-04 02:27:38
问题 I have this simple program #include <stdio.h> int main(void) { unsigned int a = 0x120; float b = 1.2; printf("%X %X\n", b, a); return 0; } I expected the output to be some-value 120 (some-value will depend on the bit pattern of `float b` ) But I see 40000000 3FF33333 Why is the value of a getting screwed up? %X treats its arguments as signed int and hence it should have retrieved 4 bytes from the stack and printed the calue of b and then fetching the next 4 bytes print the value of a which is

How to display large double numbers without scientific notation in C?

被刻印的时光 ゝ 提交于 2019-12-04 02:09:14
问题 How can I display a double like 5000683 Instead of 5.000683e6 in C? I have tried %d , %g and %f , but to no avail. 回答1: It looks like %f works just fine: #include <stdio.h> int main() { double d = 5000683; printf("%f\n", d); printf("%.0f\n", d); return 0; } The output of this code will be 5000683.000000 5000683 The second printf() statement sets the precision to 0 (by prefixing f with .0 ) to avoid any digits after the decimal point. 来源: https://stackoverflow.com/questions/42321120/how-to

How do I printf() a uint16_t?

吃可爱长大的小学妹 提交于 2019-12-04 00:24:37
I need to use printf() to print a uint16_t. This SO answer ( How to print uint32_t and uint16_t variables value? ) says I need to use inttypes.h. However, I'm working on an embedded system and inttypes.h is not available. How do I print a uint16_t when the format specifier for a uint16_t is not available? An obvious way is: printf("%u\n", (unsigned int)x); The unsigned int is guaranteed to be at least 16 bits, so this is not a lossy conversion. You should use the style of inttypes.h but define the symbols yourself. For example: #define PRIu8 "hu" #define PRId8 "hd" #define PRIx8 "hx" #define

Is there a format specifier that works with Boolean values?

淺唱寂寞╮ 提交于 2019-12-03 10:11:46
I want to do something like this: NSLog(@"You got: %x", booleanValue); where x is the specifier. But I can't find one! I want to avoid: if (booleanValue) { NSLog(@"You got: YES"); } else { NSLog(@"You got: NO"); } Any ideas? The docs didn't have a Boolean specifier. %@ didn't work either. PengOne Here are two things that work: NSLog(@"You got: %@",booleanValue ? @"YES" : @"NO"); or you can cast: NSLog(@"You got: %d", (int)booleanValue); Which will output 0 or 1 You can cast it to an int and use %d : NSLog(@"You got: %d", (int)booleanValue); Or use something like this: NSLog(@"You got: %@",

Format specifier %02x

醉酒当歌 提交于 2019-12-03 08:17:21
问题 I have a simple program : #include <stdio.h> int main() { long i = 16843009; printf ("%02x \n" ,i); } I am using %02x format specifier to get 2 char output, However, the output I am getting is: 1010101 while I am expecting it to be : 01010101 . 回答1: %02x means print at least 2 digits, prepend it with 0 's if there's less. In your case it's 7 digits, so you get no extra 0 in front. Also, %x is for int, but you have a long. Try %08lx instead. 回答2: %x is a format specifier that format and output