Constants in C

Constants values are those values which can’t alter or change during program execution. These fixed (constants) value are also known as literals. Constants can be any data types like an integer constant, a floating constant, a character constant, or string literal.

Integer literal:

An integer literal is a sequence of digits and denotes a number. An integer literal is either signed or unsigned. The sign of a literal determines how C– decides whether it stats into its type:

  • A literal starting with “0x” or “0X” is in hexadecimal notation (base 16) and is unsigned.
  • A literal starting with ‘0’ (zero) is in octal notation (base 8) and is unsigned.
  • A literal starting with any of the digits 1 through 9 and ending in the letter “u” or ‘U’ is in decimal notation (base 10) and is unsigned. Also, a literal ‘0u’ or ‘0U’ is in decimal notation and is unsigned.
  • A literal starting with any of the digits 1 through 9 and ending in a digit is in decimal notation (base 10) and is signed.
  • A literal starting with a minus sign, followed by any of the digits 1 through 9, and ending in a digit, is in decimal notation (base 10) and is signed.

Examples:

Floating-point literal:

A floating-point literal denotes a floating-point number. It has  an integer part, a decimal point, a fractional  and an exponent part. It is a checked compile-time error when the floating-point literal can’t be represented by k bits where ‘k’ is the native word size. A floating-point literal may not begin with a dot.

Example:

Character literals:

A character literal is a value of type bits 8 by default. It is specified as an ASCII character enclosed in single quotes:’a’ denotes the decimal value 97 (ASCII code for ‘a’). A character literal can be a plain character (e.g., ‘x’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u02C0’).

After execution of above , we will get below result:

String literal:

A string literal is an abbreviation for a sequence of character literals and can be used only to define the initial value of data. A String literal is terminated by double quotes and must contain only printable characters. Each character is value of type bits 8. You can break a long line into multiple lines using string literals and separating them using white spaces.

 

Related posts