首页 > 解决方案 > C语言中的断言

问题描述

所以我尝试编写一个代码将 kmph 转换为 m/s 并想使用 Assert 进行验证。

这是我写的一段代码。尝试与打印语句比较时该值是正确的,但断言给出错误

#include <stdio.h>
#include <assert.h>


float convert(float num);

int main()
    {
        float kmph;
         printf("Enter Velocity in Kmph\n");
        scanf("%f", &kmph);

        printf("Velocity in m/s is %f\n", convert(kmph));
        assert(convert(1) == 0.277778);
        assert(convert(18) == 5.0);
        return 0;
    }

     float convert(float num)
        {
           float mps;
           mps = num * 0.277778;

           return mps;

          }

错误描述:

   Enter Velocity in Kmph
   1
  Velocity in m/s is 0.277778
   a.out: program4.c:14: main: Assertion `convert(1) == 0.277778' failed.
    Aborted (core dumped)

标签: assert

解决方案


推荐阅读