简体   繁体   中英

Are literal numbers treated as constants?

It is always better for contants such as PI to #define them or declare them const so the compiler can optimize and it becomes less error prone. I was wondering however, how are literal numbers in statements treated? Ex:

float x;
const int y = 60;
x = y / 3.0f;

In this example how would 3.0f be treated? Would it inherit the optimizations of a constant?

What optimizations will take place depends on the compiler. In your case, both C and C++ compilers will normally have enough information to optimize your source code into identical machine code. In other words, it doesn't really depend much on what is literal and what is constant in this code.

Having said that, the meaning of the terms literal and constant are significantly different in C and C++ (and you tagged your question C and C++ at the same time).

  • In C 60 and 3.0f are constants , but y is not a constant . You can call y a const-qualified variable if you wish, but it is not a constant in C terminology, in a sense that a single y is not a constant expression in C.

As for literals , in C language the term literal only applies to string literals (and also compound literals in C99), ie there are no literals in your code at all.

  • In C++, 60 and 3.0.f are literals , which form constant expressions (integral and floating -point respectively). y is also a constant of int type, in a sense that a single y is a constant expression in C++.

The situation when you might notice the difference has nothing to do with optimizations, but rather with the way the languages are defined. For example, using the above y in a file-scope array type declaration is legal in C++, but not in C

 typedef int int_array[y]; /* OK in C++, ERROR in C */

Since by using a #define you ask the preprocessor to do a text replacement, your code is the same as the following:

#define VAL 3.0f

float x;
const int y = 60;
x = y / VAL;

How the direct const value is optimized is obviously dependent on the compiler. However if you watch at the assembly code (by example the one produced by gcc) you'll notice that the compiler directly writes the binary sequence encoding the value 3.0 in the floating point standard.

On some architectures, there is a limit to the size of string literal that can be used directly. When the size of the string literal is too big, the compiler will need to store the constant somewhere in read-only data memory and then load the value from memory when required.

If one stores the value in a constant variable, there is a good chance that the compiler will store only one value of the constant and use as appropriately. However if one #defines the constants, this just makes the preprocessor place the literal value into the code and therefore there is a chance that the compiler not realize that you are using the same value and store the constant multiple times. It is for this reason that const variables are better than #defines.

The answer depends on whether you're using C or C++, so you need to pick one and stop pretending they're the same thing. In C, the const keyword does not declare a constant ; it declares a variable for which attempts to modify that variable invoke undefined behavior. In C, you should never declare const int y; unless it's static/global and intended to convey internal information from one module to another which you don't want to be fixed at the other module's compiletime (for example, const int my_library_version = 0x1001; in a shared library might be useful, although a function call to return the value would be cleaner).

As for optimizations, no C compiler I know of will optimize out the division in your example, and it's questionable whether the compiler is even allowed to.

As for C++, you should ask a C++ expert to explain (of which I am not one).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM