Tuesday, January 19, 2016

C Constants and Literals

Constants in C programming language, as the name suggests are the data that doesn't change. Constants are also known as literals. There are five types of constants or literals available in C programming language as shown below.
  • Integer Constants
  • Floating-point Constants
  • Character Constants
  • String Constants
  • Enumeration Constants

Integer Constants/Literals

Integer constants, as the name suggests, are integer values ie; numbers like 1, 20 etc. In C programming language, we can have integer constants of 3 different bases (or radix) namedDecimal (Base 10), Hexadecimal (Base 16) or Octal (base 8). During the declaration of an integer constant, a prefix is used along with the value to specify the base or radix.
  • For decimal literals, no prefix is used.
  • Prefix used for hexadecimal: 0x / 0X
  • Prefix used for octal: 0

Examples of different integer constants
123        /* decimal constant*/
0x9b       /* hexadecimal constant*/
0X9c       /* hexadecimal constant*/
0456       /* octal constant*/
In C programming, you can use upper case or lower case letters (ABCDEF or abcdef) as part of hexadecimal literals.

We already have qualifiers like signed, unsigned, short, long for declaring integer variables. Also, we can use specific suffixes along with integer constants which are u/U and l/L. In case of long integer constants, the value should have letter L following it (UL in the case of unsigned long). Example of an integer constant with suffix is given below-
const long int big = 12345678L; 

No comments: