c - Typedef 参数和函数

标签 c function typedef

我有以下代码:

#include <stdio.h> 

typedef char km_ph;
typedef double nnew;

int printme(km_ph b){
        printf ("%c  %d\n", b,b);
        return 0;
    }

int main() {

    km_ph a = 'o';
    nnew b = 100.09;
    printme(9);
    printme(a);
    printme(b); 
    return 0;
}

我期望 printme(b)printme(9) 抛出某种类型的警告或错误,因为 b 的类型不是可以传递给 printme 的参数类型。 printme 需要 km_ph 类型,或者至少是 char 类型。我得到以下输出:

                    9
 0        111
 d        100

为什么会发生这种情况?

最佳答案

取决于编译器,在其上运行 FlexLint 会给出这个(请参阅重要的原型(prototype)强制)

FlexeLint for C/C++ (Unix) Vers. 9.00L, Copyright Gimpel Software 1985-2014 
--- Module: diy.c (C)
     1  #include <stdio.h> 
     2  
     3  typedef char km_ph;
     4  typedef double nnew;
     5  
     6  int printme(km_ph b){
     7          printf ("%c  %d\n", b,b);
     8          return 0;
     9      }
    10  
    11  int main() {
    12  
    13      km_ph a = 'o';
    14      nnew b = 100.09;
                      _
    15      printme(9);
diy.c  15  Warning 534:  Ignoring return value of function 'printme(char)' (compare with line 6)
                      _
    16      printme(a);
diy.c  16  Warning 534:  Ignoring return value of function 'printme(char)' (compare with line 6)
                     _
    17      printme(b); 
diy.c  17  Warning 524:  Loss of precision (arg. no. 1) (double to char)
diy.c  17  Info 747:  Significant prototype coercion (arg. no. 1) double to char
diy.c  17  Warning 534:  Ignoring return value of function 'printme(char)' (compare with line 6)
    18      return 0;
    19  }
    20  

see

这就是为什么在项目中拥有某种静态代码分析器总是好的。

关于c - Typedef 参数和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40864040/

相关文章:

C - 从 va_arg 中检索 int 数组的值

c - 为什么运算符在 CShell 中是右结合的,而在 C 中是左结合的?

sql - 如何将 SELECT 语句的一部分放入 Postgres 函数中

c++ - 从 Main 方法调用函数

c - return 在返回结构时从指针生成整数而不进行强制转换

c++ - 创建高效函数

c++ - 通过属性问题初始化typedef struct

c - 为什么 {typedef int* PTR;const PTR p=#} 不等于 "const int* p=&num"而等于 "int *const p=&num"?

c++ - 将 `std::sort` 与模板类中的成员函数一起使用

c - 64 位值的反转字节