user-0 提问: C语言printf函数问题

回复:
user: user-1
content: 6楼正解！

user: user-2
content: 在缓冲区了 会把所有的数都存着 存的是1.2345 而你输出是按float格式输出的 所以··只会去前面的一部分··

user: user-3
content: 总结；跟编译器有关，但定义float型加f更明了，且没有错。

user: user-4
content: float f = 1.2345;这句报的，1.2345是double类型，1.2345F才是float类型。

user: user-5
content: 没有问题，跟你的编译器有关

user: user-6
content: 什么提示都没  正常运行-.-

user: user-7
content: 引用 13 楼 sai_johnny 的回复:MSDN《C Floating-Point Constants》中“A floating-point constant without an f, F, l, or L suffix has type double.” 即，没有f, F, l, L后缀的浮点常量是double型。 这估计在微软的编译器中才有体现。 再结合我的情况，这么说来就是微软的问题了--这估计在微软的编译器中才有体现。。

user: user-8
content: f加一个，解决问题

user: user-9
content: 你定义的数，应该在后面加一个 f，否则会被看成是 double 类型的

user: user-10
content: 那要看編譯器,我用的都不會警告,可能跟設定有關 但以常量來說,整數都會被當INT類型(除非比INT大) 小數點都會被當DOUBLE類形  有需要加上后缀就好啦

user: user-11
content: MSDN《C Floating-Point Constants》中“A floating-point constant without an f, F, l, or L suffix has type double.” 即，没有f, F, l, L后缀的浮点常量是double型。 这估计在微软的编译器中才有体现。

user: user-12
content: float初始化就应该加f，否则会自动转化成double类型。

user: user-13
content: 引用 楼主 heyuntao112356 的回复:#include <stdio.h>  int main(int argc,char *argv[]) { 	float f = 1.2345;  	printf("%f",f);  	return 0; }  编译为什么会有警告，'initializing' : truncation from 'const double ' to 'float ' printf函数入栈时已经将f转换成double类型了，为什么还有警告？  这个警告不是print的警告，而是定义f时，用1.2345初始化f的警告呀

user: user-14
content: 学会看提示，可以查网上字典或自动翻译

user: user-7
content: 引用 3 楼 mgy4938 的回复:1.2345默认数据类型是double，你将它定义为float，精度肯定是不够滴，高精度向低精度转换是不允许滴，会截断的。为什么是double呢？gcc下编译，运行都正常。

user: user-15
content: 引用 3 楼 mgy4938 的回复:1.2345默认数据类型是double，你将它定义为float，精度肯定是不够滴，高精度向低精度转换是不允许滴，会截断的。++

user: user-16
content: 就是说f是double类型的。 应该定义为double

user: user-1
content: 常量也有类型。 C++ Floating-Point Constants Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.  Syntax  floating-constant :  fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt  fractional-constant :  digit-sequenceopt . digit-sequence digit-sequence .  exponent-part :  e signopt digit-sequence E signopt digit-sequence  sign : one of  + –  digit-sequence :  digit digit-sequence digit  floating-suffix :one of  f l F L  Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example:  18.46 38.  The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example:  18.46e0      // 18.46 18.46e1      // 184.6  If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0.  Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively.  Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like  void func( double );  and   void func( long double );  C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.  Syntax  integer-constant :  decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence'  decimal-constant :  nonzero-digit decimal-constant digit  octal-constant :  0 octal-constant octal-digit  hexadecimal-constant :  0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit  nonzero-digit : one of  1 2 3 4 5 6 7 8 9  octal-digit : one of  0 1 2 3 4 5 6 7  hexadecimal-digit : one of  0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F  integer-suffix :  unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt  unsigned-suffix : one of  u U  long-suffix : one of  l L  64-bit integer-suffix :  i64  To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type.  To specify a decimal constant, begin the specification with a nonzero digit. For example:  int i = 157;   // Decimal constant int j = 0198;  // Not a decimal number; erroneous octal constant int k = 0365;  // Leading zero specifies octal constant, not decimal  To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example:  int i = 0377;   // Octal constant int j = 0397;   // Error: 9 is not an octal digit  To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:  int i = 0x3fff;   // Hexadecimal constant int j = 0X3FFF;   // Equal to i  To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example:  unsigned uVal = 328u;             // Unsigned value long lVal = 0x7FFFFFL;            // Long value specified                                    //  as hex constant unsigned long ulVal = 0776745ul;  // Unsigned long value

user: user-17
content: gcc不会有警告。  引用 楼主 heyuntao112356 的回复:#include <stdio.h>  int main(int argc,char *argv[]) { 	float f = 1.2345;  	printf("%f",f);  	return 0; }  编译为什么会有警告，'initializing' : truncation from 'const double ' to 'float ' printf函数入栈时已经将f转换成double类型了，为什么还有警告？

user: user-18
content: 'initializing' : truncation from 'const double ' to 'float '