A constant or sometimes called literal is an entity that doesn’t change, whereas a variable is an entity that can change. In this section we will discuss constants, variables are discussed later.
Types of constants
In C there is two type of constants:
- Primary constants.
- Secondary constants.
Both types are further divided into more categories as shown in the figure.
Here we will discuss only Primary constants, Secondary constants will be discussed later. To create Primary constants you need to obey the following rules.
Numeric constants are digits that may or may not have a decimal point. The rules for creating Numeric constants are:
- Must have at least one digit.
- No space, comma or any other special symbols are allowed.
- It can be positive or negative if no sign precedes integer constant then it is assumed to be positive.
Numeric Constants are two types:
- Integer constants
- Floating point or real constants
Integer constants do not have decimal point (
.). They can be written using decimal numbers (base 10), octal numbers(base 8) and hexadecimal (base 16).
It contains digits between
9, but should not begin with a zero.
43, 199, 3452, -100
It contains digits between
7, and must begin with a zero.
012, 034, 01144
It contains digits from
9 , and letters from
f (can be either uppercase or lowercase), and must always start with
0x23, 0Xff, 0x37a
As already said letters can be uppercase or lowercase.
0x23, 0XFF, 0x37A
We can also mix decimal, octal and hexadecimal.
int i = 12 + 045 + 0x3a;
Always keep in mind that whatever number system we use to represent a number; they have no effect on how they are stored internally in memory (eventually every numbers and character are stored as streams of binary digits). Octal and hexadecimal are commonly used while writing low-level programs, so, in the beginning, you will not see them much.
By default, an integer constant is of type
int. In a 32-bit system
int type occupies 4 bytes of memory. If the value of an integer is too large to be stored as an
int then the type of the constant is
long int. Again if the value is still too large for the
long int, then the compiler will try
unsigned long int eventually. Rules for determining type of octal and hexadecimal are as follows:
The compiler will go through the following type (top to bottom) until it finds a suitable type that can represent the constant.
unsigned long int
You can also explicitly specify the type of an integer constant as
long int by appending
10l, 3789L, 094l, 0xabL
Similarly, to specify the type of an integer constant as
unsigned int append
U after it:
23u, 034U, 0x8au
We can also use
L to specify the type of an integer constant as an
unsigned long int.
89ul – is of type
unsigned long int
Floating point or real constants
Numeric constants having decimal point are called floating point or real constants.
Floating-point constants can be written in two forms:
- Fractional forms
- Exponential form or Scientific notation
Here are the rules for creating floating point constants in Fractional form:
- Must have one at least one digit
- Must have a decimal point
- Can be positive or negative, default is positive
- No comma, blanks, or any other symbols are allowed
Here are some examples:
Exponential form is used in cases when a number is tool small or too large. For e.g 0.00000941 can be represented as 9.41e6. The part of the number before e is called mantissa i.e 9.41, whereas part following e is called exponent i.e
Here are the rules for creating floating point constants in Exponential form:
- Mantissa and exponent must be separated by e or E.
- Mantissa can be positive or negative, default is positive.
- Exponent must have at least one digit.
- Exponent can be positive or negative default is positive
Some examples of floating point number is exponential form:
By default, floating constants are of type
double. We can explicitly mention the type of a floating-point constant as a
float by appending
F at the end of the constant. For example:
12f , -0.87f
Similarly, We can explicitly mention the type of a floating-point constant as
long double by appending
L at the end.
A Character constant is a single alphabet, digit or any special symbol enclosed using single quotes. Here are some examples:
'A', 'c', '4', '$', '^'
Note: Character constant must always be enclosed in single quotes so the following is wrong.
The maximum length of a character constant is 1 character long. That means you can not put more than one character inside single quotation marks like this:
'ab' // Wrong
'12' // Wrong
As you may already know inside computer memory everything is stored as binary. It is easy to see how integer and floating point constants are stored as binary numbers. But how character constants are stored in the memory? It turns out that Every character constant has a unique integer associated with it. An ASCII table represents the decimal number used to represent every known character in the English Language.
Consider the following statement:
char ch = 'a'; // declaring a variable ch and assigning 'a' to it
Here we are declaring a variable
ch of type
char and assigning a character constant
'a' to it (don’t worry too much about the syntax right now, the next chapter discusses variables in detail) . Although it might appear that we are assigning
'a' to variable
ch but actually what’s gets assigned to ch is the ASCII value of
'a' i.e 97. Here is the little secret C treats characters as small integers, so characters actually have
int type rather than the
char type. A
signed char has range -127 to 128 , while an
unsigned char has range 0-255 . Well, now you can answer the questions, we had asked you in the previous chapter.
Q1: It is obvious that
int can be
unsigned but how
char can have a sign ?
Ans: Because char type is actually an
int type since
int can be
unsigned that’s why
char type can be too
Q2: Why character type has integer range ?
char type is actually an
int type of small range.
String constants consist of zero or more characters enclosed in double quotes (
""). At the end of the string, the null character i.e
'\0' is automatically placed by the compiler.
Here are some examples of string constants:
"" // This is empty string it consists of only one character '\0' which is added by the compiler automatically.
Note: Although not officially part of Primary constants, string constants are given here for the sake of completeness. C has no data type for string, they are stored as an array of characters. We will learn more about strings in a great deal in its own chapter.
If we want to use constant several times in a program, then we can provide it a name. For example: If there is a need to use constant ? = 3.141592 at several places in the program, then we can give it a name and use that name instead of writing this long number. This constant is called Symbolic constant. It is generally defined at the beginning of the program. Here is the syntax of creating a Symbolic constant.
#define NAME VALUE
#define is a preprocessor directive just like
#include, that’s why it doesn’t end with the semicolon (
NAME indicates the name we want to give to our constant and is generally written in uppercase.
VALUE can be numeric, character or string constant.
Let’s create a symbolic constant called
#define PI 3.141592
When the program is compiled, the preprocessor replaces every occurrence of
PI by its value. So that the statement:
printf("Circumference of circle = %f", 2*PI*4);
printf("Circumference of circle = %f", 2*3.141592*4);
Use of symbolic constant makes the program more maintainable and readable. For example: let’s say we want more accurate results, so we decided to update the value of
3.14159265359, if we had not used symbolic constant then we would have to go through each occurrence of
3.141592 and update all of them. However, since we have already defined
PI in a
#define directive, we only need to make one change.