【问题标题】:Strange behaviour of printf string format in CC中printf字符串格式的奇怪行为
【发布时间】:2013-06-03 14:27:30
【问题描述】:

据我了解,我在nesC 中具有以下功能,与C 基本相同!

event void AdaptiveSampling.dataAvailable(error_t result, float val, bool isRealData)
{       
    if(result == SUCCESS)
    {
        //converting raw ADC to centigrade
        centiGrade = -39.60 + 0.01 * val;                       

        //printing the value to serial port
        if(isRealData)
        {
            printf("REAL: Temperature is: %d CentiGrade\r\n", centiGrade); //line 91
            printf("%d,R,%d,%d\r\n", _counter, val, centiGrade); //line 92
        }   
        else
        {
            printf("PEDICTED: Temperature is: %d CentiGrade\r\n", centiGrade); //line 96
            printf("%d,P,%d,%d\r\n", _counter, val, centiGrade); //line 97
        }   
        _counter++;     
    }
    else
    {
        printf("Error reading sensor!");
    }
}

而且,在我的代码顶部,我定义了这些变量:

uint32_t _counter;
uint16_t centiGrade;

这是我在构建过程中收到的警告:

AdaptiveSamplingC.nc:92:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘uint32_t’ [-Wformat]
AdaptiveSamplingC.nc:92:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘float’ [-Wformat]
AdaptiveSamplingC.nc:97:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘uint32_t’ [-Wformat]
AdaptiveSamplingC.nc:97:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘float’ [-Wformat]

这是屏幕输出的示例:

PEDICTED: Temperature is: 26 CentiGrade
291,P,0,-3402
REAL: Temperature is: 26 CentiGrade
292,R,0,4096
PEDICTED: Temperature is: 26 CentiGrade
293,P,0,-1495

问题:

在第 91 行,我希望看到温度值是浮动的...我的意思是像 26.25...但由于某种原因它打印为整数。我尝试将%d 更改为%f,但它没有帮助,因为您看到第 92 行和第 97 行的输出由于我无法弄清楚的原因而几乎损坏!

我也没有解释为什么第 92 行和第 97 行表现得那么奇怪,以及为什么在构建期间它们会出现警告。

您能告诉我如何改进吗?

【问题讨论】:

  • 为什么要使用无符号整数类型来表示明显是有符号浮点数的值?
  • %u 是你应该用来代替 %d 的东西。您无法从整数中获取浮点值。对于浮点数,你必须使用浮点数(和%f)

标签: c printf string-formatting tinyos nesc


【解决方案1】:

您的问题是未定义的行为,这就是您收到警告的原因。警告表示确实有问题。

printf 是一个可变参数函数,因此它需要一些关于参数类型的信息。这是格式说明符的工作(例如%d)。

%d 告诉 printf 期待 int 类型的参数,您的警告消息告诉您:警告:格式 %d 期待 int 类型的参数

但是,您提供的类型不是ints。它们是 uint32_t 和 float,您的警告消息也告诉您:但参数 2 的类型为 uint32_t [-Wformat] 并且但参数 3 的类型为 @987654332 @[-Wformat].

有很多解决方案。最好使用正确的格式说明符!(duh)。那是"%d" 对应int,"%"PRIu32 对应uint32_t 和"%f" 对应double(floats 被提升为)。对于uint32_t,您可以转换为unsigned long,然后使用"%lu" 打印。

【讨论】:

  • 你的用户名和我的问题只是巧合吗?我吓坏了!
  • @Saeid87 这是一个 C 标准技术术语,用于表示不可移植、错误且 C 标准没有任何要求的行为。换句话说,未定义的行为意味着“这可能会起作用,如果只是巧合的话,但它也可能使你的计算机爆炸......你可能想避免它。”
【解决方案2】:

您正在将 16 位无符号整数变量与此行上的浮点计算混合在一起 centiGrade = -39.60 + 0.01 * val;

您想要的是将 centiGrade 保留为浮点数,并仅在打印时将其转换为 int。

【讨论】:

    【解决方案3】:

    警告说明了一切。您提供了错误的格式字符串。 (记住:警告是伪装的错误。总是修复它们。)

    对无符号整数使用%u,对浮点数使用%f。您甚至可以提供有关如何格式化浮点数的额外信息。例如,%.2f 会在小数点后给您 2 个数字。

    还有这个:

    centiGrade = -39.60 + 0.01 * val;  
    

    错了。您不能将浮点数分配给 uint16_t。只需将centiGrade 设置为float 就可以了。

    有关说明符的更多信息可以在这里找到:http://www.cplusplus.com/reference/cstdio/printf/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      相关资源
      最近更新 更多