当我尝试在C中实现min函数时,因为C缺少此函数,编译器会给出错误。我已经使用了类型来实现它,就像这样
供快速参考:
>#define min(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \ //Min defined
_a < _b ? _a : _b; })
.
.
.
c= min(c+floor(1.0/f), bi.biWidth) ;
r= min(r+floor(1.0/f),abs(bi.biHeight)) ;
生成错误:
>make resize
clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall
-Werror -Wextra -Wno-sign-compare -Wshadow resize.c -lcrypt -lcs50
-lm -o resize
resize.c:191:18: error: implicit declaration of function 'typeof' is
invalid in C99 [-Werror,-Wimplicit-function-declaration]
c= min(c+floor(1.0/f), bi.biWidth) ;
^
resize.c:11:6: note: expanded from macro 'min'
({ typeof (a) _a = (a); \
^
resize.c:191:18: error: expected ';' after expression
resize.c:11:17: note: expanded from macro 'min'
({ typeof (a) _a = (a); \
^
resize.c:191:18: error: use of undeclared identifier '_a'
resize.c:11:17: note: expanded from macro 'min'
({ typeof (a) _a = (a); \
^
resize.c:191:18: error: expected ';' after expression
resize.c:12:18: note: expanded from macro 'min'
typeof (b) _b = (b); \
^
resize.c:191:18: error: use of undeclared identifier '_b'
resize.c:12:18: note: expanded from macro 'min'
typeof (b) _b = (b); \
^
resize.c:191:18: error: use of undeclared identifier '_a'
resize.c:13:5: note: expanded from macro 'min'
_a < _b ? _a : _b; })
^
resize.c:191:18: error: use of undeclared identifier '_b'
resize.c:13:10: note: expanded from macro 'min'
_a < _b ? _a : _b; })
^
resize.c:191:18: error: use of undeclared identifier '_b'
resize.c:13:20: note: expanded from macro 'min'
_a < _b ? _a : _b; })
^
resize.c:193:13: error: expected ';' after expression
r= min(r+floor(1.0/f),abs(bi.biHeight)) ;
^
resize.c:11:17: note: expanded from macro 'min'
({ typeof (a) _a = (a); \
^
resize.c:193:13: error: use of undeclared identifier '_a'
resize.c:11:17: note: expanded from macro 'min'
({ typeof (a) _a = (a); \
^
resize.c:193:13: error: expected ';' after expression
resize.c:12:18: note: expanded from macro 'min'
typeof (b) _b = (b); \
^
resize.c:193:13: error: use of undeclared identifier '_b'
resize.c:12:18: note: expanded from macro 'min'
typeof (b) _b = (b); \
^
resize.c:193:13: error: use of undeclared identifier '_a'
resize.c:13:5: note: expanded from macro 'min'
_a < _b ? _a : _b; })
^
resize.c:193:13: error: use of undeclared identifier '_b'
resize.c:13:10: note: expanded from macro 'min'
_a < _b ? _a : _b; })
^
resize.c:193:13: error: use of undeclared identifier '_b'
resize.c:13:20: note: expanded from macro 'min'
_a < _b ? _a : _b; })
^
15 errors generated.
答案 0 :(得分:0)
应该是:
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); // Min defined \
_a < _b ? _a : _b; })
您的评论不应该在\
之后但在此之前。
你也写下没有下划线的typeof。建议使用它们,给出解释here at SO。
typeof()
,__typeof__()
和__typeof()
是C语言的特定于编译器的扩展,因为标准C不包含此类运算符。
没有typeof的宏:
如果你的编译器不支持typeof
“运算符”,你可以定义没有typeof的min宏:
#define min(a,b) (((a) < (b)) ? (a) : (b))
但是,在变量上使用postfix或前缀/减量运算符时会产生副作用,因为会发生双重评估。
内联功能:
使其安全的另一种可能性是内联函数。它将是类型安全的,不会发生双重评估:
inline int min(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
你失去了类型的通用性,但没有别的办法。您也可以考虑使用double,因为整数将被隐式转换。但要记住精确度的松散。