我有以下代码:
#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
这就是为什么在项目中拥有某种静态代码分析器总是好的。
关于c - Typedef 参数和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40864040/