0% found this document useful (0 votes)
30 views139 pages

UNIT I Full

This document outlines a course on C programming, detailing its structure, objectives, and evaluation scheme. It covers fundamental concepts such as algorithms, flowcharts, data types, and the history of the C programming language. Additionally, it discusses the components of computer systems and the functions of computers, emphasizing the importance of programming skills in placement interviews.

Uploaded by

abolimes4444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views139 pages

UNIT I Full

This document outlines a course on C programming, detailing its structure, objectives, and evaluation scheme. It covers fundamental concepts such as algorithms, flowcharts, data types, and the history of the C programming language. Additionally, it discusses the components of computer systems and the functions of computers, emphasizing the importance of programming skills in placement interviews.

Uploaded by

abolimes4444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 139

UNIT I

Introduction to program planning and c programming

Guided By
Prof. Supriya G.Jadhav
Teaching & Credit Examination Scheme

FPL Theory
l Credits: 2
l Mid Semester(TH): 30 Marks
l End Semester(TH): 70 Marks
l Total: 100 Marks

FPL Lab
l Credit: 1
l TermWork: 25 Marks
l Total: 25 Marks
Prerequisite Courses : Basics of Computers, Basic Mathematics

Companion Course : Fundamentals of Programming Languages Lab

l
Importance in placement interviews
Course Objectives
• To understand the fundamental Concepts of C Programming
• To acquire knowledge and Compare usage of Operators and Expressions in C
Programming
• To apply Control Flow structures in C Programming for Problem solving
• To design a solution using Arrays, Character and String Arrays in C
programming
• To design a develop solution for simple computational problems using User
Defined Functions in C Programming
• Justify the use of structures in Problem solving using C programming language
UNIT- I: Introduction to Program
Planning & C Programming
UNIT 1 to 5
UNIT- II: Operators and
Expressions

UNIT- III: Control Flow


UNIT- IV: Arrays

UNIT- V: User Defined Functions


Introduction to Program Planning & C
Programming

l
1.Program Design Tools: Art of Programming
through Algorithms, Flowcharts.
l
2.Overview of C:History and importance C,
Character Set, C Tokens, Keywords and
Identifiers.
l
3.Constants, Variables, Data types, Declaration
of variables, Storage Class, Assigning Values
to variables.
l
4.Defining Symbolic Constants, declaring a
Variable as Constant, Declaring a Variable as
Volatile.
Algorithm
l
Algorithm:
l
Def:
l
It is defined as a finite set of instructions that
describe a method for solving a problem. In other
words it is a step by step procedure for solving a
problem.
l
Systematic procedure that produces - in a finite
number of steps - the answer to a question or the
solution of a problem.
l
Sequence of instructions which can be used to
solve a given problem
Examples
l
Example 1: l
Write an algorithm to print all
l
Write an algorithm to check l
natural numbers up to n
l
whether he is eligible to
l
Step 1: Start
vote?
l
Step 2: get n value.
l
Step 1: Start
l
Step 3: initialize i=1
l
Step 4: if (i<=n) go to step 5
l
Step 2: Get age
else go
l
Step3: if age >= 18 print l
to step 7
l
“Eligible to vote” l
Step 5: Print i value and
l
Step 4: else print “Not increment i
l
eligible to vote” l
value by 1
l
Step 6: Stop l
Step 6: go to step 4
l
Step 7: Stop
Write an Algorithm
l
To find the average of three numbers taken as
input from the user.
l
The process of boiling an egg
l
The process of mailing a letter.
l
To divide one number by another & find the
quotient.
l
To find the maximum of two nos. Input by the
user.
l
l
ALGORITHM- POINTS TO
NOTE
l
1. The process consists of repeated application of
simple steps
l
2. All steps are unambiguous (clearly defined)
l
3. We are capable of doing all those steps
l
4. Only a limited no. of steps needs to be taken
l
5. Once all those steps are taken according to the
prescribed sequence, the required result will be found
l
6. Moreover, the process will stop at end point.
l
THREE REQUIREMENTS

