Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想定义一个数字变量可以属于的固定范围。我使用的是 c89 编译器。所以,如果我这样做:
#define minValue -200
#define maxValue 200
我想要以下内容: typedef int range[minValue,maxValue]
typedef int range[minValue,maxValue]
可以做这样的事情吗?
不,这是不可能的。您必须添加一些代码来检查您的值是否在定义的范围内。
例如
typedef int Range; // must be between minValue and maxValue #define CheckRange(x) (((x)>=minValue) && ((x)<=maxValue)) [...] Range a = ...; // a call to a function or user input request if (!CheckRange(a)) // handle error