C Programming 1

You might also like

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

# VIOLETT HOW TO LEARN C PROGRAMMING 1

Contents Page
Introduction to C
1. How we learn C programming 1
2. History of C 7
3. Compilers 8
4. Structrur of C program 8
5. Programming Rules (C program ) 12
The C Declarations
1. C character Set 12
2. C Keywords 14
3. Identifiers 14
4. Constants 15
5. Variables 16
6. Variables 17
7. Data Types 17
8. Declaring Variables ( v r s ) 19
9. Variables 20
t typ 21
11.Constants and Volatile Variables 22
Operators and Expressions
1. Introduction 25
2. Arithmetic Operators 26
3. Relational Operators 33
4. Logical Operators 35
5. Priority of Operators and their Clubbing 37
Input and Output in C
1. Introduction 40
2. Formatted Functions 41
3. Unformatted Functions 41
Decision Statements
1. Introduction 55

K ZAN HTIKE
2 HOW TO LEARN C PROGRAMMING # VIOLETT

2. The if Statement 55
3. Th f… s St t m nt 60
4. Nestef if...else Statement 64
5. The break Statement 70
6. The continue Statement 71
7. The goto Statement 71
8. The switch Statement 73
Loop Control Statements
1. Introduction 81
2. The for loop 81
3. Nested for loops -
4. The while loop 94
5. The do while loop 96
6. the do..while 96
Arrays 98
1. Array Initialization 98
2. Definition of Array 98
3. One Dimensiona Array 99
4. Two-Dimensional Arrays 107
User defined functions 111
1. Make user defined functions 111
2. Call by value and call by refefence
Pointers 121
Pointer 121

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 1

1. How we learn C programming

We are Order or Programmer


C Programming Language
Programming ႔

႔ programming ႔
programming

r w r n Softw r ႔
Hardware ႕
Keyboard, Driver
tron v ႕ system Hardware
machine
Hardware task ( ) ႕ Software
႕ “ r w r w thout softw r s s m r to
o y w thout sou ” Softw r hardware “ ”
႕ r w r ႕
႕ softw r Softw r
programming language ႔ ႕
Adobe PageMaker,Excel,Power Point
pro r mm n n u Softw r pro r mm n

programming languages C programming language


႕ C programming ႔ ႕
႕ Academy
Un v rs ty ႕ C language

pro r mm n n u
pro r mm n n u
n u C language middle level
language ႕ programmer C language
C language hardware

K ZAN HTIKE
2 HOW TO LEARN C PROGRAMMING # VIOLETT

language
programming sense,programming flow ႕ language
C language softw r m
႕ ႔ ႕
n u ႕ program
High level language C language
Softw r
o pp t on ႕ high level language
႕ C language structure language
႕ memory concept high level
language C language Human language
language 100%
pro r mm n programming
computer t t sp y

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 3

C program run output result ႕


Monitor sp y computer
stru tur r m ႕ programming language
language Software C
programming language ႕ Softw r
o o s v ++,
V su stu o C program ႕ sour o
complie ႔ compiler Software ႔
pro r mm n sour o ႕ software ႕ Editor
sour o ႕ pro r mm n n u
n sh n u
printf() function function C language t
utput sp y ႕ printf() n sh ph t code
English like language
႔ programming language ႕
sour o computer
pro m ႕ nstru t on
instruction program

Computer hum n n u
႔ pulse(1) or no pulse(0)
႕ code
႕ human language machine language ႕
programming terms Compiler
႔ sourc o (*.c) ႕
extension dot c ႔ sour f (.c)
႕ c++ source file ( *.cpp)
Compiler ႕ (*.c) ႕ o
Assembly language ႕
data omm n Analysis
႔ error ( ) ႕ instruction
႔ source file t omm n
rror error sour

K ZAN HTIKE
4 HOW TO LEARN C PROGRAMMING # VIOLETT

o Assembly language Assembly


language low level programming language language 30%
machine 70% ss m y o
computer computer
machine code ႕ assemble
Assemble - (3 ႕4) ႕ pro r m
႔ human language (3+4) ႕ program 3
႕ 4 data ႔ ႕ ႕ (+) sign command
႔ computer
Assembler machine code

Decimal number 3 ႕ binary code Decimal number 4 ႕ binary


code binary code 0 to 1 (2 digit) t
႕ base 2 3 binary code ႕
0011 Decimal number 0 to 9 ( 10 digit)
23 8 digit t ႕ 24 ( t)
base 2 ႕ power( ) bit ႔ t
m num r n ry o ႕ 4 bit (8 4 2 1) code
decimal number 3 ႕ 4 compiler

m num r ႕ (8 4 2 1) code 2 ႕
+ 8 ႕ 4 2 ႕ 1 1 (on)
4 ႕ 4
1 8,2,1 0(off) 4=0100
decimal number 2 ႕ 0
2 ႕ binary code

3/2=1 =1
½
4 bit 11 t ႕
3 = decimal number 4

4/2=2 =0

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 5

2/2=1 =0
½ 100 ႔ ႕ t
႕ ႔
Assembler 3 ႕ 4 binary code (machine code)
command data addition (+)

0011
+ 0100
0111
binary code ႔ result machine
o encoding ႔ computer
႕ decimal
o 0111 (8 4 2 1)
8 (4 2 1)
7 encoding t decode
execute 7 ႔ sp y ႔
Assemble m hn o object code
႔ Assembler ႕ ႔
Source code Assemble error ႔
Editor object file (*.obj)
t o linker ႕ ႕
Obj code sour o linker
C libries file sour f ႕ linking linking
linker error error libries
h r f fun t on C source code program
linker error C libries f h r
f fun t on ႔ error Editor
rror o t f Executable file
x ut f (*.exe) f
o t f user sp y ႕ decode
f 3+4 = 7 ႔ user sp y
႕ n u pro r m Computer

K ZAN HTIKE
6 HOW TO LEARN C PROGRAMMING # VIOLETT

႕ x ut o

Programming language ႕
China, India, Japan
programming paradigms ႔ n u
n sh n u ႕ English language paragraph
႕ s nt n ႕ ph ts Vouw
Noun or Verb ႕ C language
program ႕ sentences nstru t on
Vouwel, Noun, verb Data type, Keyword,
Variables ႕ Alphabests Sp sym o s ts
႔ n u ႕ ph ts
program ႕

C language nstur t ons ႕ fun t ons h rf


fun t ons h r f
႕ ႕ ႕

႕ ႔

၁ ၅

၃ PT dress

“ ” ႔
“ ႕ ”


Dress

႕ “ ႕ ”

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 7


႕ “
” “
႕ ”
႕ functions
႔ header file ႔
functions function header file
C language fun t on h r f
C language t output sp y ႕
printf() function (print function) function library file
stdio.h(standard input /output header) ႔ header
file " "
fun t ons program fun t on
function header file program
႕ t ywor p r tor
႕ ႕ program
programming language paradigms ႕
programming Analog
Digital C
programming language
We are order ႔
႕ ႕
pro r mm r ႕

2. History of C
1960 omput r n u -
COBL ( language omm r pp t on ) ႕
FORTRAN Engineering ႕ S nt f pp t on ႕

n u language ႕
ALGOL 60 language
language CPL(Combined Programming Language)
Cambridge University develop language

K ZAN HTIKE
8 HOW TO LEARN C PROGRAMMING # VIOLETT

Cambridge University Martin


Richards BCPL(Basic combined programming language ) develop
language CPL language BCPL
n u T & T'‟ s s B language
Dennnis Retchie language language develop
language pro r mm n n u C Program
Dennis Ritchie Bell Laboratories 1972 C
programming language “ s om n pro r mm n n u “ ႔ “ ”
developed n u C language Extented B language
႔ C language High levevl language
Low level language
Language C programming language ႕ History

3. Compilers
Compilers ႔ Human
language ႕ Machine Language Translate ႔ ႔
Sour o s Compilers Run Translate
Error rror List Sour
o Compiler ႔ GNU GCC Compiler Code block software
Built in

4. Structure of C program

Include header file section


Global Declaration Section
/* comments */
main() function
{
/*comments*/
Declaration part
Executable part
}
User-defined functions
{
}

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 9

C program n u Sty
Stru tur -

1. include header file section


include header file section pro r m fun t on
h r f ႔ (
႕ “h o wor “ ႕ output printf()
function ႔ nput utput fun t on
predefined standard Input/Output header file
႔ ) h r
f default ႕ (extension dot h) (.h) ႕
h r f
#include preprocessor directive opein/closed arrow
header file ႕ name
#include<file name>
- #include<stdio.h> or #include"stdio.h"
standar I/O fun t on program ႔
header file ႕ Header file
program

2. Global Declaration
Global Declaration variable program
function functions ႕
႔ variable ႕ function ႕
scope ႔ function
႔ main() function ႕ Global ႕
Global v r o v r ႔
Structure program

3. Executable part
Executable part p r tor s on St t m nt fun t ons
program ႕ nstru t ons
st t m nt Curly brace { } ႕ program
႕ x mp

K ZAN HTIKE
10 HOW TO LEARN C PROGRAMMING # VIOLETT

4. User_defined function
User_define function user fun t on
fun t on main() function ႕
main() function ႕ ႔
user ႕
syntax ႕ ၈

5. Comments
Comments program St t m nt
program ႕
user ႕
/* */ ႕ //
႔ omm nt program run Compiler run
programmer
.. Structure x mp pro r m ႕

o Wor pro r m ႔ ....

Structure ႔ Beginner
႔ programming language
Hello World Programming ႕
႕ … ro r m
St t m nt
line by line program ....

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 11

◆ #include <stdio.h> Standard Input/Output Header file


