0% found this document useful (0 votes)
15 views80 pages

Unit-1: CO-1: Prepare Flowchart and Algorithm For Real Life Problem

The document provides an introduction to C programming focusing on flowcharts and algorithms. It outlines the symbols used in flowcharts, their purposes, and compares flowcharts with algorithms. Additionally, it includes examples of flowcharts and algorithms for various problems, highlighting their advantages and limitations.

Uploaded by

pyagna6969
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)
15 views80 pages

Unit-1: CO-1: Prepare Flowchart and Algorithm For Real Life Problem

The document provides an introduction to C programming focusing on flowcharts and algorithms. It outlines the symbols used in flowcharts, their purposes, and compares flowcharts with algorithms. Additionally, it includes examples of flowcharts and algorithms for various problems, highlighting their advantages and limitations.

Uploaded by

pyagna6969
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/ 80

C Programming - I (CP – I)

DU # 2304CS101

Unit-1
Introduction
CO-1: prepare flowchart and
algorithm for real life problem

Prof. Ashok G. Badresiya


Department of Computer Application
Darshan Institute of Computer Application
[email protected]
9913131691
 Outline
Looping
 Flowchart and It’s symbols
 Algorithm
 Examples of Algorithm and Flowchart
 History and Importance of C
 Header Files
 C Tokens
 Keywords and Identifiers
 Datatypes
 Type Casting
 Constant Variable
 Structure of C Program
Algorithm and
Flowchart
Section 1
Flowchart
 Flowchart is a pictorial or graphical representation of a process.
 Each step in the process is represented by a different symbol and contains
a short description of the process.
 The flowchart symbols are linked together with arrows showing the
process flow direction.
 This pictorial representation can give step-by-step solution of the given
problem.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 4


Symbols of Flowchart
 Flowchart use special shapes/symbols to represent different types of
actions or steps in process.

Parallelog
Oval Rectangle
ram

Arro
Diamo Connect
ws
nd or

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 5


Symbols of Flowchart (Conti..)

1 Oval

 An oval represents start or end in the flowchart.


 When we want to represent STARTING POINT or ENDING POINT of the
flowchart at that time we can use this symbol and we have to write START
or STOP respectively.
 In every flowchart, Oval symbol must be there to indicate start and end.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 6


Symbols of Flowchart (Conti..)

2 Parallelog
ram
 Parallelogram represents input or output in the flowchart.
 When we want to take any value from user during the process; to indicate
that step we will use parallelogram. It will be known as INPUT.
 When we want to show any value during the process; to indicate that step
we will use parallelogram. It will be known as OUTPUT.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 7


Symbols of Flowchart (Conti..)

3 Rectang
le
 Rectangle represents process for the flowchart.
 When we want show any step/calculation/logic in the process at that time
we will use this rectangle in the flowchart.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 8


Symbols of Flowchart (Conti..)

4 Diamond

 Diamond represent condition/decision-making in the flowchart. When


there are two paths, and user has to select one path either YES or NO, at
that time we can use this diamond shape.
 In short, when we want to put a condition in a flowchart, where we have to
select either option-1 or option-2 at that time we can use a diamond
shape.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 9


Symbols of Flowchart (Conti..)

5 Connector

 This symbol indicates that the flow continues where a matching symbol
has been placed.
 When we are drawing a flowchart and reach the bottom of the page or
need to jump to another page, draw a connector symbol and connect it to
the last item on the flowchart.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 10


Symbols of Flowchart (Conti..)

6 Arrow

 When we want to join/connect two symbols at that time we can use arrow
symbol.
 When we want show the flow direction in flowchart at that time we can
also use respective arrow symbol.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 11


Symbols of Flowchart
Symbol Symbol Name Purpose
Start/Stop Used at the beginning and end of the
algorithm to show start and end of the
Oval program.

Process Indicates processes like mathematical


Rectangle operations / step / calculation / logic.

