C 中文字常量和符号常量的区别和定义?
我很难掌握符号和文字常量的定义和使用,我想知道是否有人可以解释它们并强调它们的差异。谢谢!
I am having trouble getting to grips with the definition and uses of symbolic and literal constants and I was wondering if you anyone could explain them and highlight their differences. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
符号是编译器处理的东西。编译器对待 const 的方式与对待变量的方式非常相似。另一方面,#define 是编译器甚至不知道的东西,因为预编译器将其转换为其值。这就像搜索和替换。如果这样做
,那么
预编译器会将其转换为
数字 5,编译器看到的只是数字 5。
A symbol is something that the compiler deals with. The compiler treats a
const
pretty much the way it treats a variable. On the other hand, a#define
is something the compiler is not even aware of, because the precompiler transforms it into its value. It's like search-and-replace. If you doand then
The precompiler translates it into
and all the compiler sees is the number 5.
文字常量是在程序需要时直接键入的值。例如
int tempInt = 10;
tempInt 是 int 类型的变量; 10 是一个字面常量。您无法为 10 赋值,并且其值无法更改。符号常量是用名称表示的常量,就像表示变量一样。然而,与变量不同的是,常量初始化后,其值就无法更改。
如果你的程序有一个名为students的整型变量和另一个名为classes的整数变量,那么在已知班级数量的情况下,如果你知道每个班级有15名学生,你可以计算出你有多少学生:
students =classes * 15;
A literal constant is a value typed directly into your program wherever it is needed. For example
int tempInt = 10;
tempInt is a variable of type int; 10 is a literal constant. You can't assign a value to 10, and its value can't be changed. A symbolic constant is a constant that is represented by a name, just as a variable is represented. Unlike a variable, however, after a constant is initialized, its value can't be changed.
If your program has one integer variable named students and another named classes, you could compute how many students you have, given a known number of classes, if you knew there were 15 students per class:
students = classes * 15;
(借用之前的帖子)
文字常量是在程序需要时直接键入的值。例如
变量断点是一个整数(int); 10 是一个字面常量。您无法为 10 赋值,并且其值无法更改。与变量不同,常量在赋值(初始化)后就无法更改。
符号是编译器处理的东西。在此示例中,TEN 是使用#define 函数创建的符号常量。 #define 是编译器甚至不知道的东西,因为预编译器将其转换为其分配(定义)的值。预编译器搜索并用值替换程序中的每个符号常量。
预编译器将其转换为
编译器永远不会看到 10,而只会看到其分配的值 10。为什么这很有用?如果断点更改为 11,会怎么样。与其查看整个程序并将每个变量定义更改为使用文字常量 10 设置的新值,不如将单个符号常量的定义... 10 更改为 11 并让预编译器为您进行更改。
(Borrowing from earlier posts)
A literal constant is a value typed directly into your program wherever it is needed. For example
The variable breakpoint is an integer (int); 10 is a literal constant. You can't assign a value to 10, and its value can't be changed. Unlike a variable, a constant can't be changed after it is assigned a value (initialized).
A symbol is something that the compiler deals with. In this example, TEN is a symbolic constant created using the #define function. A #define is something the compiler is not even aware of, because the precompiler transforms it into its assigned (defined) value. The precompiler searches out and replaces every symbol constant inside your program with a value.
The precompiler translates it into
The compiler never sees TEN but only its assigned value, 10. Why is this useful? What if the breakpoint is changed to 11. Rather than looking through the entire program and changing every variable definition to the new value that was set using a literal constant, 10, change the definition of a single symbol constant... TEN to 11 and let the precompiler do the changes for you.
我认为你的意思是,文字常量是一个原始表达式,如“string”或 2 或 false,而符号常量是当你给它一个名称时,如 const int MagicNumber = 42。两者都可以用作表达式,但是您可以用名称来引用后者。如果您在许多地方使用相同的常量,则很有用。
I think what you mean is that a literal constant is a primitive expression like "string" or 2 or false, while a symbolic one is when you give it a name, like const int MagicNumber = 42. Both can be used as expressions, but you can refer to the latter with a name. Useful if you use the same constant from many places.