You are on page 1of 10

I I

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.

In C programming language, a constant can be of any data Problem


type likeSolving
integer,and Programming In C (Co
floating-point,
character, string and double, etc.,

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

125 -----> Decimal Integer Constant

O76 -----> Octal Integer Constant

OX3A -----> HexaDecimal Integer Constant

50u -----> Unsigned Integer Constant

30l -----> Long Integer Constant


100ul -----> Unsigned Long Integer Constant

Floating Point constants


A floating-point constant must contain both integer and decimal parts. Sometimes it may
also contain the exponent part. When a floating-point constant is represented in exponent form,
the value must be suffixed with 'e' or 'E'.

1. Fractional forms
2. Exponential form or Scientific notation

Here are the rules for creating floating point constants in Fractional form:

Problem Solving and Programming In C (Co


1. Must have one at least one digit
2. Must have a decimal point
3. Can be positive or negative, the default is positive
4. No comma, blanks, or any other symbols are allowed

Here are some examples:

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:

1. Mantissa and exponent must be separated by e or E.


2. Mantissa can be positive or negative, default is positive.
3. Exponent must have at least one digit.
4. The exponent can be positive or negative default is positive
Some examples of floating point numbers in exponential form are:

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'

'+'

Escape Sequence Description

\t Inserts a tab in the text at this


point

\b Inserts a backspace in the


text at this point
\n Inserts a newline in the text
at this point

\r Inserts a carriage return in


the text at this point

\f Inserts a form feed in the text


at this point

\’ Inserts a single quote


character in the text at this
point

\” ProblemaSolving
Inserts and Programming
double quote In C (Co

character in the text at this


point

\\ Inserts a backslash character


in the text at this point.

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.

We define string constants in a single line as follows...


"This is btechsmartclass"

We can define string constant using multiple lines as follows...

" This\

is\

btechsmartclass "

We can also define string constant by separating it with white space as follows...

"This" "is" "btechsmartclass"


Problem Solving and Programming In C (Co

All the above three define the same string constant.

Creating constants in C
In a c programming language, constants can be created using two concepts...

1. Using the 'const' keyword


2. Using '#define' preprocessor

Using the 'const' keyword


We create a constant of any datatype using the 'const' keyword. To create a constant, we
prefix the variable declaration with the 'const' keyword.

The general syntax for creating constant using the 'const' keyword is as follows...

const datatype constantName ;

const datatype constantName = value ;


Example

const int x = 10 ;

Here, 'x' is a integer constant with fixed value 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 ;

x = 100 ; // creates an error

printf("i = %d\nx = %d", i, x ) ;

}The above program gives an error because we are trying to change the constant variable value
(x = 100).

Using '#define' preprocessor


We can also create constants using '#define' preprocessor directive. When we create a
constant using this preprocessor directive it must be defined at the beginning of the program
(because all the preprocessor directives must be written before the global declaration).

We use the following syntax to create constant using '#define' preprocessor


directive...

#define CONSTANTNAME value

#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 ;

printf("Please enter the radius of circle : ") ;

scanf("%d", &r) ;

area = PI * (r * r) ; Problem Solving and Programming In C (Co

printf("Area of the circle = %d", area) ;

Enumerated Types (enum) in C


In C programming language, an enumeration is used to create user-defined datatypes.
Using enumeration, integral constants are assigned with names and we use these names in the
program. Using names in programming makes it more readable and easy to maintain.

We use the keyword enum to create enumerated data types. The general syntax of enum is
as follows…

enum {name1, name2, name3, ... }

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.

Example Program for enum with default values


#include<stdio.h>

#include<conio.h>

enum day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} ;

void main()

enum day today; Problem Solving and Programming In C (Co

I today = tuesday ;

printf("\ntoday = %d ", today) ;

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'.

Example Program for enum with changed integral constant values


#include<stdio.h>

#include<conio.h>

enum day { Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} ;

void main()

enum day today;


today = tuesday ;

printf("\ntoday = %d ", today) ;


}

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

Problem Solving and Programming In C (Co

You might also like