Input/ Output Used for denoting program inputs and


Parallelog outputs.
ram

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 12


Symbols of Flowchart
Symbol Symbol Name Purpose
Decision Stands for condition / decision-making in a
flowchart, where answer is usually Yes or
Diamo No.
nd

Arrows To show the flow direction.


Arrows

Connector Used to show flow across multiple charts


or pages
Connec
tor

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 13


Flowchart Example-Find out Area of Circle
Start
It indicates staring point of the
flowchart
Initialize PI 
3.14, area  0
It indicates initialization
process
Read Step Start
value of 1:
Step Initialize PI  3.14, area 
r It indicates reading input value 2:
process Step Read value of r
3:
Step Calculate area  PI * r * r
Calculate
area  PI * r * r 4:
It indicates calculation process Step Print area
5:
Step Stop
6:
Print area It indicates output process

It indicates ending point of the


Stop
flowchart

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 14


Advantages of Flowchart
 Easy to draw.
 Easy to understand logic.
 Easy to identify mistakes by non-technical person.
 Easy to show branching and looping.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 15


Limitation of Flowchart
 Time consuming.
 Difficult to modify.
 Very difficult to draw flowchart for big or complex problems.
 Have to use special symbol for each and every task.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 16


Need of Flowchart and Algorithm
 Programming is nothing but describing the steps needed to be executed
by the computer to solve any problem or task.
 For example;
 We all had seen food recipes to prepare any food.
 Same like in our Computer Programming the word recipes will be replaced by the
Procedure.
 The ingredients are called Input/s.
 Computer will follow the procedure with the given input/s data and what we final
achieved that is known as Output.
 Helps Developers to plan, design and visualize the structure and flow of
program
 Helps to represent the logical flow of a program
 Developers can identify the issues if there are any before writing the code
 Helps to provide clear and structured approach
 Due to it, programmers can easily write, debug and maintain the code
Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 17
Algorithm
 An algorithm is a step by step procedure for solving a problem in a finite
number of steps.
 It is written in the natural languages like English.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 18


Algorithm Example–Find out Area of Circle
Step Start
1: To Start execution, we have to
Step Initialize PI  3.14, area 0 write 1st step “Start”
2: In this step we had declared 2
Step Read value of r variables PI and area then we
3: In this step we had
will initialize
take value of values
their r
Step Calculate area  PI * r * r from user
4:
Here calculation for area will done
Step Print area and value for area will be initialized
5:
After calculation, we have to print the
Step Stop area’s value, So this statement will print
6: thatexecution,
variable’s we
value
To terminate have to write last
step “Stop”

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 19


Advantages of Algorithm
 Easy to write.
 Easy to understand it
 It is not dependent on any programming languages.
 Every steps has its own logical meaning and then it follows the next step.
 Big problems can be divided in to small parts.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 20


Limitations of Algorithm
 Difficult to debug.
 Time Consuming.
 Difficult to show branching and looping.
 goto makes it hard to trace some problems.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 21


Characteristic of Flowchart
 Must use standardized Symbols.
 Symbols should be correct and as per the flowchart rules.
 Should contains clear, short and readable statements in symbols.
 Must be clear starting and ending point.
 Must contains clear arrows for conversion from one step to another step.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 22


Flowchart v/s Algorithm
v/
Flowchart s
Algorithm
• It is a pictorial representation of a process. • It is step wise analysis of the work to be
done.

• Solution is shown in graphical format. • Solution is shown in natural language like


English.

• Easy to understand as compared to algorithm. • It is somewhat difficult to understand.

• Easy to show branching and looping. • Difficult to show branching and looping.

• Flowchart for big problem is impractical. • Algorithm can be written for any type
problem.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 23


Examples
Section 2
Example – 1
 Draw flowchart and Write algorithm to find the sum and average
of two numbers
Start
Step 1: Read the numbers
Read a a, b
Read b
Step 2: sum= a+b, avg=
sum = a + b sum/ 2
avg = sum / 2
Step 3: Print sum, avg

