开发者

Difference and definition of literal and symbolic constants in 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开发者_StackOverflow them and highlight their differences. Thanks!


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;


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 do

#define A 5

and then

b += A;

The precompiler translates it into

b += 5;

and all the compiler sees is the number 5.


(Borrowing from earlier posts) A literal constant is a value typed directly into your program wherever it is needed. For example

int breakpoint = 10;

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.

#define TEN 10      /* These two lines of code become one... */
breakpoint  +=  TEN;    /* after running through the precompiler  */

The precompiler translates it into

Breakpoint  +=  10;

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