conversion-specifier

Why printf is not able to handle flags, field width and precisions properly?

老子叫甜甜 提交于 2019-12-13 10:52:23
问题 I'm trying to discover all capabilities of printf and I have tried this : printf("Test:%+*0d", 10, 20); that prints Test:%+100d I have use first the flag + , then the width * and the re-use the flag 0 . Why it's make this output ? I purposely used printf() in a bad way but I wonder why it shows me the number 100? 回答1: This is because, you're supplying syntactical nonsense to the compiler, so it is free to do whatever it wants. Related reading, undefined behavior. Compile your code with

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

What is exactly an “invalid conversion specification”?

烈酒焚心 提交于 2019-12-10 16:35:24
问题 As per C11 , chapter §7.21.6.1, P9 If a conversion specification is invalid, the behavior is undefined. 282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined. Till time, my understanding was, for char str [] = "Sourav"; A statement like printf("%S", str); belong to the first sentence, there exist no CS as %S (UPPERCASE) A statement like printf("%d", str); belongs to the second sentence (mismatch between CS and argument type, but

Reading in double values with scanf in c

主宰稳场 提交于 2019-11-26 17:49:20
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code: double a,b; printf("--------\n"); //seperate lines scanf("%ld",&a); printf("--------\n"); scanf("%ld",&b); printf("%d %d",a,b); If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried: using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that

Reading in double values with scanf in c

 ̄綄美尐妖づ 提交于 2019-11-26 04:51:39
问题 I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code: double a,b; printf(\"--------\\n\"); //seperate lines scanf(\"%ld\",&a); printf(\"--------\\n\"); scanf(\"%ld\",&b); printf(\"%d %d\",a,b); If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried: using float or int instead of double, using scanf_s, using scanf(\"%d or %f for %i or %li or %lf or %e or %g ), using