Print sum Step 4: Stop


Print avg

Stop

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 25


Example – 2
 Draw flowchart and Write algorithm to find whether given number
is even or odd.

Start
Step Start
Read 1:
value of Step Read value of no
no 2:
Step If no mod 2 , goto Step
3: =0 5
If no Step Print no is , goto
YES mod NO 4: odd Step 6
Step Print no is even
2= 5:
Step Stop
0
6:
Print no is Print no is
even odd

Stop
Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 26
Example – 3
 Draw flowchart and Write algorithm to find maximum number
from two numbers.

Start
Step Start
1:
Read a, b Step Read a, b
2:
Step If a > , goto
3: b Step 5
Step Print b is , goto
YES If NO 4: maximum Step 6
Step Print a is
a>b 5: maximum
Step Stop
6:
Print a is Print b is
maximum maximum

Stop

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 27


Example – 4
 Draw flowchart and Write algorithm to print 1 to N numbers.

Start Step Start


1:
Step Read
Read N 2: N
Step Initialize count  1
3:
Step Repeat step 5 to step 6 until
Initialize count 
4: count ≤ N
1 Step Print count
5:
Step Increment count by 1
Is NO
6: count  count +1
count
≤ N Step Stop
YES 7:
Print
count
Increment count
by 1 Stop
count  count +
Prof. Ashok G. Badresiya
1 #2304CS101 (CP-I)  Unit 1 – Introduction 28
Example – 5
 Draw flowchart and Write algorithm to print ODD numbers
between 1 to N numbers.
Start
Step Start
1:
Step Read
Read N
2: N
Step Initialize count  1
Initialize count  3:
Step Repeat step 5 to step 6 until
1 4: count ≤ N
Step Print count
5:
Is NO Step Increment count by 2
count 6: count  count +2
≤ N Step Stop
YES 7:
Print
count
Increment count
by 2 Stop
count  count +
Prof. Ashok G. Badresiya
2 #2304CS101 (CP-I)  Unit 1 – Introduction 29
Example – 6
 Draw flowchart and Write algorithm to print addition of ODD
numbers between 1 to N numbers.
Start Step Start
1:
Step Read
2: N
Read N Step Initialize count  1
3:
Step Repeat step 5 to step 6 until
Initialize count  1 , sum 4: count
Step sum  ≤
sumN + count
0
5:
Step Increment count by 2
Is NO 6: count  count +2
count Step Print sum
≤ N 7:
YES Step Stop
8:
sum  sum + count

Increment count
by 2 Print
Stop
count  count + sum
Prof. Ashok G. Badresiya
2 #2304CS101 (CP-I)  Unit 1 – Introduction 30
Example – 7
 Draw flowchart and Write algorithm to find maximum number
from three numbers.
Start
Step 1: Start
Read a, Step 2: Read a, b, c
b, c
Step 3: If a > b , goto
YES NO Step 7
If Step 4: If b > c , goto
a>b Step 6
Step 5: Print c is maximum
YES If NO NO YES
If Step 6: Print b is
a>c b>c maximum
Print a is Print c is Print b is Step 7: If a > c , goto Step 9
maximu maximu maximu Step 8: Print c is maximum
m m m
Step 9: Print a is
maximum
Step 10: Stop

Stop
Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 31
Example – 8
 Draw flowchart and Write algorithm to find factorial of the given
number.
Start Step 1: Start
Step 2: Read N
Read N
Step 3: Initialize count  1, fact
Initialize 1
Step 4: Repeat step 5 to step 6 until
count  1, fact  count ≤ N
1 Step 5: calculate fact  fact *
count
Step 6: count  count +1
Is NO
count Step 7: print fact
≤N Step 8: Stop
YES
calculate
fact  fact *
count
Increment count
by 1
count  count + print fact Stop
Prof. Ashok G. Badresiya 1 #2304CS101 (CP-I)  Unit 1 – Introduction 32
Introduction of
Programming
Section 3
Program
 Program : a set of instruction which is written in any language which
