C Constants and Literals

    1 Votes

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) named Decimal (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; 

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

Floating Point Constants/Literals

Floating point constants represents the floating point numbers, as the name suggests ie; numbers like 1.2, 3.6894 etc. In C programming language, we can represent a floating point constant in two ways,

  • Decimal point format or in
  • Exponent format.

In order to represent a floating point constant in decimal format, you need to include the decimal point (.) in the number. Floating point numbers can be expressed in exponent format like 1.23E-4 or 1.23e-4. It is another way to represent 1.23 x 10-4. The number after E can be signed, but should always be an integer. So, 3.56E1.2 is an invalid floating point constant. Some valid floating point constants in C are given below

3.968421
3968421E-6L
// Both of the above float literals represent the same value.

Character constants

Character constants hold a single character enclosed in single apostrophes. Characters are short integers. So, it can hold an integer value as well (maximum value character constants can hold is 127 and 255 for unsigned character constants). Also, different characters have associated integer values (like 'A' is 65, 'a' is 97 etc.). These values are standard and known as ASCII (American Standard Code for Information Interchange).

There are some characters which have special use in C language compared to other characters. They are preceeded by a backslash(\) (like \n, \b). These characters are called escape sequences. Below, you can find examples of escape sequences available in C.

CharactersEscape sequence
bell \a
backspace \b
horizontal tab \t
vertical tab \v
newline \n
form feed \f
carriage return \r
quotation mark \"
apostrophe \'
question mark \?
backslash \\
null \0

For example, using '\n' in printf statement will feed a line in the output. Character constants can hold escape sequences too. Examples of few of the character constants are given below.

'a'
'b'
1
2
'\n'
'\0'

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

String Constants/Literals

String constants consist of any number of consecutive characters in enclosed quotation marks ("). String constants can have any valid character as part of it, including escape sequences. Example of string literal is given below

"Hello World!"

Some string constants can span multiple lines like the one given below

"First line \nSecond line \nThird line"
// Above statement when printed will look like as given below
First line 
Second line 
Third line

If you want to declare a string constant with quotation marks or backslash as part of the string, then you have to include proper escape character (\) for that. For example if you want a double quote (") as part of your string (like - Press the "Enter" button) you can do it as given below

"Press the \"Enter\" button"
// Above statement when printed will be: Press "Enter" button

Here middle two quotations are to be included in the string constant and so are used with escape sequences. In C programming language, system internally stores the string as a character array with a null character(\0) as the terminator. ie; null character marks the end of a string. After declaring a string constant, system automatically adds a null character(\0) after it, which denotes the end of string, as given below.

char my_string[] = "My String";
// Compiler will interpret the above statement as
char my_string[10] = {'M', 'y', ' ', 'S', 't', 'r', 'i', 'n', 'g', '\0'};

Enumeration Constants

Enumeration constants represents the individual values of an enumeration set. They are actually integer literals internally. In the following enum 'Boolean', we've two enumeration constants 'TRUE' and 'FALSE'. They internally has integer constant values 1 and 0 respectively.

enum Boolean
{
  FALSE = 0,
  TRUE = 1
}	

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know