预处理器 - 样本 [英] Preprocessor - sample
本文介绍了预处理器 - 样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < stdio.h >
#define SIZE 10;
#define FUDGE SIZE -2;
int main()
{
int size; / * 真正使用的尺寸* /
size = FUDGE;
printf( 大小为%d \ n,大小);
return ( 0 );
}
以下程序的o / p大小值是多少?
解决方案
< blockquote>你编译并运行它了吗?
结果不符合预期?
当你自己执行预处理器任务时,它是预期的:
size = FUDGE;
// 替换FUDGE = SIZE -2;
size = SIZE - < span class =code-digit> 2 ;;
// 替换SIZE = 10;
size = 10 跨度>; - 2 ;;
你看错了吗? #define语句不需要以分号结尾。如果有的话,它也会插入。
- 你不应该在<$ c $结尾处放置分号c> #define 语句,它们可能会在您的代码中产生不必要的副作用。
- 在
#define
中使用表达式时出于同样的原因,你应该把它们放在括号中。
#include <stdio.h>
#define SIZE 10;
#define FUDGE SIZE -2;
int main()
{
int size;/* size to really use */
size = FUDGE;
printf("Size is %d\n", size);
return (0);
}
what is the value of size in o/p for the following program?
解决方案
Did you compile and run it?
And the result is not as expected?
It is as expected when you perform the preprocessor tasks yourself:
size = FUDGE; // replace FUDGE = SIZE -2; size = SIZE -2;; // replace SIZE = 10; size = 10; -2;;
Did you see your mistake? #define statements need no trailing semicolon. If there is one, it is also inserted.
- You should not put semi-colons at the end of
#define
statements, they may have unwanted side effects in your code.- When using expressions in
#define
s you should put them in parentheses, for the same reason.
这篇关于预处理器 - 样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文