computer can understand.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 34


Programming
 Programming : Task to write the program is known as Programming .

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 35


What is Language?
 A language is a system of communication used by a particular country or
community.
 It is a structured system of communication used by humans, based on
speech and gesture (spoken language), sign or often writing.
 Both person should understand each other’s language.

Hello! કેમ છો!


你好 મજામાં

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 36


Programming Language
 A programming language is a computer language that is used by
programmers (developers) to communicate with computers.
 It is a set of instructions written in a specific language to perform a
specific task.
 It allow us to give instructions to a computer in a language the computer
understands.
 5000+ programming languages are there, notably used are approximate
250.
 E.g. C, C++, C#, Java, Python, PHP, Pascal, etc.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 37


Types of Programming Languages
1. Low-level programming language
 It is a machine-dependent (hardware specific) programming language.
 It consists of a set of instructions that are either in the binary form (0 or 1) or in a
symbolic and human-understandable mnemonics (ADD, MOV, SUB).
 E.g. Machine level language, Assembly language, etc.
2. High-level programming language
 It is closer to human languages than machine-level languages.
 It is easy to read, write, and maintain as it is written in English like words.
 It allows to write the programs which are independent of a particular type of
machine (hardware).
 A compiler is required to translate a high-level language into a low-level language.
 E.g. Python, Java, JavaScript, PHP, C#, LISP, FORTRAN, etc.
3. Middle-level programming language
 Middle-level programming language lies between the low-level and high-level
programming language.
 E.g. C, C++, etc.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 38


History and Importance
of C
Section 4
History of C
 C Programming language was developed in 1972 by Dennis Ritchie at
Bell laboratories of AT & T, which is located in USA.
 It was named as ‘C’ because many of its features were derived from
earlier language ‘B’ .
 C is structured programming language.
Year Name of Language Developed By

1960 ALGOL International Group

1967 BCPL Martin Richards

1970 B Ken Thompson

1972 Traditional C Dennis Ritchie

1978 K&R C Kernighan & Ritchie

1989 ANSI C ANSI Committee

1990 ANSI/ISO C ISO Committee


Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 40
Importance of C
 Simple and Clear
 Small Size
 Structured Language
 Pointer Implementation
 Middle level language
 Portable
 Case sensitive
 Easy Error Detection

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 41


Advantages of C Language

1. Modular Language:
 Large program can be divided into small functions to reduce complexity.

2. Portable language:
 Program written in one computer can be executed on another computer without any
modification.
3. Concise language:
 There are only 32 keywords in C.

4. Datatypes:
 Variety of data types are available for efficient and easy programming.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 42


Advantages of C Language (Conti..)

5. Operators:
 Most of the possible operators for variety of operations are available.

6. Speedy Execution:
 Execution of program is much faster than languages program.

7. Core Language:
 We can write application software as well as system software in C. Also it is base of
many other programming languages.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 43


Compiler
 Compiler will convert the high-level code in to low level code which
computer can understand.

High Low
Compiler
level level
Languag Languag
e e
Compila
tion
Error

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 44


Compiler v/s Interpreter
v/
Compiler s Interpreter
• It takes an entire program at a time. • It takes a single line of code at a time.

• It generates intermediate object code. • It does not generate intermediate object


code.

• It works fast. • It works slower comparatively.

• Display all errors after compilation, all at the • Displays error of each line one by one.
same time.

• Error Detection is difficult comparatively. • Error Detection is easy comparatively.

• C, C++ uses compiler. • PHP, Pearl, Python uses Interpreter.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 45


Introduction of C
Section 5
C Header File
 In C programming, a source code file that contains common functions and