Header file un t on current
program ႔ ႕ ..
◆ Comment
႕ Hello world screen ႕ monitor
( ) output ႕ programmer program
o wor pro r m
◆ main() function C language
႔ main function ႕ program
run ႔ C program main function
St t m nt fun t on run ႕
႔ C language ႕ ov r
..... m n fun t on ႕ program
႔ op n n r ( { )
os n r ( ) ႕
◆ pr ntf(“ o wor ”); St t m nt ႕
Hello world output ႔
pr ntf(" “); function printf function
႕ ႕ ou quot (" ”) Monitor ႕ -
Hello world ႔ I love U double quote I love U
screen monitor pr ntf()
fun t on ႔ program ႕ function ႕ r ry f
Standard I/O header file include ႔
semicolon (;) Statement
Compile error

◆ .. pro r m ႔
os n r ( ) ႕ su ss
pro r m Examp

My name is Mg Mg
I Love U Baby !
Hi! Candy.
… screen

K ZAN HTIKE
12 HOW TO LEARN C PROGRAMMING # VIOLETT

5. Programming Rules (C Program )


programming languages pro r m
C programming ႕

၁ Statements lower case letter ႕ upp r s tt r


symbolic const nts ႔ Symbolic constants

#define AGE 19
# fn N “ ”
AGE, N Sym o onst nt
႕ n sp s ႔
v r n m keywords, onst nt n fun t on
n sp s ႔
၃ open brace { ႕ close brace } St t m nt
႔ ႔
Syntax ႕ programmer
St t m nt ႔ statement
႕ semicolon(;)
-
a =b+c;
d=b*c;
(or)
a =b+c; d=b*c; …
opening brace { ႕ closing brace } ႕
open brace ၃ close brace

lesson

2. The C Declarations
1. C character Set
C language h r t rs t ႕
1. Letters
2.Digits
3.White spaces
Sp h r t rs T ႕

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 13

1. Letters 2.Digits 3. White Spaces

Capital A to Z All decimal digits 0 to 9 Blank space

Small a to z Horizontal tab

Vertical tab

New line

Form feed

4. Special Characters
, Comma & Ampersand
. Period or dot ^ Caret
; Semi-colon * Asterisk
: Colon - Minus
„ Apostrophe + Plus
“ Quotation mark < Less than

! Exclamation mark > Greater than


| Vertical bar () Parenthesis left/right
/ Slash [] Bracket left/right

\ Back slash {} Braces left/ right


~ Tilde % Percent

_ Under score # Number sign or Hash


$ Dollar sign = Equal to
? Question mark @ At the rate

h r t r S t ႕ ro r m
႕ ႕ pro r m

Sp h r t rs
Delimiters Use

K ZAN HTIKE
14 HOW TO LEARN C PROGRAMMING # VIOLETT

: Colon Use for label


; Semicolon Terminates statement
() Parenthesis Used in expression and function
[] Square bracket Used for array declaration
{} Curly Brace Scope of statement
# Hash Preprocessor directive
, Comma Variable separator

2. C Keywords
Program ywor s
႔ ywor s C Compiler
႔ ႕ t ႕ m mory
႕ v r n m
C compiler ywor ႕ ႔
error ႕ ywor
႕ ႕ ႔
“ ႔
” ႕ ႔ T

C Keywords

auto do Double If Int static Struct while


Break Default Else Goto Long Sizeof Switch Volatile
Case Continue Enum For Register Signed Typedef Void
char Const extern Float return Short union Unsigned

3. Identifiers
Identifiers v r s n m ,fun t ons n m , rr ys n m
nt f rs un r s or ( ) ႔
႕ -
F_name, sum_of user nt f rs
example ႕
a) #define N 10

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 15

b)# define a 15
N ႕ a user nt f rs

4. Constants
Constants program execute
a ႕
3 ႔ program execute a ႕
3 ႔ constants value ႔ C
language ႔
T ႕

1. Numeric Constants
a) Integer Constants
Integer Constants

႕ C language onst nts



- 10,20 ,+30, -5 etc.

b) Real Constants
Real constants
floatin point constants ( )
- , , t r onst ns

- 2456.123 2.456123E3 ႔ exponent ႔ „ ‟ tt r
mantissa ႔ exponent

K ZAN HTIKE
16 HOW TO LEARN C PROGRAMMING # VIOLETT

Exponent
onst nts (constant)

2. Character Constants
1) Single character constants
Character constant Single character constant ႔
onst nt single quote (' ' )
- ' ' , ' ‟, ' ‟ t
h r t r onst nt ASCII (American Standar Code For Information
Interchange) letter A to Z
integer 65 90 Small letter a to z
97 122 decimal ႕ x mp

printf("%c %d", 65, 'B') Output 'A' ႕ 66 display


႔ A ႕ ASCII value 65 conversion character (%c)
character Output 65 ႕ character A
Output
B letter B ႕ ASCII 66 conversion character (%d)
႕ Decimal output B ႕ decimal 66
႔ ASCII 0 127
decimal ႕ character

2) String Constants
String Constants double quote (“ “)

………… “ o” ,” n ” , “ ” ,” ”
ou quot (“ “) Str n
onst nts ႔ ႔

5. Variables
Variables ႕ program t
႕ memory ႕ v r n m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 17

႕ ႔
႕ ႔
႕ ႔ v r n m
႔ Variables ႕ ႕
a variable ႔ ႔ a ႕ 1
(a constant
႕ ႕ ) variable ႕
Variable ႕ ႔

…… Tr n ႕ Area ႕ v r n m
႔ length, width ႔ length, width ႔ a, b
႔ v r n m ႕

6. Variables
Variables v r

၁ V r n m ႕ h r t r ႕

= 1height height ႔
un rs or ႔ (F_name).
V r s ႕ ywor s ႔ ႕ ႔

၃ V r n m character upper ႕ lower
variable ..... SUM ႕ sum variable

/* */

7. Data Types
Data Types program t ႕
data integer character
Floating point ႕ Data type
data integer integer data type
Data type output
.... data t typ
T ႕

K ZAN HTIKE
18 HOW TO LEARN C PROGRAMMING # VIOLETT

Data Type Size/Bytes Range Format String


char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
short or int 2 -32768 to 32768 %d or %i
unsigned int 2 0 to 65535 %u
long 4 -214783648 to %ld
214783648
unsigned long 4 0 to 4294967295 %lu
float 4 3.4e -38 to 3.4e +38 %f or %g
double 8 1.7e-308 to 1.7e+308 %lf
long double 10 3.4e-4932 to 1.1e+4932 %lf

T char h r t r t
character data type name ႕ ႕ Byte (memory allocation size) yt
yt ႕ ႕ range -128 to 127
format string( or) conversion character t typ

video lesson ႕ ( data


type name ႕ ႕ ႕ format string int format
string ႔ %c user ႕
output ) program

Program a integer data type (int) data type


integer data type ႕ format string(conversion character )

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 19

%d %c character data type ႕ format string


Output a ႕ integer 65
character A output data type ႕ format string
output
data type format string
႕ ႔ pro r m

rn V r s (V r s )
V r s
variables
V r s C program ႕ structure
Declaration part program ႕
main function ႕ Global ႕ main
function ႕
႕ ႕

nt ; ( )

int data type 3 ႕ ႕


b v r n m integer
data v r n m
variable data t typ
႕ memory ႕
a variable name
3 data

Compiler variable name


႕ memory t ႕ v r n m
႕ variable t
data type

K ZAN HTIKE
20 HOW TO LEARN C PROGRAMMING # VIOLETT

m moy integer yt
char yt program ႕ variable
data type memory space
Compiler ႕ ႕ nt r t value type
String string data
႔ r f r n t typ ႔

char* v_name="abc"

႕ ႕ memory abc string v_name


variable name v_name variable name abc
string Address ႕ String reference type
႔ variable name t

9. Variables
Initializing v r
assign ႔ assign
ss nm nt op r tor "=" Operator
ss nm nt op r tor
... initializing ႕ declaring

int a;
a variable name nt r t typ

....
a=3; a ႕ ႔ assign
declaring ႕ initializing …
int a=3; ႔ ႕

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 21

t typ
Type Conversion data type data type
pro r m

program
◆ printf() function output
Division operation Result ႔ printf() ႕
႕ printf() 5 2 ႕
Screen
◆ Printf() function output ႔

႔ output output
႕ data type
format string program 5 ႕ integer
2 integer ႕ (Division) ႕
႔ ႕
integer ႕ integer data type ႕ format string (%d)
ou quot (“ “) output
႔ 2.5 2 ႔ ႕
%d integer data type ႕ f.s ႔ ႕ ႕
synt x double quote output r turn
႕ f.s omm (,)
statement

K ZAN HTIKE
22 HOW TO LEARN C PROGRAMMING # VIOLETT

variable name semicolon ႔

◆ Type Conversion program ႔


result 2.5 typ
႕ ႔ integer integer ႕
output integer data type
program
statement type conversion (float)5/2
႕ type 5 2 ႕ Division
integer integer ႕
႕ data type integer data type

႕ integer float 5/2 ႕ ႕ float


integer integer ႕ result ႕
return integer data type fo t t typ
output ႕ double quote %g float data type
႕ format string ႕ output %g ႔ %f
႔ float data type ႕ S

11. Constant And Volatile Variables


a) Constant Variables
Constant Variables program constant
variable ႕ ႔
constant

const int a=4; variable a ႕


4 (integer data type) program ႔ Compiler
constant const ႕
ywor data type ႕ ႕
b) Volatile variable
Volatile variable constant variable ႕ ႕ ႕
volatile variable ႕
user 3 ႔ input user input

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 23

variable ႕ ... volatile


v r syntax
volatile int d; d ႕ v r
nt r t typ vo t v r
... ( ) Constant variable volatile ywor
ywor small letter

.. pro r m

pro r m line by line trace( ) ႕


◆ header file ႕ fun t on
႔ t output ႕ printf() function
standard input /output (stdio.h) header file include

◆ main() function ႕ st t m nt run


႕ program line by line
႔ ႔ compiler program run
run ႕
st t m nt ႔
◆ main () fun t on program ႕ ႕ curly braces({ })
Declaration part v r
int a; variable a nt r t typ ႔

K ZAN HTIKE
24 HOW TO LEARN C PROGRAMMING # VIOLETT

◆ a=3; initializing or aassigning 3


variable a ႕ ႔ a
႔ 3 …
◆ Declaration ႕ Initializing
int b=4; b nt r t
႔ b ႔ initializing
program
v r n m v r
a+b
◆ statement printf() function
a+b output a ႕ b ႕
nt r t typ %d output
program ႔ pro r m

program line by line trace \n


\n Escape Sequence s p S qu n
lesson program \n
\n n w n ( )
Output a ႔ b ႕
႕ \n \n
output

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 25

Sum of a and b=10Subtraction of a and b=2Multiplication of a and b=24Division of


a and b=1
႔ \n
..

3. Operators and Expressions


1. Introduction
p r tors ႕ p r tor
p r tors program
Operators ႕ ႕
op r tors ႕ a ႕
op r t on ႕
op r tors C program op r t on ႕
p r tors p r tors ႔
( ..). Operators T ႕

Type of Operators Symbolic Representation


Arithmetic operators +, - , * , / and %
Relational operators >,<,== ,>=,<= and !=
Logical operators &&, || and !
Increment and decrement operator ++ and --
Assignment operators =
Bitwise operators &,|,^,>>,<< and ~
Comma operators ,
Conditional operators ?:
op r tors Operators
softw r pp t on
pro r m programming
flow ႔ programming sense ႕ softw r
pp t on pro r m C language programming sense
C language ႕ softw r pro r m
႕ C language sense
n u ႕ C

K ZAN HTIKE
26 HOW TO LEARN C PROGRAMMING # VIOLETT

language C program
Operators 3 op r tors
႔ pro r m

