【发布时间】:2021-05-18 17:19:53
【问题描述】:
根据给出的一些建议,我编辑了代码以显示以下内容。
#include <stdio.h>
#include <stdlib.h>
//function declaration
unsigned char float_to_hex(float value);
int main()
{
unsigned char hex_value=0x00;
//variable that stores returned hex value
hex_value = float_to_hex(1.0);
printf("The hex value is %x\n", hex_value);
return 0;
}
// Function definition
unsigned char float_to_hex(float value)
{
float atten_float=0.0;
unsigned char atten_hex=0x00;
if( atten_float == 0.0)
{
atten_hex = 0x00;
return atten_hex;
}
else if (atten_float== 0.5)
{
atten_hex = 0x01;
return atten_hex;
}
else if (atten_float == 1.0)
{
atten_hex = 0x02;
return atten_hex;
}
else
{
atten_hex = 0x00;
return atten_hex;
}
return -1;
}
我通过在函数定义中本地初始化变量来编辑代码。另外,我正在使用“%x”来打印“hex_value”的十六进制值,但是,我仍然得到相同的结果,即 0。
【问题讨论】:
标签: c if-statement hex return-value