types that all programmers may use as required is called Header file.
 We have to include them in any program using #include statement.
 It is also known as include files or library files.
 Header files has .h extension
 Example of header files are: stdio.h, math.h, conio.h

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 47


C Token
 In C, token means smallest individual element of the C programming
language that is meaningful to the compiler.

Keywo
rd

Identifi
String
er

C Tokens

Special
Consta
Operat nts
ors
Operat
ors

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 48


Keyword
 Keywords are the words whose meaning has already been fixed to the
compiler. Its meaning can not be changed.
 Keywords cannot be used as variable names because if we do so we are
trying to assign a new meaning to the keyword, which is not allowed by
the computer.
 All keywords have fixed meanings and these meanings cannot be
changed.
 Keywords serve as basic building blocks for computer statements.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 49


32 Keywords in ‘C’

KEYWORDS
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 50


Identifier
 Identifier is used as the general terminology for the names of variables,
functions and arrays.
 Identifiers are user defined names, combination of letters and digits with
either a letter or the underscore (_) as a first character.
 Both uppercase and lowercase letters are permitted.
Valid Identifier Invalid Identifier
• Name • 5size
• _Student_Name • \@hello
• length • int
• total_sum • mn
• num1 • m+n
• num_2 • $num

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 51


Rules to declare Identifier
 The first character in an identifier must be an alphabet or an underscore.
 Must contain only letters, digits or underscores.
 Uppercase and lowercase letters are distinct. Means, identifiers are case
sensitive.
 Special characters, blank spaces or keywords are not allowed within an
identifier.
 Only first 31 characters are significant.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 52


Variable
 To store any value, we will require some location.
 Variable is a name used to refer that location in memory.
 Variable may take different values at different times during execution of
the program.
 At the time of variable declaration, we have to tell which type of data it
can hold.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 53


Rules to declare Variable
 It must consists of only alphabets (a-z, A-Z), digit (0-9) and underscore(_).
 First character must be an alphabet or an underscore.
 Only first 31 characters are significant.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 54


C Comments
 A comment is a line or paragraph of text in a program which is used for
documentation or message purpose.
 Comment is ignored by the compiler when it compiles programs.
 The C/C++ language accepts two types of comments.
1. Single line Comments //
2. Multi line Comments /*………
………*/

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 55


Datatypes in C
Language
Section 6
• Datatypes are used to store
various types of data. Data
• Different data type has
different memory allocation. type • These data types

For Example age,
are created by
roll number,
programmer as
mobile no.,
per requirement.
1. 2. name, • It is combination
Primary Secondar pr/percentage,of primary data
book name,type book
Data y Data and/or
price etc. derived data type.
types types •• ItIt is combination
helps to
• They are basic oforganize
primary dataand
handle to
types realhandle
world
datatypes.
1.1 integer 2.1 2.2 User data lifein data
real morein
• Programmers
mostly use this 1.2 float Derived Defined convenient
more
• They
way.
convenient
are extension
datatype. 1.3 char Data Data way.
of primary data
• 1.4 void • Directly not
They are machine
types types type by compiler.
supported by the
specific datatype.
machine.

2.1.1 2.2.1
2.1.2
Array 2.2.2
Structure
2.1.3
Pointer 2.2.3
Union
Prof. Ashok G. Badresiya
Function
#2304CS101 (CP-I)  Unit 1 – Introduction
Enum 57
1. Primary Data type
1.1
integer
 Integer numbers are whole numbers without decimal point and fractional
part, e.g. 20, -154 etc.
 Signed number can store positive or negative values. e.g. 20, -154.
 Unsigned number means value can be only zero or positive. e.g. 20,
100, 0.
 By default variable is signed.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 58


1. Primary Data type
1.1 in
integer t
Signed Integer Unsigned Integer
Data
type Size in bytes Range Size in bytes Range
short int 1 -128 to 127 1 0 to 255

-32768 to 0 to
int 2 2
32767 65535
-2,147,483,648 0 to
long int 4 to 4 4,294,967,
2,147,483,647 295

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 59


