使用%f格式说明符打印int [英] printing int using %f format specifier

查看:255
本文介绍了使用%f格式说明符打印int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么输出为0.000000。
我知道使用float格式说明符或使用b类型的值类型来打印int会产生未指定的行为,如许多地方所写。
我引用了它

I wanted to know why output is coming as 0.000000. I know printing int using float format specifier or using a type of value using b type gives unspecified behaviour as written in many place. I'm quoting it


如果转换规范无效,则行为是
undefined。如果有的话参数不是对应的
转换规范的正确类型,行为未定义。

"If a conversion specification is invalid, the behaviour is undefined. If any argument is not the correct type for the corresponding conversion specification, the behaviour is undefined."

但是它总是给0.000000必须在寄存器或汇编代码中定义一些内容。

But it is always giving 0.000000 there must be something defined going either in register or in assembly code.

总之,我想知道对于任何i值,输出始终为0.000000的原因是什么? / p>

In short I wanted to know what cause output to be always 0.000000 for any value of i?

#include<stdio.h>
int main()
{
    int i=10;
    printf("%f\n",i);
    return 0;
}

所有人都说我知道我的价值可能在产出中,但是由于岁差窗口大小,它不会显示在控制台上。
如果我使用%e,我会得到

All said I know value of i may be somewhere in output but due to precession window size it is not shown on console. If I use %e I'm getting

推荐答案

使用

printf(%f\n,(float)i );

编译器不会自动将int转换为浮点型

The compiler do not automatically cast your int to float

编辑
我认为这是一个有趣的问题,因此我找到了类似的文章:
C中的printf函数代码

然后我尝试浏览__printf,vfprintf和___ printf_fp

Then I tried to explore __printf , vfprintf and ___printf_fp

__printf (const char *format, ...)
{
   va_list arg;
   int done;

   va_start (arg, format);
   done = vfprintf (stdout, format, arg);
   va_end (arg);

   return done;
}

在vfprintf中,他们定义了一个jump_table,用于处理特定格式(%d ,%f,%x ...)

In vfprintf, they define a jump_table, that processes the specific format (%d, %f, %x ...)

static const uint8_t jump_table[] =
 {
   /* ' ' */  1,            0,            0, /* '#' */  4,
              0, /* '%' */ 14,            0, /* '\''*/  6,
              0,            0, /* '*' */  7, /* '+' */  2,
              0, /* '-' */  3, /* '.' */  9,            0,
   /* '0' */  5, /* '1' */  8, /* '2' */  8, /* '3' */  8,
   /* '4' */  8, /* '5' */  8, /* '6' */  8, /* '7' */  8,
   /* '8' */  8, /* '9' */  8,            0,            0,
              0,            0,            0,            0,
              0, /* 'A' */ 26,            0, /* 'C' */ 25,
              0, /* 'E' */ 19, /* F */   19, /* 'G' */ 19,
              0, /* 'I' */ 29,            0,            0,
   /* 'L' */ 12,            0,            0,            0,
              0,            0,            0, /* 'S' */ 21,
              0,            0,            0,            0,
   /* 'X' */ 18,            0, /* 'Z' */ 13,            0,
              0,            0,            0,            0,
              0, /* 'a' */ 26,            0, /* 'c' */ 20,
   /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
   /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28,            0,
   /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
   /* 'p' */ 22, /* 'q' */ 12,            0, /* 's' */ 21,
   /* 't' */ 27, /* 'u' */ 16,            0,            0,
   /* 'x' */ 18,            0, /* 'z' */ 13
 };

然后他们将LABEL放在此vfprintf函数中(类似切换用例)

Then they put LABEL in this vfprintf function (something like switch case)

int vfprintf (FILE *s, const CHAR_T *format, va_list ap)
{
...
LABEL (form_percent):           
  /* Write a literal "%".  */   
  outchar (L_('%'));            
  break;                        

LABEL (form_integer):           
  /* Signed decimal integer.  */
  base = 10;

LABEL (form_float): 
...
}

在标签 form_float 中,他们定义了一个结构,该结构定义了浮点格式

In Label form_float, they define a struct which defined float format

struct printf_info info = { 
.prec = prec,                    
.width = width,                  
.spec = spec,                    
.is_long_double = is_long_double,
.is_short = is_short,            
.is_long = is_long,              
.alt = alt,                      
.space = space,                  
.left = left,                    
.showsign = showsign,            
.group = group,                  
.pad = pad,                      
.extra = 0,                      
.i18n = use_outdigits,           
.wide = sizeof (CHAR_T) != 1 
}; 

最后,他们称

int ___printf_fp (FILE *fp, 
                  const struct printf_info *info,
                  const void *const *args)

打印输出。

结论:如果输入格式不正确,我们将使用错误的格式结构信息,那么输出也必定是错误的。

Conclusion: if input format is not correct, we wil have a wrong format struct info, then output must be wrong too.

这篇关于使用%f格式说明符打印int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