1.Arithmetic operators
2.Relational operators
3.Logical operators

1. Arithmetic operators
Arithmetic operators ……
1) Binary Operator ႕
2) Un ry p r tor
1) Binary Operators
Operator Computer Language
r thm t op r tors

Addition Operator
႕ ႔ r thm t
op r tor Table ႕

Arithmetic Operator Explanation Example


Operators
+ Addition 2+2=4
- Subtraction 5-3=2
* Multiplication 2*5=10
/ Division 10/2 =5
% Modular division 11%3=2(Remainder
2)

Table ႔ ro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 27

◆ program Table r thm t op r tor


printf ()
Program run run ox
Operation Answer ႔ Printf() ႕ ႕ double
quote sp y utput fun t on ႔

◆ printf 2 ႕ 2 ႕ ႔
double quote Addition double quote
display %d ႔
%d Compiler t typ ႕
onv rs on h r t r (form t str n ) 2 ႕2
4 integer data type ႔
႔ ႕ data
type ႕ integer
integer data nt r t Output
%d %d place order ႕
program ႔ fo t
t typ %f double quote
Synt x ou quot comma
Expression

K ZAN HTIKE
28 HOW TO LEARN C PROGRAMMING # VIOLETT

႕ program double quote t sp y


႕ Expression ႔ 2+2 expression
႕ C program o
comma
comma ႕ expression
Additional opetator (+)
expression
႔ double quote
%d s m o on(;)
English Sentence fu
stop( ) C language Statement
Compiler
◆ Division ႕ Modular division
႔ Division
Modular division
… program 10 2 ႕ division
Output 5 11 3 ႕ modular division
output 2 … v s on ႕ Modular division ႕
………
b) Unary Operator

Operator Description or Action


- Minus
++ Increment
-- Decrement
& Address Operator
Size of Gives the size of variable
Table p r tor ႕ Unary Minus (-)

int x=-50;
int y=-x;
x=-50 x ႕ -
y=-x ႔ ss n x -
50 y y ႕ -

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 29

႔ x ႕ ႕ -
-(- ) - × - + y ႕
႕ Unary minus
Increment /Decrement Operators C oop st t m nt ႕
operator ႔

Assign x =x+1 ႔ assign x++


႔ increment operator ႕ x=x-1
decrement operator x-- ႔
Increment ႕ Decrement ..
1) Suffix and
2) Preffix
1) Suffix
Suffix Compiler run

Suffix ႕ (x++) or (x--)


example ႕
x=20;
y=10;
z=x*y++; x ႕ 20 y 10
y run 11 pro r m

K ZAN HTIKE
30 HOW TO LEARN C PROGRAMMING # VIOLETT

Program 20 ႕ 10 ႕ variable x ႕ y
int nt r t typ assignment operator (=)
variable z ႕ b suffix

◆ statement z=x*y++; x 20 ႕ y 10
y suffix run

200 z z output 200

◆ b=x*y; ႔ x
20 y
y run
y 220
b b output
220 Monitor display … suffix

2) Preffix
Prefix prefix variable run
prefix …(++x) or (--x)
pro r m

Program ႕
႕ prefix
x ႕ 10 , y ႕ z ႕ y

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 31

prefix y y run
suffix x ႕ y
210
◆ a x 10 y 21
y 20 ႔ a=x*y y (++y) ႔ ႕
႔ y vo t v r ႔
volatile variable ႕ y 20
႔ y prefix
y
◆ y 20 21 a y
႕ 21 output

Suff x Prefix

c) Size of () n „&‟ p r tor


„&‟ ( r ss) Operator computer ႕ memory variable ႕
႕ pro r m ႕

… program x ႕ y variable
Address ( ) ႔

K ZAN HTIKE
32 HOW TO LEARN C PROGRAMMING # VIOLETT

x ႕y 2 ႕3 program x
႕ y & operator
display &x x ႕ ႕ & & x
2 display &y ႔ Program
output & ႕ output

program & operator ႔

Size of () Operator
sizeof() Operator sizeof() Operator
႕ data m mory
pro r m ro r m
programming sense ႔

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 33

Program Program ႕
program line by line trace
႔ size of () ႔ sizeof () operator ႕
() variable ႕ byte Output Programming terms ႕
sizeof () operator ႕ parenthesis argument ႕ byte
program sizeof () ႕ argument x ႕
byte variable x ႕ data type nt r t typ 4
byte ႔ Output sizeof(y) float t typ
႔ fo t t ႕ m mory byte 4 output

2.Relational operators
t on op r tors v u s ႔
႕ r t on op r tors ႕

> greater than ႔


< less than ႔
== equal to ႔
>= greater than or equal ႔
<= less than or equal ႔
!= not equal ႔
op r tor ႕ ႕ ႕

Expression Result Value


4>3 True 1
4<3 False 0
5>=5 True 1
4<=3 False 0
3!=4 True 1
5==4 False 0
4 3 result
tru value ႕ Less than operator

K ZAN HTIKE
34 HOW TO LEARN C PROGRAMMING # VIOLETT

4 3 result f s value
5 5 5 ႕
tru
tru result pro r m

'
pro r m relational operator ႕ ႔
on t on op r tor Sorry...
Conditional operator Conditional
operator ႕
result pro r m ႕

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 35

◆ Program Conditional operator ႕ Symbol


( ? : ) Question mark ႕
႕ u st on m r
is to (:) ႕
program
4>3?1:0; Question mark ႕ ႕ 4 3
Question mark ႕ 1
Output Result = 1 ႕
◆ 4 3
is to (:) ႕ 0
st t m nt trace
pro r m ႕ flow

3. Logical Operators
Logical operators expression
program ႕ ႕ log
op r tor ႕ sym o ႕ t ႕
Operator Description or Action Example Return value
&& Logical AND 5>3 && 5<10 1
|| Logical OR 8>5||8<2 1
! Logical NOT 8!=8 0

◆ T operator &&
logical AND expression value
&& ႕ ႕ ႕ expression
value 1 return value
0 x mp && ႕ ႕ expression 5 3
႔ expression 5 10
expression return value

K ZAN HTIKE
36 HOW TO LEARN C PROGRAMMING # VIOLETT

◆ || operator o OR ႕
|| ႕ ႕ ႕ expression
return value 1
◆ ! operator o N T Example 8 8 ႕
expression return value 0
program

◆ pro r m output trace


printf () function Condition: Return Values
double quote
statement
\n

◆ printf Expression r turn v u ႕
return type 1 ႕ nt r t typ %d
printf() function ႕ output display Syntax
႕ comma(,) ႕ output value
variable output statement
program expression
return value statement
program

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 37

4. Priority of Operators and their Clubbing


… p r tor ႕ Priority ႕ Clubbing
p r tor
Priority Statement Operator ၅ ႔
op r tor priority
႕ system Clubbing
Statement Operators ၅

op r tor
T

Operators Operation Clubbing Priority
() Function call Left to Right 1st
[] Array expression or square
-> bracket
. Structure Operator
Structure Operator
+ Unary plus Right to Left 2nd
- Unary minus
++ Increment
-- Decrement
! Not Operator
~ Ones complement
* Pointer Operator
& Address Operator
Size of Size of object
type Type cast
* Multiplication Left to Right 3rd
/ Division
% Modular division
+ Addition Left to Right 4th
- Subtraction
<< Left shift Left to Right 5th
>> Right shift
< Less than Left to Right 6th

K ZAN HTIKE
38 HOW TO LEARN C PROGRAMMING # VIOLETT

<= Less than or equal to


> Greater than
>= Greater than or equal to
== Equality Left to Right 7th
!= Inequality
& Bitwise AND Left to Right 8th
^ Bitwise XOR Left to Right 9th
| Bitwise OR Left to Right 10th
&& Logical AND Left to Right 11th
|| Logical OR Left to Right 12th
?: Conditional operator Right to Left 13th
=,*=,-=, &=,+ Assignment operator Right to Left 14th
=,^=,|=,
<<=,>>=
, Comma operator Left to Right 15th
… table op r tor
x mp

Example

x mp Table multiplication (*)


Division(/) Addition Operator(+)

Example.

x mp priority
function call () ,multiplication(*) ,Division (/) Operators ၃ Table
function call() operator function

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 39

call ၃ ()
( ) ႕ 2 * 2
() Division operator
Program
Output ႕ pro r m

program ၃ ႕ average ၃
variable a,b,c ၃ ႕ average
average ႕
program ႕
႕ (a+b+c)/3 St t m nt
႕ op r tor ႕ Statement
႔ ၃ ၃ ႕
… + + program
pro r m

K ZAN HTIKE
40 HOW TO LEARN C PROGRAMMING # VIOLETT

Program 6 output Computer ႕


op r tor ႕ priority ႔
Syntax

a,b,c
3 ႕ Compiler
op r tor ႕ priority Division (/) operator
variable c 3 ႕ c 4
1.3333 integer data type ႔ integer
1 a , b ႕ 1 6
output . ႔ average
program Syntax
(a+b+c)/3

Syntax Operator 3 function call (), Addition(+),Division(/)


operator 3 priority fun t on ႕
Addition variables a,b and c 2,3,4
9 Division 9 3 ႕
Average 3 output
. p r tors ႕ Priority
umm s
Dummies ( )
႕ ro r mm r ( .....)

4. Input and Output in C


1. Introduction
c program input /output function
Programming Language nput utput fun t on

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 41

fun t on ႕
႔ User nput t us r
႕ fun t on ႕
C I/O function I/O data type

1) Formattef functions and


2) Unform tt fun t ons

1) Formatted functions
Formatted functions functions data
type Input/Output ႔ orm tt fun t ons User
t t Output User nput
onv rs on sym o data ႕ data type
integer data type nput v u
integer data type ႕ conversion symbol %d ႕ printf
() function t conversion symbol Output

3) Unformatted functions
Unform tt fun t ons t Input/Output ႔
h r t r t typ string typ ႔ Output ႔
function t I/o ႔ onv rs on
sym o
orm tt n Unform tt fun t ons fun t on t

K ZAN HTIKE
42 HOW TO LEARN C PROGRAMMING # VIOLETT

T fun t on ႕ ro r m ႕

1) Use of printf()
printf() ႕ printf()
function data values display Output ႕
conversion symbol v r n m s conversion symbol ႕ variable
names format x mp

◆ example program x,y,z t printf () function


output x nt r t ႔ conversion symbol
%d printf () function output data ႕
variable program y fo t
t typ ႔ conversion symbol %f z h r t r t
typ ႔ conversion symbol %c data type ႕
conversion symbol ႕
z C character ႔ sn quot („ „)
initializing program printf()

C ASCII value
ASCII value Table

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 43

K ZAN HTIKE
44 HOW TO LEARN C PROGRAMMING # VIOLETT

T pro r m S v u ႕ Sym o
display