How to calculate Range ??
1.1 in 1 byte = 8
integer t bits Equation
Equation
for
for Signed
Unsigned
Signed Integer Unsigned Integer n- n- n
Data
type Size in Size in - 1 t 21 - 0t 2 -
Range Range n = No. of n = No. of
bytes bytes
2 o bits 1 o bits1
short int 1 8 -128 to 127 1 8 0 to 255

-32768 to 0 to
int 2 1 2 1
32767 65535
6 6
- 16- t 216- - 0 t 21 -
1 1 6
0 to 1
o
- 21 o1
2,147,483,64 0 to
1
3 3 - t 25 -
long int 4 2 8 to 4 2 4,294,96 5 65535
2,147,483,64 7,295 2 o 1
-32768 to
7
32767

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 60


1. Primary Data type

1.2 float

 Float numbers contain decimal point and fractional part, e.g. 3.25, 7.89, -
100.25, 10.00.
 Precision means with how much accuracy required when we store digits
after decimal points. e.g. 3 then we can store up to 3 digits after decimal
point.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 61


1. Primary Data type
flo
1.2 float
at
Data Size in
Range Precision
type bytes
float 4 -231 to 231-1 6

double 8 -263 to 263-1 14

long
10 -279 to 279-1
double

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 62


1. Primary Data type
1.3 cha
character r
 Character type variable can hold a single character, e.g.: ‘A’, ‘z’, ‘9’, ‘ ‘.
 Internally, each character is assigned some integer value known as ASCII
values.
Signed Character Unsigned Character
Data
type Size in bytes Range Size in bytes Range
char 1 -128 to 127 1 0 to 255

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 63


1. Primary Data type
1.4 voi
void d
 void means nothing, no value. So we can not declare variable of type void.
 void is used to indicate that function is not returning any value.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 64


2. Secondary Data type
 Secondary data types are not directly supported by the machine.
 It is combination of primary data types to handle real life data in more
convenient way.

2.1 Derived
2. Secondary Data type
Data type
2.2 User Defined
Data type

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 65


2. Secondary Data type
2.1 Derived
Data type
 Derived data type is extension of primary data type.
 Derived data type is built-in in the C language and its structure cannot be
changed.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 66


2. Secondary Data type

Poin
2.1 Derived Arr ter
Data type ay
Functi
on

 An array is a fixed-size sequenced collection of elements of the same data


type grouped under a single variable name.
 Pointer is a special variable which stores address of another variable.
 Function is a block of code for some particular task.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 67


2. Secondary Data type
2.2 User Defined
Data type
 User defined data type is created by programmer as per requirement.
 It is combination of primary data type and/or derived data type.
 It helps to organize and handle real world data in more convenient way.

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 68


2. Secondary Data type

Unio
2.2 User Defined Struct n
Data type ure
Enum

 Structure is a collection of logically related data items of different data


types grouped under a single name.
 Union is like a structure, except that each element shares the common
memory.
 enum is a user-defined type consisting of a set of named constants called
enumerators. enum day {Mon, Tue, Wed, Thu, Fri,
Exampl
e Sat, Sun};
enum day week1stday ;
Prof. Ashok G. Badresiya week1stday = Mon;
#2304CS101 (CP-I)  Unit 1 – Introduction 69
Constant
Section 7
Constant
 constant means not modifiable.
 When we want to use certain variables whose values are not changeable
during the execution of a program, we may declare the variable with the
const keyword at the time of initialization.
 Example-1: const int year= 2020;
 Example-2: const float spi = 7.8;

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 71


• • Number containing the fractional part is
Constants are values that never
change. called real number.
• • A real number may also be expressed in
A constant is a number,
character, or string that can be exponential notation.
• Example: 215.62  2.1562e2 or 21562e-2
used as a values.

