format-specifiers

why does std::numeric_limits<float>::min() differ in behavior when streamed to output with different functions?

时光怂恿深爱的人放手 提交于 2019-12-11 02:32:15
问题 I got a weird behaviour with std::numeric_limits<float>::min() when I call std::cout I get an output value of 1.17549e-38 in contrast when I use printf("%f", std::numeric_limits<float>::min()); I get a value of 0.000000. note that when I evaluate ( std::numeric_limits<float>::min() == std::numeric_limits<float>::min() ) I get true (which is intuitive and logical) so, can any one explain to me this difference in output? 回答1: cppreference for numeric_limits: std::numeric_limits<float>::min()

Is there a printf specifier that requires float not double?

与世无争的帅哥 提交于 2019-12-10 13:18:47
问题 I'm getting MISRA type errors when I use "%f" specifier for snprintf with a parameter of type float . According to my research, MISRA is correct because "%f" expectes a type of double . Is there a floating point specifier or modifier that will use a float type parameter and not a double ? I'm working on an embedded system and don't want to convert from 32-bit float to 64-bit double just to please the snprintf function. The code prints to the debug / console port and this is the only place

scanf - limit decimal places while reading

戏子无情 提交于 2019-12-10 12:12:01
问题 I know how to limit the decimal and integer while printing values in printf (%x.x ), In my scenario I want to implement that while reading from the console itslef. So is there any way to llimit the number of decimals in a floating point value while reading using scanf function? I tried below , doesn't show any errors while compiling but get's the value 0 always float f1; . . scanf("%.3f",&f1); printf("%3.3f\n",f1); 回答1: If you check the documentation, it says scanf: A format specifier for

confusion about short data type format specifier in C

一个人想着一个人 提交于 2019-12-10 10:06:52
问题 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

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

前提是你 提交于 2019-12-10 02:51:58
问题 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

How do I printf() a uint16_t?

为君一笑 提交于 2019-12-09 15:08:30
问题 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? 回答1: 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. 回答2: You should use the style of inttypes.h

console.readLine() and console.format(): What is meant by arguments referenced by format specifiers?

不羁的心 提交于 2019-12-08 12:20:56
问题 This question has a follow-up question here. Following this tutorial and compiling the given RegexTestHarness is giving the following errors on console.readLine(String) and console.Format(String), respectively: The method readLine() in the type Console is not applicable for the arguments (String) The method format(String, Object[]) in the type Console is not applicable for the arguments (String, String, int, int) According to the documentation, there are two arguments required there: public

what does the % % mean in java?

为君一笑 提交于 2019-12-07 14:15:41
问题 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? 回答1: It is similar to C's printf : http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax 回答2: 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 回答3: The % character is a format specifier which controls

Correct use of format specifier to show up to three decimals if needed, otherwise zero decimals?

此生再无相见时 提交于 2019-12-07 04:18:40
问题 I've found %g to show only decimals if needed. If the number is whole, no trailing .000 is added, so thats good. But in the case of for example 1.12345 I want it to short the answer to 1.123. And in the case of 1.000 I want to only show 1, as %g already does. I've tried to specify %.3g in the string, but that doesn't work. If anyone has the answer, I'd be grateful! 回答1: I reviewed the abilities of a "format string" via the IEEE Specification and as I understand it your wished behavior is not

how can I remove zeros from exponent notation

為{幸葍}努か 提交于 2019-12-06 19:22:46
问题 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 回答1: 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