◆ program y 65 ႔ assign y ႕ Screen


display ႕ printf() function printf() function t
Output function
◆ statement y conversion symbol %c Output
%c h r t r t output onv rs on sym o
Output value 65 display 65 ႕ ASCII Symbol A
sp y
◆ Statement y %d output integer 65
display ......
ASCII symbol assign ASCII value

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 45

program A h r t r t single quote


Data type h r t r t char
႔ character data type y output %c ႕
%d ႕ character A ႕ ASCII value 65 Output
Sym o ႕v u
ASCII ႕ Value output %d Symbol %c
..........

b) scanf() function
scanf()function Input function User v u
variable ႕ input
value ႕ data type conversion symbol
variable name ႕ ႕ Address operator (&) ႔ nput
t variable name ႕ Address ႔ ႕ v r data
program
scanf() function user 3 ႔ 3 user

◆ program variable name a ႔ a nt r


t typ ႔ Declare
◆ printf() User nput ႕ User
႔ Enter a number
monitor User number

◆ statement user ႕
variable a &a ႔ &

K ZAN HTIKE
46 HOW TO LEARN C PROGRAMMING # VIOLETT

input data memory location ႕


program User 3 ႔ ႔ input data 3 variable a

◆ statement a output User


value 3 output ႔
float data tye sp y

program foat data type sp y a


float ႔ scanf() function ႕ data
%f printf() ႕ output sp y
%f ႔
program user 2.5 ႔ input value output sp y
2.500000 ႔ 2.5 2.5 ႔ limit ႕

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 47

%0.1f ႔ () ႕ limint
႕ ႕ .1 (point 1)
႕ ႕

printf() function ႕ s nf () fun t on data type


conversion symbol Table ႕

Data type Conversion symbol


short integer %d or %i
short unsigned %u
integer
long signed %ld
long unsigned %lu
unsigned hexadecimal %x
unsigned octal %o
float %f or %g
real
double %lf
signed character %c
character
unsigned character %c
string %s

Table data type conversion symbol


Table ႕ t typ
short integer integer int a=2;
short int b=2; ႔ short int
default short data type ႔ Compiler
႕ ႕ range ႕႕႕ -32,768 to 32,768
short unsigned to signed ႕
unsigned
data type long, unsigned long
nt r t typ ႕ ႕ ႔ t typ
on short r un W n tutt ႕
႕ ႕ ႕ short on

short ႔
႕ long ႔
short ႔ m mory ov rfu short

K ZAN HTIKE
48 HOW TO LEARN C PROGRAMMING # VIOLETT

႔ long ႔
m mory w st ႕ ႕ ႕
real
character data type ႕ conversion symbol

C language s p s qu n

Escape Sequence Use


\n New line
\b Backspace
\f Form feed
\‟ Single quote
\\ Blackslash
\0 Null
\t Horizontal tab
\r Carriage return
\a Alert
\” Double quote
\V Vertical tab
? Question mark
s p S qu n ႕ ႕ ႕ ႕ program

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 49

program Escape sequence \t ႕ \n The value


of a=2 output t ႕ ႔ tab
\t output
v u of ႕ \n new line
s p s qu n ႕ ႔
s qu n …

2 ႕ 3 ႕ pro r m

◆ program ႕ pow(argument1,argument2) function


႕ function
function ႕ library <stdio.h>
<math.h> pow() function r thm t fun t on
<math.h> header file m th h

◆ x 2 , y 3 pow(x,y) ႔ ႕ 8
y
Compiler x ႔ 23 8 Output
pow(argument1,argument2) function argument1
base variable argument2 exponent variable
program ......
Relational operator Logical Operator Conditional operator
nput v u iinput value value
True ႔ output False ႔ output

K ZAN HTIKE
50 HOW TO LEARN C PROGRAMMING # VIOLETT

(or)

◆ program variable a ႕ b nput v u ႕


user Enter value of a and b user
v u ႔
variable a ႕ b user a 2 b 1
႔ Statement a ႕ b ႕ 3
Relational operator( a<3,b<3 ) 3
True ႔ output Logical operator (
a<3 && b<3 ) ႔ Conditional
operator ( a<3 && b<3 ) condition u st on m r
Expression sp y is to(:) Expression
sp y user 2 ႕1 Condition
3 ( ) expression
pr ntf(“Tru ”) display printf True Output
User
variable a 4 b 1 ႔ Condition
b 3 a 4
3 && operator True sp y
s to( ) expression output
False output program

user
Addition operator

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 51

program ႕
User input value 2 ႕ 4 ႔ a ႕ b 2 ႕ 4
nput %d,%d ႕ ၇
႔ user a ႕ b comma comma
a omm omm
b omm ႕ a omm
b ..... sum=a+b; statement
additional operator (+) a ႕ b ႔
sum sum ႕
monitor sp y ႕ printf() function Output

The addition of a and b =6 Screen


႔ .....
program ႕ ton op r tor Multiplication operator

K ZAN HTIKE
52 HOW TO LEARN C PROGRAMMING # VIOLETT

program trace Operator

Character Character output

program a
character data type ႔ %c input data a
printf () function ႕ output trace
programming line by line
………
Programming ႕ s n ႔ .…
variable (swap)

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 53

◆ program A ႕ B int
a=7,b=4 a ႕b
◆ pr ntf(“ n ”, , ); a ႕ b output

◆ statement a=a+b; a ႕ b a
a 7 ႕b 4 11
a a 7
11
◆ statement b=a-b; a b a
11 7 b b 4
7
◆ statement a=a-b; ႔ a 11 b
7 4 4 a ႕ assign
a 11 4 pr ntf(“Now n
”, , ); ႔ ၇ a 4 ႕ b
7 Output a ႕b
႕ swapping ႔
႔ …

b) Unformatted functions
Unform tt fun t ons getch() function ႕ getche() function
pro r m

◆ program #include<conio.h> header file


header file current program getche(),getch()

K ZAN HTIKE
54 HOW TO LEARN C PROGRAMMING # VIOLETT

Console input /output function header file


program h r f

◆ statement printf() function ႕ Enter any two alphabets
alphabets ႔ user ႕ user
alaphabet user A ႕B ႔
alphabet A getche() function us r
display alphabet B getch()
function us r sp y
program run A B
႔ t input
/output ႕ pr ntf(), s nf()fun t on onv rs on sym o
… program ႔ …
unformatted function getch() ႕ putch() functions

◆ program variable ch character data type ႔ declare


statement Enter any key monitor
sp y user key
9 ႔ user getch() function
us r scanf () function user
3 ႔ 3 program nt r ny y
9 9 getch() function

◆ ch=getch(); variable ch
ch us r sp y
sp y ႕ putch(argument) function sp y

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 55

printf() ႕ putch() function ႕


႕ putch(argument ) function output fun t on
conversion symbol printf() ႕
output putch() ႕
(parathesis) argument display
program putch(ch) ႔ argument ch
9 sp y program
st t m nt ႕
program
႔ ႔ ......
unformatt fun t on pointer

5. Decision Statements
1. Introduction
program ႕ flow ႕ o op r tors
႔ ႕ program ႕ execution flow ႕ s on st t m nts

St t m nt ႕
1) The if statement
2) The if-else statement
3) The if-else-if statement
4) The switch () statement
1) The if statement
If statement ႕ ႕
Logical operator logical operator expression
if statement tru
f s if statement ႕ syntax

if (condition)
{
Statement;
}

K ZAN HTIKE
56 HOW TO LEARN C PROGRAMMING # VIOLETT

syntax if ႕ ႕ (parenthesis) condition


condition if block statement if
block statement statement program
{} if block ႕ s op {} ႕
St t m nt if block if ႕
pro r m

(or)

if statement ႕ user
user 9 ႔ ႔ 9 varable v if ႕
if(v<10) ႔ variable v
10 ႔
၇ if block statement run if block
statement run ႕ printf() function compiler ႕
print() function output fun t on double quote
sp y ႕ ႔ user 11 ႔
႔ if statement if(v<10)
condition v 11 10
condition if statement ႕ block statement run
program

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 57

႔ START Program
INPUT VALUE OF V Variable v
IS V<10 condition variable v 10
T U MESSAGE S FALSE
MESSAGE program

If statement program

program

K ZAN HTIKE
58 HOW TO LEARN C PROGRAMMING # VIOLETT

◆ Line_2 int main() ႔ program run main() function


r turn ႕ data integer data type ႕
program ႕ return 0 ႔
႕ main () function r turn ႔
compiler pro r m
program ႕
႕ printf() ႕ scanf() function t

◆ Line_7 if(m-n==0) st t m nt variable m
n ႕ 0 ႕ ႔ Condition
user m 5 n 5 ႔
႔ m-n condition
compiler if block statement run
◆ n pr ntf(“Two num rs r qu ”); run printf()
႕ output m-n==0
program output run ox

◆ Line_9 getche() console function


program run
run ox user
output t h ()
user key run box
user key run ox Code
block ႕ run box Visual Studio

run ox process returned output t



K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 59

programming sense ႕ ႔
compiler run run ႔
compiler run program ႕ flow
program
႕ …
If statement pro r m
user ႔ user
၃ you are young
၃ program

(or)

K ZAN HTIKE
60 HOW TO LEARN C PROGRAMMING # VIOLETT

program ႔ 40 ႔ 30
program sp y tr
programming sense …

2) if ...else...statement
control statement if ႕ condition
႕ condition if block
statement if statement program
f… s else block statement
run output ႕ ႕ synt x
if(condition)
{
Statement;
}
else
{
Statement;
}
႕ ႕ syntax ႕ ႕

႔ pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 61

(or)

program ႕ if
statement user Enter number ႔ 10 ႔
႔ a 10 if (a>9) ႔ condition
a 10 9 if condition
if block statement run printf() The number is
greater than 9 output if statement
else block run program
႔ user 4 ႔ ႔ if ႕
a 4 9 condition
program if block statement run else statement run
else block The number is less than 9
output program ႕ run flow
႕ …
႕ program ႔ if-else statement ႕ logical AND
operator pro r m

K ZAN HTIKE
62 HOW TO LEARN C PROGRAMMING # VIOLETT

(or)

◆ Line_5 program Enter number input


variable b
◆ Line_7 if(b>10 && b<20) statement ႕
12 ႔ ႔ if condition ႕ 12>10&& 12<20
12 10 12 20 condition
if statement ႕ block
printf() statement output output The number is
between 10 and 20 sp y user
9 ႔ ႔ If condition ႕ 9>10 && 9<20
9 10 expression
9 20 && (logical AND ) operator
expression ccondition ႔
if statement block run if block else block
statement output Invalid number
display program ႔

pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 63

ro r m
user ၁ v r n m
variable d
user 25 ႔ ႔ value variable s