Constant
(Literal • String constant is a sequence of characters
s) enclosed within a double inverted comma.
• The characters may be letter, number,
1. Decimal (0-9) special character, blank space, etc..
(signed, • Non
Example: “darshan”, “university”,
unsigned) Numeric
Numeric
“2023”, “@s”
2. Octal (0-7) Constant
(signed, Constant
unsigned)
3. Hexadecimal (0-
9,A-F)
Float/
Integer Character String
Real
Constant Constant Constant
Constant
1. Decimal : 123,-79 • Single character enclosed within a pair of single
2. Octal : 127,-73 quote mark.
3. Hexadecimal : • Example: ‘a’, ‘p’,’P’, ‘Z’
9AB,Prof.
82F Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 72
Backslash Constant
 Back slash constants are a special type of character constant which
consists of two characters.
 This is known as escape sequence. Escape sequence starts with backslash
‘\’ character.
Escape
Seque Meaning
nce
\0 End of string – NULL

\n End of line – takes the control to next line

\t Horizontal tab

\\ Prints backslash character ‘\’

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 73


Control String
 To access different kind of data types, we have to use different kind of
control strings, which are as below.

Data types Control String


signed char / unsigned float %f
%c
char
short signed int / signed double %lf
%d
int
short unsigned int / long double %Lf
%u
unsigned int
long signed int %ld

long unsigned int %lu

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 74


Type Casting
Section 8
Type Casting
 Converting a variable from one datatype into another datatype is known
as type-casting.
Sometimes we want to
do a type conversion in
a way that which is
int different from automatic
a=12,b=5,su conversion is known as
m; avg = (float)sum
“Explicit / 2; // Explicit
Conversion”
float avg; conversion
short long
long doub
int int float doub
/char int le
le
Conversion is
done avg = sum / 2; // Implicit
automatically by conversion
the compiler is
known as
“Implicit
Prof. Ashok G. Badresiya
Conversion”
#2304CS101 (CP-I)  Unit 1 – Introduction 76
Structure of C Program
Section 9
//This program calculate sum of
Documentation two digits
Section
Link Section //Author : Ashok
Consists of a set #include<stdi
of comment lines Badresiya
which provides
Contains name
Definition Section headeroffiles
the program,
o.h>
which the author
includes
#define
and other details.
PIfunction which
are being to be used in the program.
This section defines all 3.14
Global Declaration the symbolic
void print();
Section constants
Used to declare the global variables used in the program. Global variables
main() void
are the variables main()
used in more than one function. This section also declares
{ all the
Every C program mustuserhave{ one
defined functions.
main function section. It indicates
Local Declaration int a;
start point.
Section
Executable part Declares all the print();
variables used in
} Containthe
at executable part.
least one}statement which contains instructions to perform certain
tasks. The declaration and executable part must appear between the opening and
closing braces. All statements in the declaration part must end with the semicolon.
Sub Program void print()
Section()
{ This section contains all {the user defined functions that are called in the main
function. These functions areprintf(“Hello
performed by!”);
user specific tasks.

… }
}

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 78


ASCII Code
 ASCII means American Standard Code for Information
Interexchange
 ASCII Characters and Code;
Charac Decim Binary Charac Decim Binary Charac Decim Binary
ter al ter al ter al
A 065 010000 a 097 011000 0 048 001100
B 066 010000
01 b 098 011000
01 1 049 001100
00
10 10 01
C 067 010000 c 099 011000 2 050 001100
11 11 11
.. .. .. .. .. .. .. .. ..

.. .. .. .. .. .. .. .. ..

Z 090 010110 z 122 011110 9 057 001110


10 01 01

Prof. Ashok G. Badresiya #2304CS101 (CP-I)  Unit 1 – Introduction 79


C Programming - I (CP – I)
DU # 2304CS101

Thank
You

Prof. Ashok G Badresiya


Department of Computer Application
Darshan Institute of Computer Application
[email protected]
9913131691

You might also like