Home > Electronics Tutorials > C Language Tutorial > Identifiers - Constants

C Language Programming Library Reference Guide

Identifiers - Constants

Constants provide a way to define a variable which cannot be modified by any other part in the code. Constants can be defined by placing the keyword const in front of any variable declaration. If the keyword volatile is placed after const, then this allows external routines to modify the variable (such as hardware devices). This also forces the compiler to retrieve the value of the variable each time it is referenced rather than possibly optimizing it in a register.

Constant numbers can be defined in the following way:

Hexadecimal constant: 0x hexadecimal digits...
Where hexadecimal digits is any digit or any letter A through F or a through f. Decimal constant: Any number where the first number is not zero. Octal constant: Any number where the first number must be zero. Floating constant: A fractional number, optionally followed by either e or E then the exponent. The number may be suffixed by:
U or u: Causes the number to be an unsigned long integer. L or l: If the number is a floating-point number, then it is a long double, otherwise it is an unsigned long integer. F or f: Causes the number to be a floating-point number. Examples: const float PI=3.141;
Causes the variable PI to be created with value 3.141. Any subsequent attempts to write to PI are not allowed.

const int joe=0xFFFF;
Causes joe to be created with the value of 65535 decimal.

const float penny=7.4e5;
Causes penny to be created with the value of 740000.000000.
Note: To report broken links or to submit your projects, tutorials please email to Webmaster

Discover

     more......