l
1.Sequence is
l
a. accurate
l
b. Consists of a limited number of steps
l
2.Each step is:
l
a. Unambiguous
l
b. Executable
l
3.The sequence of steps terminates in the form
of a solution
l
Flowchart
l
Flowchart is a graphical representation of
alogorithm, in which graphic objects are used to
indicate the steps & decisions that are taken as
the process moves along from start to finish.
l
Individual steps are represented by boxes and
other shapes on the flowchart, with arrows
between those shapes indicating the order in
which the steps are taken.
Symbols of Flowcharts
Addition of two numbers
l
DRAW FLOWCHART

l
1. Multiplication of two Nos.
l
2. To display large no. From two nos.
l
3. Draw flowchart to calculate Area of circle.
l
Pseudo code
l
Pseudo code is the artificial and informal
language that is typically used for writing
algorithms.
l
SimilarIy to a programming language, but not as
rigid
l
is text based algorithmic design tool.
l
Pseudo code can be developed using different
computer operations.
Psudocode for Addition

1.Begin
2.WRITE “Please enter two numbers to add”
3.READ num1.
4.READ num2.
5.Sum = num1+num2.
6.WRITE Sum.
7.End
Psudocode for Area of circle

1.Begin
2.NUMBER r, area
3.INPUT r
4.area=3.14*r*r
5.OUTPUT area
6.End
Computer Concepts -
Introduction to Computer
BACKGROUND
In today's world, we use computers for all our tasks. Our
day-to-day activities: paying bills, buying groceries, using
social media, seeking entertainment, working from home,
communicating with a friend, etc., can all be done using a
computer.
So it is important not only to know how to use a computer,
but also to understand the components of a computer and
what they do.
What is a Computer?
A computer is an electronic device that accepts data from
the user, processes it, produces results, displays them to the
users, and stores the results for future usage.
Data is a collection of unorganized facts & figures and does
not provide any further information regarding patterns,
context, etc. Hence data means "unstructured facts and
figures".
Information is a structured data i.e. organized meaningful
and processed data. To process the data and convert into
information, a computer is used.
Computer Basics
What do you mean by Computer …?
It is an electronic device that is able to take raw input or “data” from
a user, store it, process it and output information as per requirement.

Significance: Computer is Intelligent machine


Has left great impact on our day to day life.
Responsible for reducing our efforts by automating many tasks and
saves our time also.

Eg: Online booking tickets (Railway, Plane and Bus), ATM Machine ,
update our bank passbook.
Evolution Of Computers
Two ERA’s
• Machine Era (Before 1965)
• Electronic Era (1945 –till Date)
• Generation of Computers:
First Generation (1945-1954)
Use of bulky electric tubes i.e Vaccum Tubes
Large Space
Slow Speed
Second Generation (1955-1964)
Use of Transistors
Reduced Size
Third Generation (1965-1974)
Use of IC’s
Low Cost
High Processing Speed

Forth Generation (1975-1989)


Use of VLSI
Use of Microprocessor

Fifth Generation (1990-Till Date) A.I , ROBOTICS


Characterized by very high speed
Implements parallel processing
Functions of Computers
A computer performs the following functions-
Receiving Input
Data is fed into computer through various input devices like keyboard,
mouse, digital pens, etc. Input can also be fed through devices like CD-
ROM, pen drive, scanner, etc.
Processing the information
Operations on the input data are carried out based on the instructions
provided in the programs.
Storing the information
After processing, the information gets stored in the primary or
secondary storage area.
Producing output
The processed information and other details are communicated to the
outside world through output devices like monitor, printer, etc.
Components of Computer
System
Computer systems consist of three
components as shown in below image:
Central Processing Unit
 Input devices
 Output devices
 Input devices provide data input to
processor, which processes data and
generates useful information that’s
displayed to the user through output
devices.
This is stored in computer’s memory.
Components of Computer
System
Computer systems consist of three
components as shown in below image:
Central Processing Unit
 Input devices
 Output devices
 Input devices provide data input to
processor, which processes data and
generates useful information that’s
displayed to the user through output
devices.
This is stored in computer’s memory.
Components of Computer System
Features of Computer
 Performs Information processing with amazing speed
Reliability

Accuracy

Ability to store huge amount of data and Information

Ability to communicate with Computers


Operation Performed By Computer

Computers perform four general operation:

a)Input

b)Process

c)Output

