Professional Documents
Culture Documents
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C
(Common to ALL Departments)
UNIT No. II
BASICS OF C PROGRAMMING
2.3 Constants-Enumeration
V i 1X
Introduction of Constants:
Constants are identifiers whose values do not change. While Values of variables can be changed
by any time, values of constants are never changed. constants are used to define fixed values
like pi or the charge on an electron so that their value does not change in the program made
even by mistake.
A Constant is an explicit data value specified by the programmer. The value of the constant is
known to the compiler at the compile time.
Constants are also called literals.Constants can be any of the data types.
It is considered best practice to define constants using only upper-case names.
Integer constants
An integer constant can be a decimal integer or octal integer or hexadecimal integer. A
decimal integer value is specified as direct integer value whereas octal integer value is prefixed
with 'o' and hexadecimal value is prefixed with 'OX'.
An integer constant can also be an unsigned type of integer constant or long type of
integer constant. Unsigned integer constant value is suffixed with 'u' and long integer constant
value is suffixed with 'l' whereas unsigned long integer constant value is suffixed with 'ul'.
Example
1. Fractional forms
2. Exponential form or Scientific notation
Here are the rules for creating floating point constants in Fractional form:
Exponential form is used in cases when a number is too small or too large. For example,
0.00000941 can be represented as 9.41e-6. The part of the number before e is called mantissa i.e
9.41, whereas, the part following e is called the exponent i.e -6.
Here are the rules for creating floating point constants in 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 or 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 or L at the end.
12.13l, -98.12L
Example Problem Solving and Programming In C (Co
The floating-point value 3.14 is represented as 3E-14 in exponent form.
Character Constants
A character constant is a symbol enclosed in single quotation. A character constant has a
maximum length of one character.
Example
'A'
'2'
'+'
\” ProblemaSolving
Inserts and Programming
double quote In C (Co
String Constants
A string constant is a collection of characters, digits, special symbols and escape
sequences that are enclosed in double quotations. At the end of the string, the null character i.e
'\0' is automatically placed by the compiler.
" This\
is\
btechsmartclass "
We can also define string constant by separating it with white space as follows...
Creating constants in C
In a c programming language, constants can be created using two concepts...
The general syntax for creating constant using the 'const' keyword is as follows...
const int x = 10 ;
Example Program
#include<stdio.h>
#include<conio.h>
void main()
int i = 9 ;
Problem Solving and Programming In C (Co
const int x = 10 ;
i = 15 ;
}The above program gives an error because we are trying to change the constant variable value
(x = 100).
#define PI 3.14
Here, PI is a constant with value 3.14
Example Program
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main(){
int r, area ;
scanf("%d", &r) ;
We use the keyword enum to create enumerated data types. The general syntax of enum is
as follows…
In the above syntax, integral constant '0' is assigned to name1, integral constant '1' is assigned to
name2 and so on. We can also assign our own integral constants as follows...
enum {name1 = 10, name2 = 30, name3 = 15, ... }
In the above syntax, integral constant '10' is assigned to name1, integral constant '30' is assigned
to name2 and so on.
#include<conio.h>
void main()
I today = tuesday ;
In the above example program a user defined datatype "day is created seven values, monday as
integral constant '0', tuesday as integral constant '1', wednesday as integral constant '2', thursday
as integral constant '3', friday as integral constant '4', saturday as integral constant '5' and sunday
as integral constant '6'. Here, when we display tuesday it displays the respective integral constant
'1'.
#include<conio.h>
void main()
In the above example program, the integral constant value starts with '1' instead of '0'. Here,
tuesday value is displayed as '2'.
We can also create enum with our own integral constants, consider the following example
program