gpt4 book ai didi

c - 使用 typeof 编译器扩展在 C 中实现 min 函数会出现错误

转载 作者:行者123 更新时间:2023-11-30 16:44:36 24 4
gpt4 key购买 nike

当我尝试在 C 中实现 min 函数时,因为 C 缺少此函数,编译器给出错误。我已经使用 type of 来实现它,就像这样

MIN and MAX in C

My code

快速引用:

>#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.

最佳答案

应该是:

#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); // Min defined \
_a < _b ? _a : _b; })

您的评论应该\之后,而是在它之前。另外你写的 typeof 不带下划线。推荐使用它们,给出解释here at SO .

typeof(), __typeof__() and __typeof() are compiler-specific extensions to the C language, because standard C does not include such an operator.

<小时/>

不带 typeof 的宏:

如果您的编译器支持typeof“运算符”,您可以定义不带typeof的min宏:

#define min(a,b) (((a) < (b)) ? (a) : (b))

但是这样一来,在变量上使用后缀或前缀 in/decrement 运算符时就会产生副作用,因为会发生双重计算。

<小时/>

内联函数:

另一种使其安全的可能性是内联函数。它将是类型安全的并且不会发生双重评估:

inline int min(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}

你失去了类型的通用性,但没有其他办法。您还可以考虑使用 double,因为整数将被隐式转换。但请记住精度的松散。

关于c - 使用 typeof 编译器扩展在 C 中实现 min 函数会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com