d)Storage
Input Devices:
Means to collect information from user and insert into the
computer.
Eg: Keyboard, Mouse, Joystick, Scanner etc.
Central Processing Unit:
CPU contains electronic circuits that cause processing to occur.
CPU interprets Instructions.
CPU performs logical and arithmetic operations.
CPU is brain of the computer.
CPU has two parts:
1)Control Unit:
It controls signals between memory and ALU.
1)Arithmetic and Logic Unit:
It performs arithmetic and logic operations.
Memory:
Memory is also called as Random Access Memory(RAM) i.e
Temporary Memory.
RAM is main memory of the computer.
RAM is electronic component that store data i.e numbers, letters,
alphabets, graphics and sound.

ROM is memory that is etched on the chip that startup directions


for the computer. ROM is the permanent memory.

Measurement of the Memory:


Measurement of the memory is done in the Kilobytes and
Megabytes.
Memory:
1 KB = 1000 memory locations

1 MB = Approx One Million memory locations

RAM= Random Access Memory

ROM= Read Only Memory

PROM= Programmable Read Only Memory

EPROM= Erasable Programmable Read Only Memory

EEPROM=Electrically Erasable Programmable Read


Only Memory
OUTPUT DEVICE :
The output device are 1)Printer 2)Computer Screen

STORAGE DEVICE :
The auxiliary storage device are 1) Floppy Disk 2)Hard Disk
3)CD-ROM’s 4)Pendrive
Central Processing Unit
The Central
Processing Unit (CPU)
is called "the brain of
computer" as it
controls operation of
all parts of computer.

It consists of two


components:
Arithmetic Logic Unit
(ALU), and Control
Unit.
CPU FLOW CHART
Arithmetic Logic Unit (ALU)

Data entered into computer is sent to RAM, from where it is


then sent to ALU, where rest of data processing takes place.

 All types of processing, such as comparisons, decision-


making and processing of non-numeric information takes
place here and once again data is moved to RAM.
Control Unit
As name indicates, this part of CPU extracts instructions,
performs execution, maintains and directs operations of
entire system.
Functions of Control Unit
Control unit performs following functions −
It controls all activities of computer
Supervises flow of data within CPU
Directs flow of data within CPU
Transfers data to Arithmetic and Logic Unit
Transfers results to memory
Fetches results from memory to output devices
Memory Unit

This is unit in which data and instructions given


to computer as well as results given by
computer are stored. Unit of memory is "Byte".
1 Byte = 8 Bits
Basic Memory unit
 RAM is a part of computer’s  ROM stands for read
Main Memory which is only memory that can
directly accessible by CPU. only read data stored in
 RAM is used to Read and
advance.
Write data into it which is  Its characteristic is that
accessed by CPU randomly.
 RAM is volatile in nature, it once the data is stored,
means if the power goes off, it can no longer be
the stored information is changed or deleted.
lost.  It is usually used in
 RAM is used to store the computer or other
data that is currently electronic devices, and
processed by the CPU. Most even if the power is
of the programs and data turned off, the data will
that are modifiable are not disappear.
stored in RAM.

RAM (Random Access Read Only Memory(ROM)


Memory)
What Is an Operating
System?
• An interface between the hardware and the
user.
• Provides an easy and efficient use of the
system resources.
• Example:
• Windows
• Linux
l
Ubantu
l
Android
l
Mac Os
l
etc.
Introduction to Program Planning & C
Programming

l
1.Program Design Tools: Art of Programming
through Algorithms, Flowcharts.
l
2.Overview of C:History and importance C,
Character Set, C Tokens, Keywords and
Identifiers.
l
3.Constants, Variables, Data types, Declaration
of variables, Storage Class, Assigning Values
to variables.
l
4.Defining Symbolic Constants, declaring a
Variable as Constant, Declaring a Variable as
Volatile.
History of C Programming
Language
C is a programming language which born at “AT & T’s Bell
Laboratory” of USA in 1972.

C was written by Dennis Ritchie, that’s why he is also called


as father of c programming language.

C language was created for a specific purpose i.e. designing


the UNIX operating system (which is currently base of many
UNIX based OS).
From the beginning, C was intended to be useful to allow
busy programmers to get things done because C is such a
powerful, dominant and supple language

Its use quickly spread beyond Bell Labs in the late 70’s
because of its long list of strong features
Let us see programming language
developed before C
Why Name “C” was given to
Language?
 Many of C’s principles and ideas were derived from
the earlier language B. (Ken Thompson was the
developer of B Language.)
 BCPL and CPL are the earlier ancestors of B
