c语言printf中%g的含义是什么?代码如下

#include<stdio.h>
#include<complex.h>

int main(int argc,char* argv[])
{

complex comp = 5.2+5.0*I;
printf("%g + %gi\n",creal(comp),cimag(comp));

return 0;
}

转换说明及作为结果的打印输出%a 浮点数、十六进制数字和p-记数法(C99)
%A 浮点数、十六进制数字和p-记法(C99)
%c 一个字符
%d 有符号十进制整数
%e 浮点数、e-记数法
%E 浮点数、E-记数法
%f 浮点数、十进制记数法
%g 根据数值不同自动选择%f或%e.
%G 根据数值不同自动选择%f或%e.
%i 有符号十进制数(与%d相同)
%o 无符号八进制整数
%p 指针
%s 字符串
%u 无符号十进制整数
%x 使用十六进制数字0f的无符号十六进制整数
%X 使用十六进制数字0f的无符号十六进制整数
%% 打印一个百分号 使用printf ()函数 printf()的基本形式: printf("格式控制字符串",变量列表);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-04-01
%e %f %g都是输出浮点数的,%e使用科学记数法([-]d.ddd e [+/-]ddd),%f使用小数格式([-]dddd.ddd),%g在%e和%f之间自动选择。
第2个回答  2009-04-01
g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.

查看MSDN.
c int or wint_t When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character.
C int or wint_t When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character.
d int Signed decimal integer.
i int Signed decimal integer.
o int Unsigned octal integer.
u int Unsigned decimal integer.
x int Unsigned hexadecimal integer, using “abcdef.”
X int Unsigned hexadecimal integer, using “ABCDEF.”
e double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –.
E double Identical to the e format except that E rather than e introduces the exponent.
f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument.
p Pointer to void Prints the address pointed to by the argument in the form xxxx:yyyy where xxxx is the segment and yyyy is the offset, and the digits x and y are uppercase hexadecimal digits.
s String When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
S String When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
第3个回答  2009-04-01
%g用于输出浮点数(float以及double),它的特点是不会输出"多余"的0,比如以%f输出时是 0.500000 , 那么以%g输出时就是 0.5
第4个回答  2009-04-01
%f是指用浮点数的形式输出,也就是用小数了
%e是用指数的形式输出
%g是选用%f %e中较短的一种形式输出