◆ Line_7 d= s%10 statement s 25 module(%)


2 5 ႕ module
5 d ႕ assign
d
◆ n if(d==5) d 5 ႕
if block st t m nt run statement
s=s/10; s 25 25 10 ႕ Division 2
Division s
25 assign 2
◆ n statement printf (“Squ r ”, s*s++, * ); run
s*s++ s s++ Output
s s 2 2*3
multiply(*) 6 6 output ႕ conversion symbol %d
expression comma(,)

K ZAN HTIKE
64 HOW TO LEARN C PROGRAMMING # VIOLETT

expression d*d d 5 * 25
25 output ႕ %d ( Expression
4 conversion symbol ) monitor sp y
%d%d ႔ 625 ႔ disp y ႔ %d,%d
႔ 6,25 ႔ ႕
25 ႕ 625 output display
◆ ႔ user 20 ႔ ႔ s d=s%10;
statement run 20%10 modular division 0
module statement if(d==5)
if ႕ condition d s 10 ႕ module
0 5 ႕ Equal(==)
႕ Assign(=) Equal (==) ႕
Assign(=)
a=4 ႔ a 4 ႕
assign 4 variable a
႔ ႔
compiler if block statement run
else block statement run printf() function
Invalid number mon tor output display
program ႔
line by line . ....

3. Nested if-else Statement


Nested if-else statement statement Nested if-else
statement ႕ Nested if-else statement if..else f… s statement
statement Statements output
if ႕ else if
statement run else statement execute ( )
႕ ႕ Synt x

if(condition)
{
statement;
}
else if(condition)
{

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 65

statement;
}
else
{
statement;
}

… syntax ႕ ႕ flow ႕ pro r m


Energy bill
pro r m user initial
final
consumed ႕ final initial ႔ consumed
No.of unit consumed
Total bill total bill ႕ consumed No.of units consumed
႕ Rate ႔
energy bill ႔ …pro r m

No.of units consumed rates in (Rs)


200-500 3.5
100-200 2.50
Less than 100 1.50

K ZAN HTIKE
66 HOW TO LEARN C PROGRAMMING # VIOLETT

◆ program USER initial ႕ final ႕


user initial 800 ႕ final 850 ႕ ႔
consumed ႕ final initial
◆ Line_8 consumed=final-initial; 850-800
consumed 50 consumed if ႕
◆ Line_9 if ( onsum && onsum ) ႔
႔ No.of units consumed 200-500 ႕ total energy bill
႕ consumed 200
500 logical AND
if block run else if statement
run
◆ Line_11 ႔ total=consumed*3.5 output
else if ႕ 100-200 ႕ total ႕
if(consumed>=100 && <=200); consumed
50 100 200 else if
run else if run
◆ Line_15 total =consumed*2.500 statement output
else if block else if ႕ less thna
100 ႕ energy bill else if (consumed<100) consumed
100 else if block statement
run
◆ Line_18 total =consumed*1.50; total=50*1.5 run total=75.00
႔ sp y ႕ printf () function pass output
run Tot for s f”, onsum ,tot
sp y %f ႔ float data type ႕ conversion symbol
total sp y ႕ 75.00
program if-else if-else if ႔
progtamming sense ႕
◆ user ၃ output pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 67

◆ Program user ၃ variable a,b,c


comma
scanf() function onv rs on sym o comma

◆ user 3,4,5 ႔ ႔ a 3, b 4 ႕ c 5

Relational operator ႕ Logical operator
၃ if else if else Condition ႔ stage
statement output
◆ if ႕ a b a c a 3 ,b
4 ႕c 5 a if block
run else if run
◆ ႔ condition ႔ A is the largest
sp y st t m nt run program
႔ else if run
if ႕ b a b c b 4

K ZAN HTIKE
68 HOW TO LEARN C PROGRAMMING # VIOLETT

a c 5 b c
logical AND
expression else if block st t m nt
run else run condition
st t m nt else block
statement sp y c is the largest
sp y program ႔ if
else if else ႕ ႔
Nested if pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 69

program user ၃ a,b,c


pr ntf(“ r st of thr num r ”); ႔ ၃ ႕ Largest
of three number:
႕ if else neste if else
if else nested if else
if ႕ a b ႔ outer if ႕ ႔ condition
inner if ႕ a c ႔ user a
10 b 20 c 30 ႔ ႔ outer if ႕ 10
20 if block run outer else run
outer else run else block if ႕ if(c>b) ႔
30 20 inner if block statement output
c=30 ႔ sp y ႔ c b
inner else statement output
b=20 ႔ ႔ Nested if
outer if ႕ inner if ႕ ၇ inner if block
statement output program a=10 ႔
inner else statement c=30 ႔
if ႕ ႔ outer else statement run
outer if sst t m nt output program
outer else if ႕ c=30
႔ inner else statement run
b=20 ႔

K ZAN HTIKE
70 HOW TO LEARN C PROGRAMMING # VIOLETT

program Trace

5.The break statement


Break ywor ywor ႕ break
keyword switch statement ႕ break
႕ program ႕ current flow ႕ ႕
st t m nt break keyword
program ႕
nstru t on run break program
break ႕ switch statement

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 71

6. The continue statement


Continue ywor break ႕
statement continue ႕
st t m nt run looping

7. The goto statement


goto statement C program ႕
႔ program ႕ on t on
႔ ႕
goto ႕ ႕ ႕ syntax
goto label; label goto ႕
႕ program ႔
pro r m

K ZAN HTIKE
72 HOW TO LEARN C PROGRAMMING # VIOLETT

◆ program goto statement ႔


User Enter number ႔
႔ user
3 ႔ ႔ ႕ if ႕ condition

◆ if(x%2==0) ႔ 2 ႕ ႔
0 ႕ user input value
0 ႕ ႔
2 ႕ 0 user 3
႔ 2 ႕ if block
statement run else block statement run output
if block goto statement run output

◆ else block goto statement run goto odd;


satatement run 3 is odd display
oto
statement goto odd
odd statement run
program ႕ ႔
synt x program odd ႔
s to( )
odd: ႔ statement %d is odd
user value ႔ display
3 iss odd ႔ sp y goto even

goto user

pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 73

program ႕ program ႕ 4 ႕ ႔
0 ႕
sp y ႔ 4 ႕ 4
႔ trace programming flow
programming sense goto statement
႔ .....

6. The switch statement


Switch statement
smart home control 1
2 TV 3 opt on
႕ user integer ၇
variable name variable name switch() ႕ parenthesis ()
variable name case constant:

K ZAN HTIKE
74 HOW TO LEARN C PROGRAMMING # VIOLETT

constant ႕ case constant statement


႕ ႕ synt x