Language
 CPL is common Programming Language. In 1967,
BCPL Language ( Basic CPL ) was created as a
scaled down version of CPL
 As many of the features were derived from “B”
Language that’s why it was named as “C”. After 7-8
years C++ came into existence which was first
example of object oriented programming.
C programming language has
following importances:
 C is robust language and has rich set of built-
in functions, data types and
operators which can be used to write any
complex program.
 Program written in C are efficient due to
availability of several data types and operators.
 C has the capabilities of an assembly language
(low level features) with the feature of high
level language so it is well suited for writing
both system software and application
software.
 C is highly portable language i.e. code
written in one machine can be moved to other
which is very important and powerful feature.
 C supports low level features like bit level
programming and direct access to memory
using pointer which is very useful for
managing resource efficiently.
 C has high level constructs and it is more user
friendly as its syntaxes approaches to English
like language.
HIGH LEVEL LANGAUAGE
 With the help of high level language one can write
applications that are portable across various platforms
and is independent of any architecture.
 High level languages are easier to understand and is
user-friendly.
 High level languages has lots of abstractions and layers
of code before they reach the hardware itself.
 High level languages are extremely portable, they are
mostly used to write software’s which can run on
multiple platforms and architectures.
 High level is where all the creative things take place
and it can be debugged in very easier manner than a
low-level.
LOW LEVEL LANGUAGE
 Low Level languages, these languages are very close to
machine language, they are also known as assembly
language.
 Low Level languages are more appropriate for
developing new operating systems or writing firmware
codes for micro-controllers.
 Low level, machine code is nearer to the hardware,
which is actually fast to process and return the output.
 Low Level languages codes are very hard to understand,
and code written in assembly language is impossible to
run on other machine or architecture.
 low level is challenging and requires a great deal of
experience and knowledge
CONCEPT OF HARDWARE AND
SOFTWARE
Hardware:
physical component of a computer is called as
hardware. The hardware may be an electronic,
electrical, magnetic or mechanical components

Ex: RAM, Floppy Disk, Hard Disk, Key Board,


Printer etc.
Software:
Software is the set of computer programs
which is used for some particular purpose. This
may be some web applications like WhatsApp,
Twitter, Flipkart or desktop applications like- MS
Word, Ms excel etc
Types of Software

Basically there are two types of software:

1)System Software

2) Application software
System Software:
The System Software is basically used by the
computer system to control all the actions and
operations of the system.

System software is software designed to provide


a platform for other software. Examples of
system software include operating systems (OS)
(like macOS, Linux, Android, and Microsoft
Windows).[1]
1.OperatingSystem
An operating system (OS) is system software
that manages computer hardware and software
resources and provides common services for
computer programs
2. Programming Software:
Programming software is basically given for
development of software or computer program.
This computer programs can be written in
different languages like- C, C++, Java, .Net etc.
This software are used for different purpose or
different work. Ex-Flipkart, WhatsApp, MS Word,
MS Excel etc.
Examples of programming software are-
Assembler, Compiler, interpreter etc.
3. Utility Software:

Utility software is system software designed to


help analyse, configure, optimize or maintain a
computer.

Ex: antivirus is a best example for utility


software.
Application Software :

An Application software is a program or set of


programs that is designed for end-user. Each and
every applications software is designed to
perform certain work.

Ex: Facebook- is a web application which is


designed for chatting, Flipkart-For Shopping,
WhatsApp for messaging etc
Application software can be
classified into two category:
1. Readymade Software: These are the software which
are developed not for specific user but it can be
used by any user.
Ex: Tally, Ms Word.

2. User-Written Software: These are the software


which are developed by end user for specific need. It
may be the websites or some applications.
WhatsApp is user written software which is basically
for messaging purpose. The requirement varies user
to user so the development is also differ.
COMPUTER LANGUAGE
 Languages plays a vital role in communications.
Means when two or more device is in
communication they should must understand
one another. For this communication purpose
computer follows some languages. This
languages should be understand by both user
and computer systems.
The Computer languages are broadly classified
as:
1) Machine Language

2) Assembly Language

3) High Level Language


Execution of Code

-A compiler takes our source code and generates the


corresponding assembly code.
-An assembler converts the assembly code to the machine
code.
-A linker merges all the machine-code modules referenced
in our code.
-A loader moves the executable to RAM and lets it be
executed by a CPU.
Character Set in C
 The character set consist of upper and
