Friday, July 29, 2016

C Basics - Constants in C

A constant is an entity that doesn’t change during program execution. Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.

A "constant" is a number, character, or character string that can be used as a value in a program. Use constants to represent floating-point, integer, enumeration, or character values that cannot be modified. There are different types of constant in C:

Integer Constant:
  • An integer constant must have at least one digit.
  • It must not have a decimal point.
  • It can be either positive or negative.
  • If no sign precedes an integer constant it is assumed to be positive.
  • No commas or blanks are allowed within an integer constant.
  • The allowable range for integer constants is -32768 to 32767.
  • The range of an Integer constant depends upon the compiler. For a 16-bit compiler the range is –32768 to 32767. For a 32-bit compiler the range would be even greater.
  • An integer constant can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
  • An integer constant can also have a suffix that is a combination of u or U and l or L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
Example:
123
 +456
-789
345u
987ul
0x54

Real Constant:
Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form.
Fractional Form:
  • A real constant must have at least one digit.
  • It must have a decimal point.
  • It could be either positive or negative.
  • Default sign is positive.
  • No commas or blanks are allowed within a real constant.
Example:
+123.45
678.0
-91.234

Exponential Form:
The exponential form of representation of real constants is usually used if the value of the constant is either too small or too large. In exponential form of representation, the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is called exponent.
  • The mantissa part and the exponential part should be separated by a letter e.
  • The mantissa part may have a positive or negative sign.
  • Default sign of mantissa part is positive.
  • The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive.
  • Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Example:
+2.3e-4
5.2e6
-0.5e+7
-3.2e-5

A floating-point constant without an f, F, l, or L suffix has type double. If the letter f or F is the suffix, the constant has type float. If suffixed by the letter l or L, it has type long double.
For example:
100L  /* Has type long double  */
100F  /* Has type float        */

Character Constants:
A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. The maximum length of a character constant can be 1 character.
Example:
'A' 
 'I' 
 '5' 
 '='
char    schar =  'x';   /* A character constant          */

An escape sequence is regarded as a single character and is therefore valid as a character constant.

String Constants:
String constants are enclosed in double quotes "". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters. You can break a long line into multiple lines using string literals and separating them using white spaces.
Example:
"hello, world"

"hello, \

world"

"hello, " "w" "orld"
  • String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.
  • String literals have type array of char (that is, char[ ]). This means that a string is an array with elements of type char. The number of elements in the array is equal to the number of characters in the string plus one for the terminating null character.
  • The characters of a literal string are stored in order at contiguous memory locations. An escape sequence (such as \\ or \") within a string literal counts as a single character. A null character (represented by the \0 escape sequence) is automatically appended to, and marks the end of, each string literal.
  • String literals can be concatenated using backslash. The backslash causes the compiler to ignore the following newline character. For example, the string literal
    "Long strings can be bro\
    ken into two or more pieces."
    is identical to the string
    "Long strings can be broken into two or more pieces."
Enumeration constant:
An enumeration constant consists of a set of named integer constants.

enum BOOLEAN  /* Declares an enumeration data type called BOOLEAN */
{
    false,     /* false = 0, true = 1 */
    true 
}; 

Defining Constants:
In C, constants can be defined using #define preprocessor directive or const keyword.
#define preprocessor:
Given below is the form to use #define preprocessor to define a constant.

#define identifier value

Example:
#define PI 3.14
#define THIS_YEAR (2016)

const Keyword:
You can use const prefix to declare constants with a specific type as follows.

const type variable = value;

Example:
const int  LENGTH = 10;
const int  WIDTH = 5;
const char NEWLINE = '\n';



Related topics:
Data Types - Type Specifiers in C   |   Const and Volatile - Type Qualifiers in C   |   Variables in C   |   Local and Global Variables in C   |   Initialization in C

List of topics: C Programming

No comments:

Post a Comment