switch(variable or expression)
{
case constant A:
Statement;
break;

case constant B:`1


statement;
break;

default:
Statement;
}
pro r m

(or)

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 75

◆ program s onst nt opt ons statement sp y


option 3 us r sp y case constant ႕ number
case constant 1,2 and 3 1 to 3
႔ 1 to 4 number case
statement option ...
◆ user 1 to 3 2 ႔ 2 variable ch
switch(ch) ႔ us r sp y option ႕ switch
႕ parenthesis variable ႕ number ႕ number 2
case 2: option sp y You enter 2 ႕
sp y number 3 case 3: option
statement sp y
◆ user 4 ႔ ႔ switch (ch) variable 4 ႕ case
constant option ႕ case constatement 4 sp y
statement case 1:,case 2:,case 3: options ၃
constant ႔ default statement sp y
Invalid choice ႔6 ႕ case 6: sp y ႕
statement default statement sp y switch ()
႕ ႔
u tor pro r m

K ZAN HTIKE
76 HOW TO LEARN C PROGRAMMING # VIOLETT

◆ program output

pro r m ႕ output nu t t ႕ Menu 1
to 4 sp y ႕ printf() function
program
◆ user nu title Enter Your choice
program pr ntf() fun t on
. user memory ႔
႕ scanf() function
႕ variable name
program ch ႔
◆ user 2 ႔ choice ႔ title 2 ႕ Su tr t on st t m nt
1 ႔ 1 ႕ t on
Calculator ႕f tur s ႕ ႕

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 77

switch() statement program switch()


statement user
2 switch() ႕ parenthesis ႕
case constant
◆ case 2: option title
subtraction statement
user program Enter
two numbers user display

v r n m program user

scanf() function ...
◆ program a ႕ b
a-b ႔ sp y
printf() function program pr ntf(“\n
Su tr t on of n ”, -b); ႔ Subtraction of a and b
=1 sp y .... ႔

switch() statement m nut ,hour, y,month,s on


pro r m program
logic ႕ year days month logic
၁ month year 12
႕ 2 year 12 ႕ 24 month … month
day ႔ ၃၆၅
30 ႕ 360 365 360 year*5 ႕
365 ႕ hour ႕
24 365 24 ႕
8760 minute minute 60
60 8760 ႕ 525600
.. second second 60 525600
60 ႕ ၇ s on 3153600 second user 3
၇ y r 3 ႕ 3
pro r m

K ZAN HTIKE
78 HOW TO LEARN C PROGRAMMING # VIOLETT

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 79

program ႕
display ႕ v r n m
data type long ႔ ႔ ႕ ႕
႕ ႔ user ႕ ႔
nu program 1 to 5 ႕ Exit
tt display program if statement ႕
condition user 0 to 5 Enter
years ႕ case constant ႕
st t m nt 0 to 5
program ႔ program 0 to 5
case constant ႔
user ႕ ႔ Menu 4 ႔
choice 4 ch if statement
႔ Enter years ႕
user ႔
2 ႔ yrs variable
႔ year month, y,hour
program mon=yrs*12; st t m nt
mon * mon 24
variable mon
user 4 ႔ switch(ch) 4
case constant ႕ case 4: ႕ statement run
pr ntf(“\n onths ”,mon); statement run Month=24
sp y mon ႔
႕ program break; statement
break ႕ Month=24 ႔ sp y ႕
program st t m nt run
program ႔ 3 ႕ ႔ 2 year ႔
2 year ႕ hrs=ds*24;

K ZAN HTIKE
80 HOW TO LEARN C PROGRAMMING # VIOLETT

statement
႔0 case 0 run Terminate by choice
program break pro r m
program ႕
႔ …
switch() statement user input value
sp y progr m

program
user input value user 5

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 81

႔ 5 variable x user input value


0 ႔ ႔
sp y 1 ႔ sp y
0 ႕ 1 ႔ 2 ႕ ႔ 0 1
switch() ႕ case constant
0 ႕ 1 sp y ႕ user 5 ႔
outer switch(x) x 5 case constant ႕
program case constant 5 case
constant ႕ ႔ ႕ default statement run default
statement y=x%2 ႔ user value 2
႕ ႕ y ႕ assign y
inner switch(y) ႕ 5 2 ႕ switch(1
) ႕ case constant ႕ program ႕ case 0: ႕
default: case constant 1 st t m nt case 0:
2 ႕ 0 ၁ 0
default statement run Number is odd sp y
႔ 0 case 0: ႕ statement sp y
break program switch() ႕
break statement program ႕

6. Loop control Statements


1. Introduction
Loop control statement program
loop current program va u
assign
statement loop
pro r m Loop control
statement ၃ ႕ ႔
႕ syntax ႔
1) The for loop
2) The while loop
3) The do while loop
1) The for loop
The for loop statement ႕ syntax

K ZAN HTIKE
82 HOW TO LEARN C PROGRAMMING # VIOLETT

for(start;stop;step)
{
Statement;
}
for loop statement ႕ parenthesis () st rt;stop;st p
start user 1 5
၁ condition
expression start ႕ stop
၇ ၁ ၅
stop expression stop xpr ss on
step 1 5
႕ n r m nt, r m nt op r tor
expression
st rt,stop,st p xpr ss ons semicolon(;)
for loop statement ႕

Syntax Output Remarks


1)for(; ;) Infinite loop No arguments
2)for(a=0;a<=20) Infinite loop „ ‟ s n th r n reased nor decreased
3)for(a=0;a<=10;a++) Displays „ ‟ s n r s from to ur y r s
pr ntf(“ ”, ); value are not necessary.Default scope of for
From 1 to 10 loop is one statement after for loop.
4)for(a=10;a>=0;a--) Displays „ ‟s r s from to
pr ntf(“ ”, ); value
From 10 to 0

t 1) for(; ;) statement nf n t oop


႔ start,stop,step expressions
infintet loop
႔ 2) for(a=0;a<=20) nf n t oop
႔ statement step loop
a ႕ ၇ stop expression infinite
loop ႔ ) for( ; ; ++) pr ntf(“ ”, );
st rt,stop,st p xpr ss ons a ႕ 0 10 sp y
sp y start a 0 ႕ assign

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 83

printf() function ႕ a output 0 sp y


for loop statement program for loop
statement run step a increment operator
0 a 1 ႕ aassign 1 sp y
a sp y for loop statement stop 10 ႕ assign
a sp y 4)for(a=10;a>=0;a--) pr ntf(“ ”, );
၃ ႕ start 10 ႕ assign 10 sp y
step ႕ 10-1=9 a ႕ assign 9 sp y
a=0 for loop statement run a
sp y ႔ pro r m

program for(i=1;i<=5;i++) start 1 stop


5 i=1 i 1 i
less than or equal to 5 5 ႕ assign step i++
… for loop run ႕ i=1 printf() function
output Num r of t‟s squ r s sp y
conversion symbol %d i %d i*i expression
1 ႕ square sp y program
stop 5 i=5 loop
for loop run step (i++) i i
2 2 ႕ square sp y i=3,i=4,i=5 loop
program
႔ i stop 5

K ZAN HTIKE
84 HOW TO LEARN C PROGRAMMING # VIOLETT

႕ ( ) 5 ႔ condition test ႔
program ႔ program for loop statement
user 1 5 ႕
႔ 1 5 ၇ output for loop
statement st rt,stop,st p ႕ 1 to 5 ႕ sp y

1 15 sp y ႕ for loop statement

program I start i=1 ႔ ႕ I


႕ printf() function output
for loop sstatement run 1 sp y sp y ႕
program for loop statement run ႔
program run st p ( ++)
i sp y
႕ 1 to 15 loop sp y

user ႕ ၅ sp y

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 85

pro r m
for loop run ႕ printf()
function i sp y program i display
႕ ႕ conversion symbol %d ႕ i i ႕ sp y
double quote Mg Mg sp y

logic pro r m
pro r m 7 to 100 4 ႕ 0 ႕ 5
႕ 6 ႕ user ႕
computer pro r m

… omput r user
o ႕ computer ႕
64 4 ႕ 0 5
႕ 4 6 ႕ 4 computer
programmer 1 ႕ 0
computer Logic program
i=7 ႔ for loop run i
for loop ႕ scope st t m nt run statement if

K ZAN HTIKE
86 HOW TO LEARN C PROGRAMMING # VIOLETT

statement ႕ condition ႔ 7
4 ႕ 3 expression 5 ႕
2 6 ႕ 1 condition test f
if block st t m nt run for loop
for loop statement run
run i 7 99 loop
run if ႕ expression ၃ if block statement
run loop if ႕ ႕ run
condition Answer:64 sp y
program ႔
0 to 15 sp y pro r m

program for oop st m nt ႕ start,stop for


loop ႕ parenthesis step for loop statement ႕ scope statement
႕ for ႕ parenthesis ႔
...program
i=0 run i printf()
function run 0 sp y printf() function run i=i+2
statement run + for loop run
i=2 15 printf() function run 2 sp y 2
2 sp y i ႕ 2 for loop run
15 sp y i=12 sp y i

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 87

2 i<=15 f for loop


statement ႕ scope st t m nt run program

for loop Ascending order ႕ Descendin order

program ႕
ascending program for loop statement
run 0 to 15 loop I stop

K ZAN HTIKE
88 HOW TO LEARN C PROGRAMMING # VIOLETT

i<=15 ႔ test condition for loop st t m nt


run for loop run for loop i 15 ႕ assign
i ႕ sp y
for loop run i i
sp y loop i ႕
i i ႕ 0
sp y for loop run - i ႕ -1 i>=0
႔ test condition program

infinite for loop ႕ goto statement 1 to 5 sp y
pro r m

pro r m statement I 1 ႔ assign


I for(;;) nf n t oop
st rt ,stop,st p for loop
program loop run r ntf(“ ”, ); I sp y 1
sp y i++ statement ႕ i
i 6 ႕ for loop i
sp y i sp y ႕ I
if (i==6) if block statement
run goto stop statement ႔ goto ႕ ႕

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 89

stop label run stop label


statement program program
႔ …
switch () statement ႕ for loop statement ႕
႕ pro r m

K ZAN HTIKE
90 HOW TO LEARN C PROGRAMMING # VIOLETT

program ontro st t m nt ႔
program ႕ user day
st
user 2 ႔ ႔ 1 day ႕ 2nd day sp y
t
4 ႔ 1s to 4th ႕ y
႔ for loop statement for
loop statement 2 ႔ case 2: ႕ statement
sp y
program for loop ႕ user day
႔ 4 ႕ ႔ y for loop
statement ႕ loop for( ; ; ++) i
switch(i) case 1: statement displ y for loop
for loop run i
i switch (i) ႕ case constant st t m nt
sp y for loop statement i<=4 ႔
condition test program

For loop statement ႔ for


loop user pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 91

pro r m user ၃
၃ a
b c sum=a+b+c; statement run
a,b,c sum
user a=1,b=2,c=3 ႔ ႔ sum
၇ for(i=sum;i<=sum;i--) statement run
sum 6 compiler for(i=6;i<=6;i--) ႔ start i
၇ for loop ႕ scope st t m nt run
if(i==a || i==b || i==c) ႔ if ႕ i 6 a,b,c
႕ if block statement
run for loop run step (i--) I
if ႕ a,b,c ၃
႕ ||(logical OR ) operator 3
႕ if block statement run ႕
for loop a,b,c ၃ c ႕
if block statement run i sp y i
3 sp y ... user ၃
3 . ...program o nstru t on
႕ t ၇

program ၃ pro r m

K ZAN HTIKE
92 HOW TO LEARN C PROGRAMMING # VIOLETT

program sense trace


pro r m ၇
႔ pro r m
program ႔

Nested if for loop Nested ႔ Nested for


loop ႕ syntax
for(start;stop;step)
{
for(start;stop;step)
{
statement; } }

syntax pro r m

program ၇ Nested for loop Nested loop ႕


outer for loop ႕ inner for loop outer for
loop run

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 93

program outer for loop run ႕ inner for loop stop


i<=2 condition fail loop run program
for loop run ႕ r=1 r ႕
inner for loop run
inner for loop c=1 ႔ initialize c ႕
sum=r+c statement run 1+1=2 sum
printf() function r,c, ႕ sum ႕ sp y
r 1,c 1 ,sum=2 sp y for
loop statement loop loop
inner loop c<=2 loop
inner loop stop outer loop
inner loop run step(c++) c
sum=r+c run r outer loop run
r 1 sum +
r,c,sum sp y r=1,c=2,sum=3 sp y
c
stop (c<=2 ) ႔ condition test c
3 condtion f outer for loop run step
(r++) r r inner for loop run
inner for loop c 1 ႔ start r , ,sum
sp y inner for loop c
sum + sp y inner for loop c
condition c
condtion f
outer loop run r r condition
r<=2 program inner loop run pro r m
program outer loop r
inner loop c 1 ႕ Outer loop r
inner loop c 1 ႕ outer
loop run ႕ inner loop loop outer loop
run inner loop loop

Nested for loop ႔

K ZAN HTIKE
94 HOW TO LEARN C PROGRAMMING # VIOLETT

2) The while loop


The while loop ၇႕ for loop ႕ syntax
႕ ႕ syntax
start;
while(stop(test condition))
{
statement;
step;
}

႕ ႕ syntax pro r m ႔

pro r m int i =1 ႔ i 1 ႔ start


while(i<=3) statement ႕ condition
while ႕ ႔ ႔
while loop ႕ scope sstatement run
..... condition
printf() function run i 1 sp y step(i++)
႔ i while loop
for loop loop while (i<=3) i
test condition i=2 sp y
condition i 3 sp y i
condition test

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 95

I 1 2 3 sp y ႕ program

while loop 1 to 4 sp y 1 to 4
pro r m

… program program a =1 ႔ r
a =1 sum=0 ႔
႔ 1 to 4 ႕ sum
႔ while loop loop a ႕
program
a 1 ႔ start a while(a<=4)
႔ test ccondition loop a condition
a sp y sum=sum+a; run sum
0 ႕ sum + ; sum ... sum
0 ႔ ႔ ႕
0 ႔ step a++
a
while condtion ႕ a sp y
sum=sum+a run sum , sum + a
condition a sp y sum ႕ loop
sum + a 4

K ZAN HTIKE
96 HOW TO LEARN C PROGRAMMING # VIOLETT

၇ condition test while(a<=4) stop


while loop statement ႕ scope st t m nt run
while loop scope ႕ while loop scope ႕
printf() function run printf() function run sum
sp y ႔ sum ႕
Sum of 4 numbers:10 ႔ sp y .. while
loop statement ႕ ႔

3) The do while loop


The do while loop for loop ,while loop ႕
႕ ႕ syntax

start;
do
{
statement;
step;
}
while(stop)
… ႕ ႕ syntax pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 97

program do while loop ႔ for loop,while loop ႕


start i=1 ႔
Line_3. do condition ႕ run run This is a
program of do while loop sp y
line _8 . i++ i i
line_10. while loop run condition i<=5 i 8
condition do while loop loop program
do while loop ႕ condition
do st t m nt run condition
condition do scope statement loop run
program ႔
do while loop pro r m while
loop pro r m do while loop

Line_4. program start a=1 ႕


Line_5. s=0 user ႔
၃ ႔ 1 to 3 ႔ ႕
program

K ZAN HTIKE
98 HOW TO LEARN C PROGRAMMING # VIOLETT

Line_6. user ႔
Line_7. user 5 ႕ ႔ 1 to 5
5 variable I
Line_8 . do run do ႕ scope st t m nt run
Line_10. printf() a sp y a ႕ 1+ ႔
sp y + ႔ printf() ႕ double
quote ....
Line_11. s=s+a run s=0+1 ႔ s
Line_12. a++ statement ႕ a

Line_14 while(a<=i) condition while(2<=5) ႔


condition do statement
run st t m nt run
s=10+5 s a
while statement condition i condition f
do sstatement run program

7.Arrays
C language Arrary

Line_4. program a 2 ႔ assign


Line_5 statement a 4 ႔ assign a
2 assign 4

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 99

Line_6. printf() function a output 4 sp y

program array ၇ variable


value
v u
value variable name v r
variable Array

One dimensional Array


rr y ႕ program
int a[20];
single dimensional array rr y
v r ႕ assign ႕ ႕ assign

int a[5]={1,2,3,4,5};
declaration ႕ a rr y n m
The bracket([]) variable declaration Array declaration ႔ Compiler
op r tor curly brace({ }) v u
rr y m nt rr y m nt a[5] assign

Element no a[0] a[1] a[2] a[3] a[4]


Element 1 2 3 4 5
Address 2000 2002 2004 2006 2008
Array m nt table

a[0] 1st array element 1


a[1] 2nd array element 2
a[2] 3rd array element 3
a[3] 4th array element 4
a[4] 5th array element 5
႔ element number (the bracket[]) number index
st
႔ index o 1 element index 1 2nd
element ႕ array m nt

K ZAN HTIKE
100 HOW TO LEARN C PROGRAMMING # VIOLETT

index 0 index 1

**** Array int


a[5]={1,2,3,4} ႔ assign ႔ ႕ progr m ႕
array element 3 6 ႔ assign ႔
a[2]=6; ႔ assign index 2 3 index 2
6 ႔ assign index 2 3 6

***** Array nput v u
for(i=0;i<=20;i++)
{
pr ntf(“ nt r m r s”);
s nf(“ ”,&m r s[ ]);
}
program Enter marks user v u for loop
statement o to 20

marks[0]=1
marks[1]=2
…………
………
………
marks[19]=20
user value index 0
႔ 20 ႔ user ႔
marks[0]=20 ႔
***** Array element
rr y m nt variable ႕ data type ႕ size
integer data type memory yt
႔ rr y ႕ size sizeof()
operator ႔
x mp pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 101

program i array integer type variable c


char type array , l long type array ႔ 10 array elements

sizeof() operator memeory bytes
statement sizeof(int) ႔
integer data type ႕ m mory byte sp y
data type byte
႕ sizeof(char),sizeof(long)
sizeof(i) output sp y m mory o t ons ႔
array name i
integer data type array element yt
10 × 40 bytes
sizeof(c),sizeof(l) sp y ႔

h r t r rr y (str n ) output

K ZAN HTIKE
102 HOW TO LEARN C PROGRAMMING # VIOLETT

program „ ‟ ,‟ ‟, „ ‟ ,‟ ‟ , „ ‟ h r t r Array element


႕ character string ႔
str n memory

Array A R R A Y \n
element
Array no. name[0] name[1] name[2] name[3] name[4] name[5]

t ႕ ႔
.. program Array declaration ႕
while loop statement
Line_7. while( name[i]!= '\0' ) ႕ name[i] null (\0) ႕
scope st t m nt run ႔
str n array element string
element ႕ \n(null character) ႕ auto table string
element ႕ h r t r „y'‟ \n ႕
႔ .. program I 0 ႔ assign
n m [ ] while statement ႕ name[0] \n ႕
printf() function run A sp y i
n m [ ] R sp y
i ,n m [ ] \n character ႕
program while statement run program

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 103

1 to 10 ႕
pro r m

program v r n m
for loop statement
Line_8. for(i=1;i<=10;i++) statement i 0 loop
10 ႔ for loop ႕ scope st t m nt run ႔

K ZAN HTIKE
104 HOW TO LEARN C PROGRAMMING # VIOLETT

Line_10. statement if(i%2==0) if ႕ i 2 ႕


0 if block run else block run
i 1 ႔ start 2 ႕ 0 ႕ else
block statement run
Line_16. odd[++b]=i; ႔ b ႔
prefix operator b=-1 ႔ assign
o [ ] ႔ for loop loop
i if ႕
if block statement run
Line_12. even[++a]=i a - +
v n[ ] ႔ rr y m nt 1st
element index 0 ႔ pro r m

i 2 ႕ 0 ႕
else block statement run b b
o [ ] i
0 ႕ if block statement run
else block statement run ႔ even ႕ odd
table

even[0] even[1] even[2] even[3] even[4]


2 4 6 8 10

odd[0] odd [1] odd [2] odd[3] odd [4]


1 3 5 7 9

table ႔ for loop run ႕


program statement run pr ntf(“\n\t Even\t\t ”);
Even ႕ Odd sp y \t Even ႕ Odd
t
statement run for(i=0;i<5;i++) loop
i 0 ႔ start i
even[i] v n[ ] odd[i] o [ ] even[0] ႕
m nt 2 odd[0] ႕ element 1 sp y

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 105

loop i m nt sp y

sume=sume+even[i]; sumo=sumo+odd[i]; st t m nt i
႕ loop
even[0] ႕ 2 ႕ odd[o] ႕ sume=o+2 ႕
sumo=0+1 sume,sumo 0 ႔
even[i] ႕ e m nt sume
odd[i] ႕ m nt sumo
sume=30 ႕ sumo=25 user ႕ Output

printf(“ t on \t\t “,sum ,sumo ); printf() function ႕


display program run
sp y ႔
user
sp y pro r m

program variable name declare


static keyword memory t
data type
program s[15] array str n t typ ႕
str n rr y typ static storage class(or) external storage class

K ZAN HTIKE
106 HOW TO LEARN C PROGRAMMING # VIOLETT

static storage class static


keyword Enter a string
sp y ႔ puts() function
printf() puts() put string ႔ puts ႕
parenthesis() str n t sp y user
ts(s) fun t on scanf ()
function ႕ gets(s) get string ႔ gets ႕
pparenthesis variable (argument )
s array name array name Address
pointer
user str n ႕ address
scanf() ffunction user t
user user HELLO ႕ ႔
HELLO statement puts
function ႕ Reverse String sp y ႔
string data memory t ႕

s[0] s[1] s[2] s[3] s[4]

H E L L O

… program for(i=15;i>=0;i--) st rt
s[15] ႔ compiler s[15]
data sp y step
s[ ] s[14] t
sp y ႕
Loop i s[4]
s[4] ႕ rr y m nt O sp y i
i=3 s[3] ႕ array element L display
i=0 s[0] ႕ element H sp y output m nt
input element ႕ ႔
One Dimensional Array ႔

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 107

Two Dimensional Array


One dimensional array Two dimensional array
program

program Array element 1 to 9 matrix ႕


3 row ,3 column ႕ ႕ one dimensional array row
array column

Column1 Column2 Column3


Row 1 1 2 3
Row2 4 5 6
Row3 7 8 9

{1,2,3} 1 row 3 column


{4,5,6} 2 row 3 column
{7,8,9} 3 row 3column 3 row 3 column

K ZAN HTIKE
108 HOW TO LEARN C PROGRAMMING # VIOLETT

႔ 2row 3column a[2][3]={{1,2,3},{4,5,6}}


႔ ႔

Column1 Column2 Column3


Row 1 a[o][0] a[o][1] a[o][2]
Row2 a[1][0] a[1][1] a[1][2]
Row3 a[2][0] a[2][1] a[2][2]

rr y m nt program
႔ Two dimensional array 1 to 9
sp y Two m ns on rr y m nt
sp y ႕ variable 2 nested for loop
program variable i ႕ j for loop statement
Outer loop for(i=0;i<=3;i++) i
inner loop run inner loop for(j=0;j<=3;j++) j
a[i][j] a[0][0] ႔ compiler
a[0][0] array element 1
a[i][j] display 1 sp y
loop inner loop
႔ inner loop stop outer loop
j
[ ][ ]
element 2 a[i][j] sp y
a[0][1] 2 sp y
a[i][j] [ ][ ] element 3 sp y
i =3 a<3 condition
outer loop run i
inner loop run inner loop j 0 ႔ start

a[i][j] [ ][ ] element 4 display


inner loop j m nt sp y
outer loop i inner loop
element sp y j loop stop
outer loop run ႔ outer loop
stop (i<3) program program
႔ ……

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 109

user input value ႕ ႕


display pro r m

program variable declaration


USER ႕
႔ for(i=0;i<2;i++) scanf() function ႕ ႕ ႕
variable b[i]
user 5 ႕ 4 ႕ ႔ b[o]= 2686648=5 ႕
b[1]=2686656=4 b[0],b[1] r ss 5 ႕4
႕ Address b[0],b[1] sp y 5 ႕4
5 ႕4 Address ႔
statement run for(i=0;i<2;i++)
run i for loop ႕ scope
st t m nt run a[i][j]=b[i]*2 a[0][0]=b[0]*2 ႕
j ႕ 0 ႔ assign b[0][0] ႕ 5

K ZAN HTIKE
110 HOW TO LEARN C PROGRAMMING # VIOLETT

2 ႕ 10 a[0][0] ႕ assign
a[0][0]
statement a[i][j+1]=pow(b[i][j],2)
a[o][0+1]=pow(5,2) ႔ pow function ႕
2
5 =25 25 a[0][1]
for loop statement for loop
loop i i=1
scope st t m nt run a[i][j]=b[i][j]*2
a[1][0]=b[1][0]*2 ႔ 0
b[1][0] 2 ႕ 8 8 a[1][0]

statement a[i][j+1]=pow(b[i][j],2) a[1][0+1] =pow(4,2)


႕ 16 a[1][1]
i i<2 ႔ stop

for loop statement memory

b[0] b[1]
2686648 2686656

a[0][0] a[0][1] a[1][0] a[1][1]


10 25 8 16

program ႔ statement run


Number Addition Square sp y
\t ႕ t
... statement Nested loop ႕ outer loop
for(i=0;i<2;i++) i=0 * b[i] output i
b[0] ႔

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 111

[] ႕ (*) pointer ႔ pointer


b[0] Address ႕ value
b[0] Address ႕ v u 5 sp y
Inner loop run for(j=0;j<2;j++)
printf() function ႕ a[i][j] sp y a[0][0]
10 loop inner loop
a[i][j] output a[0][1]
25 sp y sp y -
5 10 25 ႔
j j=2 condition outer loop
i b[1]
Address ႕ 4 sp y inner loop run
j=0 a[1][0] 8 sp y inner loop J
a[1][1] 16 sp y
sp y -
8 16 inner loop j
condition outer loop run i
on t on f s program
program ႔

Two Dimensional array ႕ ႕ ႕


program pro r m

User defined Functions


fun t ons function predefined functions
(eg. printf(), scanf(),pow()..etc..) ႕ userdefined functions(eg.Myfun(), square(),
sum()..etc) ႕ fun t ons

Why use userdefined functions


Us r fn fun t ons program st t m nt
fun t on ႕
main() function st t m nt

K ZAN HTIKE
112 HOW TO LEARN C PROGRAMMING # VIOLETT

General form of functions


un t ons ႕ ႕ syntax
႕ function ႕ general form
function(argument1,argument2)
type argument1,argument2;
{
Statement;
Statement;
}
function ႕ n r form argument1,argument2
vv r n m r turn v r arg1,arg2
႔ ႔ 2 syntax ႕
r um nt ႕ data type r um nt return
value ႕ return value ႕ us r
fn fun t on program

program line_5. Userdef_fun() user


fun t on user function ႕ printf ()
function output sp y ႕ function
C language Userdef_fun() function

program pro r m run main()


function run ႔ program main() function

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 113

run Line_4 printf() function run run box This


is main function ႔ sp y pro r m run Top to
down run ႔ program line_5 run line_5 run
Userdef_fun () function ႕ program Userdef_fun()
function run
Line_7 Userdef_fun() function run Userdef_fun() function
statement run run box This is user defined
function ႔ sp y ႔
user definef function ၃

program ႕ ႕ program
Line_4 main() function printf() function run sp y
st t m nt Line_5 Userdef_fun() function run
Userdef_fun() Line_9 run This is user
defined function sp y program main()
function statement Line_6 SecUserdef_fun() function run
SecUserdef_fun() function Line_13 This is

K ZAN HTIKE
114 HOW TO LEARN C PROGRAMMING # VIOLETT

second user defined function sp y SecUserdef_fun() function


St t m nt run main() function
Statement Line_7 ThUsedef_fun() function run Line_17
ThUserdef_fun() function run This is third user defined function sp y
program ႔
႕ program User defined function main() function
main() function
User defined function User defined function ႔ pro r m

program Userdef_fun() function main () function


SecUserdef_fun() function Userdef_fun () function
Line_10
ThuUserdef_fun() function SecUserdef_fun() function
Lin_16 program
trace

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 115

Program run user defined function pro r m

program ႕ program run


main ()function run ႕
statement Line_4 Userdef_fun() function run Line_7
Usedef_fun() function statement run Lint_9
SecUserdef_fun() function run program Line_12 ႕ ႕
function st t m nt Line_14
ThUserdef_fun() function run Line_17 ႕ ႕ function
This is third user defined function sp y ThUserdef_fun() function
statement function SecUserdef_fun()
printf() function run sp y SecUserdef_fun()
Userdef_fun() sp y main() function
This is main function sp y … program
႔ …

K ZAN HTIKE
116 HOW TO LEARN C PROGRAMMING # VIOLETT

႕ General form of function function


with arguments ႕ arguments
႕ ႕ pr ntf() ,s nf() fun t on ႕
parenthesis () form t str n arguments ႕ r um nts
parameter ႔ program

program main() function variable a,b,c ႔ user


user 10,20,30 ၃
႔ , ,
Line_7 sum=calsum(a,b,c); statement run calsum()
user fun t on function with argrment
function Line_10 run function
variable name ႕ main() function variable name
variable a,b,c v u
x,y,z variable x , y , z
Variable a,b,c actual arguments ႕ variable x,y,z
formal arguments ႔ actual arguments ႕ formal arguments

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 117

program v r x,y,z a,b,c ႔


v r ႔ v r function
local ႕ function ႔
program globl ႕
... v r name local ႕
function ႔ ႔
x,y,z t typ general form
argument ၃ ႔ data program int
d; ႕ variable d calsum() function local
႕ ႔ variable main() function call
parameter
Line_14 d=x+y+z; ႔ 60
calling program ႕ return statement Line_15
return(d) ႕ return statement ႕ ႕ parenthesis value
n pro r m main() function r turn
calling program sum=60 line_8 sum output
60 display ႔
program variable name function
variable program

program main () function a 30 ႔ assign function


call variable name a ႔ 60 assign a=60 ႔ main

K ZAN HTIKE
118 HOW TO LEARN C PROGRAMMING # VIOLETT

function assign a=30 a 30


assign 60
program main function fun t on fun() user defined
function main function a ႕ user defined function
a ႕ fun() a sp y 60 display main
function a sp y 30 ႔
Return statement pro r m

(Or)

program return statement Line_11 user


alaphabet ႔ nput v u
Line_12 user alphabet user B ႕ Capital
letter ႕ ႔ h
Line_13 if statement ႕ condition f( h && h )
return(ch) statement run

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 119

else statement return(ch+32) run user capital B ႕


ASCII value condition return(ch)
statement run B calling program r turn
Line_5 b=fun(ch); disp y ႕
user small letter b ႔ ႔ small letter b ႕ ASCII value
if ႕ condition 65 ႕ 90 f
return(ch+32) run b ႕ 98 +
130 ႕ ASCII symbol r turn
sp y b @ return statement ႕

C language return statement nt r t
typ r turn pro r m

(or)

program a,b float data type user


float data type user 2
႔ ႔ a=2 ႔ function call
argument x float

K ZAN HTIKE
120 HOW TO LEARN C PROGRAMMING # VIOLETT

Line_14 y=x*x y=2*2 ႔ y


called program r turn return value 4 nt r t typ
r turn 4 called program
Line_7 b=square(a); sp y
sp y format string %f 4.00000 ႔ sp y

user 1.5 ႔ ႔ fo t t typ


line_14 y=x*x run y=1.5*1.5=2.25 return(y) y
r turn return statement nt r t typ r turn
floating point( )
႕ 2 r turn program sp y
2.00000 ႔
r turn

1.50000 =2.25000
႔ line_4 float squar() ႔
square function float data type r turn ႔

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 121

return statement ႔
called function v u return

program go() user defined function return


void keyword main function
return value r um nts void keyword
႕ ႔
႕ return st t m nt v u called function
r turn Call by value ႔ return
statement v u r turn ႔ ႕ ႕ Address r turn
႕ r ss r turn Call by Reference
႔ Call by reference Pointer

9.Pointer
Pointer
Pointer v r declare computer

int i=3 ;
variable i nt r t typ value 3 ႔
C Compiler ၃ ႕

K ZAN HTIKE
122 HOW TO LEARN C PROGRAMMING # VIOLETT

၁ nt rv u ႕ memory space ႕
variable name i memory ႕
၃ variable name i memory 3 ႔

Memory ႕

Computer ႕ memory 6485


value 3 o t on num r( r ss) 6485 value 3 ႕
Address ႕ ႔ ႔ computer ႕ RAM( Random
access Memory ) t Address
႔ t ႕ Address
pro r m value 3 ႕
address 6485 ႔ ႕ pro r m

program printf() function value 3 ႕ ႕ ႕ Address Output


Line_5 value 3 ႕ Address sp y ႕ '„&'
& ႔ scanf() function ႕
႔ & v u Address
႕ Address of operator ႔ pointer
႔ ႔ pointer operator ႕ symbol '„*'
Value at address ႕ pointer operator Address

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 123

value sp y operator
Indirection operator ႔ op r tor ႕ pro r m

.. program ႕ ႔ Line_5
&i ႕ output value 3 Address sp y Line 6
i sp y I value 3 sp y
Line_7 *(&i) ႕ 3 ႕ address
display value 3 sp y indirection operator (*) ႕
value at address &i value sp y ႔
i ႔ sp y *(&i) ႔ value 3 sp y

&i i ႕ address sp y v u v r
n m ႔ r ss v r n m

j=&i;
variable I ႕ address v r variable j
j v r ႕
v u variable variable ႕ address
v r ႔

K ZAN HTIKE
124 HOW TO LEARN C PROGRAMMING # VIOLETT

႔ j=&I ႔ ႕ memory
j v r compiler ႕ memory
location(address) variable i ႕ address
(6485) value 3 variable j ႕ address(3276) variable i ႕
address ႔ ႕ variable j
address ႕ value *j ႔
program

႕ ႔ Line_8 j sp y
J &i ႔ assign i ႕ address sp y
Line_9 &j j ႕ address sp y Line_13
*j sp y value 3 sp y
j=&i ႕ assign *(& ) * operator
value at address address i value sp y
pointer ႔ call by reference
variable ႕ pro r m

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 125

program a=10 ႕ b=20 ႔ assign output


a=20 ႕ sw pp n ( ) ႔
called by reference ႕ Line_6 swap() function
argument a ,argument b &a,&b ႔ declare a,b value
reference(address) line_10 x,y ႕ called
x & ,y &
Line_11 int *x,*y ႔ *(&a),*(&b) ႕
x main function swap(&a,&b) a ႕ address ႕ b ႕
address copy *x,*y ႔ value at
address x ,y *x=10 ႕ *y

Line_14 t=*x; t
Line_15 *x=*y; *x ႔ *y
*x 10 assign value 20

Line_16 *y=t *y
called function *x ,*y a ,b sp y
, call by reference ႕ swapping

K ZAN HTIKE
126 HOW TO LEARN C PROGRAMMING # VIOLETT

Exercise
C language ႕ paradigsm ႔ pro r m
trace Exercise

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 127

K ZAN HTIKE
128 HOW TO LEARN C PROGRAMMING # VIOLETT

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 129

Program Switch statement


႕ pro r m tr
programming sense
႕ ႕ passward
passward ႕
႔ passard program

program ႔ password
program password

K ZAN HTIKE
130 HOW TO LEARN C PROGRAMMING # VIOLETT

program Nested for loop st r(*) sp y ႔


Nested for loop ႔

K ZAN HTIKE
# VIOLETT HOW TO LEARN C PROGRAMMING 131

program
C language ႕ ႔ ႔
programming language ႕

1) Arduino projects ( r u no projects


)
2) ro r mm r C#, Java ႕ High level language

3) W p v op r HTML, CSS, PHP, Java Script, MYSQl


(Meet at the top)

K ZAN HTIKE

You might also like