lowercase alphabet, digit, special character and
white space and alphanumeric character.

1.Alphabets
 C language supports all the alphabets from the

English language.
 Lower and upper case letters together support

52 alphabets.
 lower case letters - a to z
 UPPER CASE LETTERS - A to Z
2. Digits
C language supports 10 digits which are used to
construct numerical values in C language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

3.Special Symbols
C language supports a rich set of special symbols that
include symbols :
to perform mathematical operations,
to check conditions, white spaces, backspaces, and other
special symbols.
Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : '
" / ? . > , < \ | tab, newline space, NULL bell
backspace ,verticaltab etc.
Tokens in C

 Tokens in C is the most important element to be used in


creating a program in C.

 We can define the token as the smallest individual element in
C. For `example, we cannot create a sentence without using
words; similarly, we cannot create a program in C without
using tokens in C.

 Therefore, we can say that tokens in C is the building block or
the basic component for creating a program in C language.
C Tokens:
• In a passage of text,
individual words and
punctuation marks are
called tokens.

• Similarly, in a C program the


smallest individual units are
known as C tokens.

• C has six types of tokens.

• C programs are written


using these tokens and the
syntax of the language.
Classification of tokens in C
 Keywords in C

 Identifiers in C

 Strings in C

 Operators in C

 Constant in C

 Special Characters in C
Keywords in C
1.All keywords have fixed meanings & these
meaning cannot be changed.
2.Keywords are the pre-defined words used by
the compiler.

3.Basiclly there are 32 keywords.

Note:C99 adds some more keywords.


1. Keywords in C
Identifiers in C
Rules of Identifiers
 The first character of an identifier should be either an
alphabet or an underscore.
 It should not begin with any numerical digit.
 In identifiers, both uppercase and lowercase letters are

distinct.
 Therefore, we can say that identifiers are case sensitive.
 Commas or blank spaces cannot be specified within an

identifier.
 Keywords cannot be represented as an identifier.
 The length of the identifiers should not be more than 31

characters.
Operators in C
Operators in C is a special symbol used to
perform the functions. The data items on which
the operators are applied are known as operands.
Operators are applied between the operands.
Depending on the number of operands, operators
are classified as follows:

1. Unary Operator
2. Binary Operator
Unary Operator

A unary operator is an operator applied to the


single operand. For example: increment operator
(++), decrement operator (--), sizeof, (type)*.
Binary Operator
The Binary operator is an operator applied between two
operands. The followibng is the list of the binary operators:

 Arithmetic Operators

 Relational Operators

 Shift Operators

 Logical Operators

 Bitwise Operators

 Conditional Operators

 Assignment Operator
Constants in C

 A constant is a value assigned to the variable


which will remain the same throughout the
program, i.e., the constant value cannot be
changed.

There are two ways of declaring constant:

1.Using const keyword


2.Using #define pre-processor
Types of constants in C
Variables

A variable is a data name that may be used to store a data value. Unlike constants that remain

unchanged during the execution of a program, a variable may take different values at different times

during execution.

A variable name can be chosen by the programmer in a meaningful way so as to reflect its function

or nature in the program.

e.g.

Average

height

Total

Counter_1

class_strength
Variables in C
 A variable in C language is the name associated with
some memory location to store data of different types.
 There are many types of variables in C depending on
the scope, storage class, lifetime, type of data they
store, etc.
 A variable is the basic building block of a C program
that can be used in expressions as a substitute in place
of the value it stores.
What is a variable in C?

 A variable in C is a memory location with some name that helps


store some form of data and retrieves it when required.
 We can store different types of data in the variable and reuse the
same variable for storing some other data any number of times.
Rules for valid Variable Name :

l
They must begin with a letter. Some systems permit underscore as the first character.

l
ANSI standard recognizes a length of 31 characters. (In C99, at least 63 characters are

significant.)

l
Uppercase and lowercase are significant. That is, the variable Total is not the same as total

or TOTAL.

l
It should not be a keyword.

l
White space is not allowed.
C Variable Syntax
Here,
 data_type: Type of data that a variable can

store.
 variable_name: Name of the variable given by

the user.
 value: value assigned to the variable by the

user.
Example
int var; // integer variable

char a; // character variable

float fff; // float variables


There are 3 aspects of defining
a variable:
 Variable Declaration

 Variable Definition

 Variable Initialization
1. C Variable Declaration

 Variable declaration in C tells the compiler


about the existence of the variable with the
given name and data type.
 When the variable is declared, an entry in
symbol table is created and memory will be
allocated at the time of initialization of the
variable.
2. C Variable Definition

 In the definition of a C variable, the compiler


allocates some memory and some value to it.
 A defined variable will contain some random
garbage value till it is not initialized.

 Example:
 int var;
 char var2;
3. C Variable Initialization

 Initialization of a variable is the process where


the user assigns some meaningful value to the
variable.
Example
How to use variables in C?
Rules for Naming Variables in C

 A variable name must only contain alphabets,


digits, and underscore.
 A variable name must start with an alphabet or
an underscore only. It cannot start with a digit.
 No whitespace is allowed within the variable
name.
 A variable name must not be any reserved word
or keyword.
C Variable Types

 Local Variables
 Global Variables
 Static Variables
 Automatic Variables
 Extern Variables
1. Local Variables in C

 A Local variable in C is a variable that is


declared inside a function or a block of code. Its
scope is limited to the block or function in which
it is declared.
Example of Local Variable in C
2. Global Variables in C

 A Global variable in C is a variable that is


declared outside the function or a block of code.
Its scope is the whole program i.e. we can
access the global variable anywhere in the C
program after it is declared.
Example of Global Variable in
C
3. Static Variables in C

They are initialized only once and preserve the


values till the end of the program,
The static keyword is used to declare the static
variables.
Syntax of Static Variable in C

static data_type variable_name = initial_value;


Example of Static Variable in
C
4. Automatic Variable in C

 All the local variable


are automatic variables by default. They are
also known as auto variables.
 Their scope is local and their lifetime is till the
end of the block. If we need, we can use
the auto keyword to define the auto variables.
 The default value of the auto variables is a
garbage value.
Syntax of Auto Variable in C
Example of auto Variable in C
OUTPUT
5. External Variables in C

 External variables in C can


be shared between multiple C files. We can
declare an external variable
using the extern keyword.
 Their scope is global and they exist between
multiple C files.

Syntax of Extern Variables in C

extern data_type variable_name;


Example of Extern Variable in
C
Constant Variable in C

 Till now we have only seen the variables whose


values can be modified any number of times.
But C language also provides us a way to make
the value of a variable immutable. We can do
that by defining the variable as constant.
 A constant variable in C is a read-only
variable whose value cannot be modified once it
is defined. We can declare a constant variable
using the const keyword.
Syntax of Const Variable in C

const data_type variable_name = value;


Example of Const Variable in
C
Data Types in C
 Each variable in C has an associated data type.

 It specifies the type of data that the variable can store like integer,
character, floating, double, etc.

 Each data type requires different amounts of memory and has some
specific operations which can be performed over it.

 The data type is a collection of data with values having fixed values,
meaning as well as its characteristics.
Data Types :

In C programming, data types define the type and size of data that variables can

store.

They specify how much space a variable occupies in memory and the operations

that can be performed on that data.

C supports various data types, which can be categorized as:

primary (or fundamental) data types

derived data types

user-defined data types.


Primary(or fundamental) Data types :

Primary data types are the basic data types provided by C. They

directly represent values stored in memory.

Examples :

int: Represents integer values (whole numbers).

float: Represents floating-point numbers (decimal numbers).

double: Represents double-precision floating-point numbers.

char: Represents a single character.


Derived Data types :

Derived data types are derived from the primary data types. They are built upon basic

data types and provide more complex data structures.

Examples :

Arrays: A collection of elements of the same data type.

Pointers: A variable that holds the memory address of another variable.

Functions: A block of code that performs a specific task.

Structures: A collection of variables of different data types under a single name.


User-Defined Data types :

User-defined data types allow the programmer to define a type that is

not predefined in C. They help to create more complex data structures.

Examples :

struct (Structure): A collection of variables of different data types

under a single name (explained in derived types).

union: Similar to a structure but stores only one of its members at a

time.

enum (Enumeration): A user-defined data type that consists of integral

constants.
1.Integer Data Type

The integer datatype in C is used to store the


integer numbers(any number including positive,
negative and zero without decimal part). Octal
values, hexadecimal values, and decimal values
can be stored in int data type in C.
Range: -2,147,483,648 to 2,147,483,647
Size: 2 byte/4 bytes
Format Specifier: %d
Syntax:

int var_name;

Example:

Int a=5;
2.Character Data Type

Character data type allows its variable to store


only a single character. The size of the character
is 1 byte. It is the most basic data type in C. It
stores a single character and requires a single
byte of memory in almost all compilers.
Range: (-128 to 127) or (0 to 255)
Size: 1 byte
Format Specifier: %c
Syntax :

char var_name;

Example:

char a=‘c’;
3. Float Data Type

In C programming float data type is used to


store floating-point values. Float in C is used to
store decimal and exponential values. It is used
to store decimal numbers (numbers with floating
point values) with single precision.
Range: 1.2E-38 to 3.4E+38
Size: 4 bytes
Format Specifier: %f
Syntax

float var_name;

Example:

float a=2.5
4. Double Data Type

A Double data type in C is used to store decimal numbers (numbers


with floating point values) with double precision.
It is used to define numeric values which hold numbers with decimal
values in C.
The double data type is basically a precision sort of data type that is
capable of holding 64 bits of decimal numbers or floating points.
Since double has more precision as compared to that float then it is
much more obvious that it occupies twice the memory occupied by
the floating-point type.
It can easily accommodate about 16 to 17 digits after or before a
decimal point.

Range: 1.7E-308 to 1.7E+308


Size: 8 bytes
Format Specifier: %lf
Syntax

double var_name;

Example

double b = 12.293123;
5.Void Data Type
The void data type in C is used to specify that no value is
present.

It does not provide a result value to its caller. It has no values


and no operations.

It is used to represent nothing. Void is used in multiple ways


as function return type, function arguments as void,
and pointers to void.
Syntax:

void function_name;

Example:

void fun1;
Declaration of variables :

After designing suitable variable names, we must declare them to the

compiler.

Declaration does two things:

l
It tells the compiler what the variable name is.

l
It specifies what type of data the variable will hold.

Declaration of variables must be done at the beginning of a statement

block before they are used in a program.

e.g. Datatype v1,v2,…..vn


Declaration of Variable

main() /*.........Program Name........................ */


{
/*................Declaration.......................*/
Float x, y;
Int Code;
short int count;
long int amount;
Double deviation;
Unsigned n;
Char c;
/*...............Computation....................... */
....
....
....
} /*.............Program ends........................*/
Storage Class
 C Storage Classes are used to describe the features of a
variable/function.

 These features basically include the scope, visibility, and lifetime


which help us to trace the existence of a particular variable during
the runtime of a program.

 Storage class provide information about variables location and


visibility.
C language uses 4 storage
classes, namely:
Declaration of Storage Class:

# Variables in C can not only data type but also storage class that provides information
about their location and visibility.
#The storage class decides the portion of the program within which the variables are
recognized.
/* Example of storage classes */
Int m; #Global Variable or External variable
Main()
{
int i; #Local variables
float balance; #Local variables
….
….
function1();
}
function1()
{
int i; #Local variables
float sum; #Local variables
Defining Symbolic Constants:

When often use certain unique constants in a program. These constant my


appear repeatedly in a number of places in the program.
One example of such constant is 3.14
Representing the value of the mathematical constant "pi".
Problems:
Modifiability : Problems in modification of the program
Understandability : Problem in understanding the program.
Declaring a variable as constant:

We may like the value of certain variable to remain constant


during the execution of a program.
We can achieve this by declaring the variable with the qualifier
const at the time of initialization.
Example :

Const int class_size = 40;


Declaring Variable as Volatile
 In C, the volatile keyword is used to indicate to the
compiler that a variable's value may change
unexpectedly, so it should not rely on the value being
cached in a register or optimized away.

 When a variable is declared as volatile, the compiler


must generate code to read and write the variable's
value from memory each time it is used, rather than
relying on a cached value in a register. It is because the
value of a volatile variable may be changed by external
factors, such as hardware or other threads running
concurrently.
Declaring a variable as Volatile:

ANSI standard define another qualifier volatile that could be


used to tell explicitly the compiler that a variable's value may
be changed at any time by some external sources(from outside
the program)

Example :

volatile int date;


Case Study

Study of “C” Program compilation Process,testing


& debugging.

You might also like