Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

|| JAI SRI GURUDEV ||

SRI ADICHUNCHANAGIRI SHIKSHANA TRUST ®

ADICHUNCHANAGIRI INSTITUTE OF TECHNOLOGY


(An ISO 9001 : 2008 Certified)
Affiliated to Visvesvaraya Technological University, Belagavi ,
Approved by AICTE and Accredited by NAAC, New Delhi
Jyothinagar, Chikkamagaluru – 577 102.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ONLINE CLASS USING ZOOM APPLICATION

Subject: ‘C’ Programming for Problem Solving


Subject Code: 18CPS23

Presented By: Semester and Section : 2nd and ‘B’


Mr. GOPINATH C. B., B.E., M.Tech., Class # : 1
Assistant
7 April 2020 Professor Date : 02-04-2020 (11:00AM To 12:00PM)
User-Defined Data Type

 typedef data type

 enum data type


enum (enumeration) Data Type

 It is a user-defined data type.

 It is denoted by keyword ‘enum’.

 enum data type consists of named integer constants as

a list

 It starts with ‘0’ by default value and incremented by

‘1’ in a sequential.
enum (enumeration) Data Type (CONTD….)

Syntax:
enum identifier{value1,value2,….,valuen};
Where,
 enum is a keyword.
 identifier is a user-defined enumerated data type
which can be used to declare variables that can be have
one of the values enclosed within the braces (known as
enumeration constants).
 value1,value2,….,valuen represents enumerated
variables.
enum (enumeration) Data Type (CONTD….)

Example 1:
enum month{jan,feb,mar,apr,may,jun,jul,aug,
sep,oct,nov,dec};
Here,
jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov
and dec variables assignd to 0,1,2,3,4,5,6,
7,8,9,10 and 11
Means
jan=0,feb=1,mar=2,apr=3,may=4,jun=5,jul=6,
aug=7,sep=8,oct=9,nov=10 and dec=11.
enum (enumeration) Data Type (CONTD….)

Example 2:

enum day{Mon,Tue,Wed,Thu,Fri,Sat,Sun};

Here, Mon, Tue, Wed, Thu, Fri, Sat and Sun

variables assignd to 0, 1, 2, 3, 4, 5 and 6.

Means

Mon=0, Tue=1, Wed=2, Thu=3, Fri=4, Sat=5 and

Sun=6.
enum (enumeration) Data Type (CONTD….)

Example 3:

enum day{Mon=1,Tue,Wed,Thu,Fri,Sat,Sun};

Here, Mon=1, Tue, Wed, Thu, Fri, Sat and Sun

variables assignd to 2, 3, 4, 5, 6 and 7.

Means

Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6 and

Sun=7.
enum (enumeration) Data Type (CONTD….)

Example 4:

enum day{Mon=20,Tue,Wed,Thu,Fri,Sat,Sun};

Here, Mon=20, Tue, Wed, Thu, Fri, Sat and Sun

variables assignd to 21, 22, 23, 24, 25 and

26.

Means

Mon=20, Tue=21, Wed=22, Thu=23, Fri=24,

Sat=25 and Sun=26.


enum (enumeration) Data Type (CONTD….)
Program Example 1:
#include<stdio.h>
void main()
{
enum day{mon,tue,wed,thu,fri,sat,sun};
enum day week_st, week_end;
week_st = mon;
week_end = sun;
if(week_st == tue)
{
week_end = sat;
}
printf(“WEEK START = %d\n”,week_st);
printf(“WEEK END = %d\n”,week_end);
}
enum (enumeration) Data Type (CONTD….)

Output:

WEEK START = 0

WEEK END = 6
enum (enumeration) Data Type (CONTD….)
Program Example 2:
#include<stdio.h>
void main()
{
enum day{mon=1,tue,wed,thu,fri,sat,sun};
enum day week_st, week_end;
week_st = mon;
week_end = sun;
if(week_st == tue)
{
week_end = sat;
}
printf(“WEEK START = %d\n”,week_st);
printf(“WEEK END = %d\n”,week_end);
}
enum (enumeration) Data Type (CONTD….)

Output:

WEEK START = 1

WEEK END = 7
enum (enumeration) Data Type (CONTD….)

Program Example 3:
#include<stdio.h>
void main()
{
enum MONTH{jan,feb,mar};
enum day month;
month = mar;
if(month == 0)
{ printf(“Month is January\n”); }
if(month == 1)
{ printf(“Month is February\n”); }
if(month == 2)
{ printf(“Month is March\n”); }
}
enum (enumeration) Data Type (CONTD….)

Output:

Month is March
enum (enumeration) Data Type (CONTD….)

The definition and declaration of enumerated

variables can be combined in one statement.

Example:
enum day{mon,tue,wed,thu,fri,sat,sun}week_st,week_end;

Instead of

enum day{mon,tue,wed,thu,fri,sat,sun};

enum day week_st, week_end;


Defining Symbolic Constants

 It is used to define symbolic constants.

 #define preprocessor directive can be used

to define symbolic constants.

 A symbolic constant is defined as follows

Syntax:

#define symbolic_name value_of_constant


Defining Symbolic Constants (CONTD….)

Valid Examples of Constant Definitions are

 #define STRENGTH 100

 #define PASS_MARK 50

 #define MAX 200

 #define PI 3.14159
Defining Symbolic Constants (CONTD….)

Examples of Invalid #define Statements are

Statement Validity Remark/Description

#define X = 2.5 Invalid „=„ sign is not allowed

No white space between # and


# define MAX 10 Invalid
define

#define N 25; Invalid No semicolon at the end

A statement can define only one


#define N 5,M 10 Invalid
name

define should be in lowercase


#Define ARRAY 11 Invalid
letters

$ symbol is not permitted in


#define PRICE$ 100 Invalid
name
Data Type Qualifier

American National Standard Institute (ANSI) introduced


two types of data type qualifiers, namely:
 const data type qualifier
 volatile data type qualifier
const data type qualifier:
The value of variable is constant during execution of
program, then such variables are declared using keyword
‘const’.
Example:
 const float pi = 3.142;
 const int size = 90;
Data Type Qualifier (CONTD….)
volatile data type qualifier:
The value of variable might change at any time by an some
external sources (from outside the program), then such
variables are declared using keyword ‘volatile’.
Example:
volatile int date;
The value of date may be altered by some external
factors even if it does not appear on the left-hand side of
an assignment statement. When we declare a variable as
volatile, the compiler will examine the value of the
variable each time it is encountered to see whether any
external alteration has changed the value.
Data Type Modifier

There are mainly FOUR data type modifiers namely:

 signed

 unsigned

 long

 short
Data Type Modifier (CONTD….)

signed data type modifier:


Indicates that value stored in a variable is capable of
storing both positive and negative values.
The values in range of -2n-1 to 2n-1 -1, where n is the
size (in bits) of that particular data type.
Example:

signed int a;
Here, variable ‘a’ can store both positive and negative
values.
Data Type Modifier (CONTD….)

unsigned data type modifier:


Indicates that value stored in a variable is capable of
storing only positive values.
The values in range of 0 to 2n -1, where n is the size (in
bits) of that particular data type.
Example:

unsigned int a;
Here, variable ‘a’ can store only positive values.

You might also like