Your First C Progra3
Your First C Progra3
In the previous tutorial you learnt about C comments. Now, let's learn about variables, constants and
literals in C.
Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Variable
names are just the symbolic representation of a memory location. For example:
Here, age is a variable of int type and we have assigned an integer value 25 to it.
char ch = 'a';
// some code
ch = 'l';
Constants
If you want to define a variable whose value cannot be changed, you can use the const keyword. This
will create a constant. For example,
PI = 2.9; //Error
Literals
Literals are data used for representing fixed values. They can be used directly in the code. For
example: 1, 2.5, 'c' etc.
Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to these terms.
1. Integers
An integer is a numeric literal(associated with numbers) without any fractional or exponential part.
There are three types of integer literals in C programming:
C Variables, Constants and Literals
decimal (base 10)
octal (base 8)
For example:
Decimal: 0, -9, 22 etc
2. Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For
example:
-2.0
0.0000234
-0.22E-5
3. Characters
A character literal is created by enclosing a single character inside single quotation marks. For
example: 'a', 'm', 'F', '2', '}' etc.
4. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example:
5. Escape Sequences
C Variables, Constants and Literals
Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C
programming. For example: newline(enter), tab, question mark etc.
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? Question mark
\0 Null character
For example: \n is used for a newline. The backslash \ causes escape from the normal way the
characters are handled by the compiler.