Pic 1 To 3 Chaptersslide Shows 1-59

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

By Prof. U. N.

Abhonkar
Department of Computer
Sandip Polytechnic


Chapter 1
Basics of 'C'

Program
A computer program is a set of instructions, executed step by step
ExampIe: Addition of two numbers
In this exampIe steps wiII be:
1) Take two numbers
2) Add these two numbers
3) DispIay the addition
These instructions are executed step by step

Programming Language
A program is written using a programming language
Example: C,C++,VB,Java etc.
There are two tpes o! programming languages:
"# $igh %evel %anguage: human un&erstan&a'le,
contains English li(e wor&s, user !rien&l.
)# %ow %evel %anguage: un&erstan& ' computer,
'inar language, machine language

How Computer Understands Program ?
There are two was through which a computer can
un&erstan& the program i.e. a program is execute&,
". *nterpretation:

*n interpretation ever single line o! program is execute&.

+eans line ' line execution.

*nterpreter interprets the program.

,- (e is use& to interpret a C program.


). Compilation:

*n compilation the whole program is compile& at a time


an& then it is execute& i.e. run.

Compiler compiles the program.



CompiIation Steps

.ource Co&e
Compiler
/'0ect Co&e
%in(er
Executa'le
( .c fiIe ) 1our actual program written in $igh level language
Converts high level language co&e into low level language co&e
( .obj fiIe) %ow level language co&e2 'inar co&e2 o'0ect co&e
"# %in(s alrea& compile& mo&ule into single mo&ule
)# %in(s C stan&ar& li'rar !unctions to our program
( .exe fiIe ) Executa'le program

History of 'C'
,irst three languages ha& 'een use& mainl
". BA.*C : use& !or 'eginners.
). C/B/% : use& !or commercial purposes.
3. ,/4T4A5: use& !or engineering purposes.
Year Language DeveIoper Features
1960 ALGOL InternationaI Committee Too generaI
Too abstract
1963 CPL Cambridge University Hard to Iearn
1967 BCPL Martin Rechard CouId deaI with any specific
probIem
1970 B Ken Thompson CouId deaI with any specific
probIem
1972 C Dennis Ritchie Combined features of B and
BCPL

Where 'C' stands
C is a high level programming language
But some times it is calle& as +i&&le %evel
programming language, 'ecause
"# *t shows !eature o! high level language 6
$uman un&erstan&a'le, contains English li(e
wor&s.
)# *t shows !eature o! %ow level language 6
*nteracts with computer.

Program Learning

Alpha'ets
7igits
.pecial
.m'ols
Constant
Varia'le
8ewor&s
*nstructions 9rogram

Character Set
A character &enotes an alpha'et, &igit or a special
sm'ol use& to represent in!ormation
Alpha'ets
7igits
.pecial .m'ols
a, ', c,......, x, , :
A, B, C,....., ;, 1, <
=, ", ), 3, .... >
~ ! ` @ # % ^ & * ( ) - _ +
= / | \ { } [ ] : ; ' < > , . ?

Tokens
To(ens are the 'asic elements recogni:e& '
compiler
*n ?C? there are six tpes o! to(ens availa'le,
To(ens To(ens To(ens To(ens To(ens To(ens
/perators
.pecial .m'ols
Constants
.tring
*&enti!iers
8ewor&s

Constants
A constant is a @uantit that &oes not changeA
A constant can 'e store& at a location in
memor
Example,
3x + ) B )=
$ere 3 ,) an& )= can not 'e change& hence
calle& as constants.

Types of constants

C constants
.econ&ar constants 9rimar constants
Integer
constant
ex. )3C
ReaI /FIoat
Constant
ex. C3D.DE-
Character
Constant
ex. ?'?
Arra
9ointer
.tructure
Fnion

VariabIes
A varia'le is an entit that ma changeA
A varia'le can 'e consi&ere& as a name&
memor location where an constant value
can 'e save&
Example, 3x + ) B )=
here values o! x an& ma change or var
hence calle& as varia'les.

DecIaration of variabIe
To &eclare an varia'le the sntax is,
data_type variabIe_name ;

Example, to &eclare a varia'le x o! tpe integer,


int x ;
$ere a memor location will 'e allocate& an& its name
is x
X
1110

RuIes for constructing variabIe name

An com'ination o! alpha'ets an& &igits

,irst character must 'e alpha'et

Commas or 'lan( spaces are not allowe&.

.pecial sm'ols are not allowe&.

/nl un&erscore G H # is allowe&.

8ewor&s are not allowe&.

Case sensitive ex, A4EA an& area are &i!!erent


varia'les

keywords
8ewor&s are the reserve& wor&s in ?C? whose meaning
has 'een alrea& explaine& to the compiler.A
The (ewor&s can not 'e use& as varia'le name.
There are 3) (ewor&s availa'le in ?C?
auto doubIe int struct
break eIse Iong switch
case enum register typedef
char extern return union
const fIoat short unsigned
continue for signed void
defauIt goto sizeof voIatiIe
do if static whiIe

Data Types
7ata tpe speci!ies which tpe o! &ata are A

7ata has &i!!erent categories which are calle& as &ata


tpes.

7i!!erent &ata tpes:


"# 9rimar &ata tpes:
a# *nteger G int # : Ex. 3)3
'# 4eal 2 ,loating point G !loat #: Ex. C)3C.DE
c# Character G char #: Ex, g
)# 7erive& &ata tpes: Arras, structures, strings etc.
3# Fser &e!ine& &ata tpes: 7ata tpes &e!ine& ' user.

Maximum VaIues of Data Types

Data Type Size Range
char or signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
int or signed int 2 bytes -32767 to 32767
unsigned int 2 bytes 0 to 65565
short int or
unsigned short int
1 byte -128 to 127
Iong int or
unsigned Iong int
4 bytes 0 to 2147483646
signed Iong int 4 bytes -2147483647 to 2147483646
unsigned Iong int 4 bytes 0 to 424967295
fIoat 4 bytes 3.4e -38 to 3.4e +38
doubIe 8 bytes 1.7e -308 to 3.4e +308
Iong doubIe 10 bytes 3.4e -4932 to 1.1e +4932

Operators
/perator is a sm'ol that &enotes operation on operan&sA
Tpes o! /perators:
". Arithmetic /perators
). %ogical /perators
3. 4elational /perators
C. *ncrement 2 7ecrement /perators
D. Bitwise /perators
E. Assignment /perators
-. Con&itional /perator
I. .pecial /perators

1. Arithmetic Operators
These operators are use& to &o mathematical operations
+ A&&ition
J .u'traction
2 7ivision
K +o&ulo &ivision Gmo&ulus#

2. LogicaI Operators
These operators com'ine two or more con&itions into one
con&ition
LL A57
MM /4
N 5/T
Truth Ta'le:
p q p && q p || q ! p
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0

3. ReIationaI/ Comparison Operators
These are use& to compare two values or varia'les
O %ess than
P Qreater than
O B %ess than E@uals to
P B Qreater than E@uals to
B B E@uals to
N B 5ot E@uals to

4. Increment / Decrement Operators
*ncrement operator increment value o! operan& ' "
Ex, a + + will increment value o! varia'le a ' "
a + + is e@uivalent to a B a + "
7ecrement operator &ecrements value o! operan& ' "
Ex, a J J will &ecrement value o! varia'le a ' "
a J J is e@uivalent to a B a R "

9reincrement an& 9re&ecrement operators:


,irst value o! operan& will 'e increase& 2 &ecrease& ' "
an& then that value will 'e use& in the expression.
Ex, 9reincrement: + + a 9re&ecrement: J J a

5. Assignment Operators
The assignment operator B is use& to assign
a value to a varia'le.
Ex, int i B DS
This will assign value D to varia'le i

7i!!erent assignment operators:


Ex, a + B ' is e@uivalent to a B a + '
a J B ' is e@uivalent to a B a R '
a T B ' is e@uivalent to a B a T '
a 2 B ' is e@uivalent to a B a 2 '

6. Bit wise Operators
These are use& to shi!t the actual 'its in a wor& or a
'te.
L Bitwise A57
M Bitwise /4
U Exclusive /4 G E;/4 #
V /ne?s Complement Truth Ta'le !or E;/4
OO %e!t .hi!t
PP 4ight .hi!t
p q p ^ q
0 0 0
0 1 1
1 0 1
1 1 0

7. ConditionaI ternary operator
This operator chec(s an expression an& &epen&ing
on the status o! expression evaluates the result
*t uses com'ination o! W : sm'ols.

.ntax: exp1 ? exp2 : exp3


Ex, a > b ? a : b
This will !irst chec( con&ition a P ', i! it is true result
will 'e a an& i! it is !alse result will 'e '.

This operator operates on three operan&s hence


calle& as ternar operator.

8. SpeciaI Operators
.pecial operators are use& to &o some special operations A

L operator:
L means the a&&ress o!
Ex, n is a varia'le, then
&n will give the a&&ress o! varia'le n

T operator:
T means at the a&&ress
Ex, n is a varia'le having value 3 i.e n B 3
p is a varia'le storing a&&ress o! n i.e p B Ln , then
T p will give value 3 i.e. value at the a&&ress p



n
1011
3
p
1011
After p = &n
After w = *p
w
3
After n = 3

Precedence and Associativity of Operators

9rece&ence stan&s !or priorit.

/ne expression can inclu&e multiple operator !or the


purpose o! calculation.

*n this situation compiler !ollows certain rules.

These rules speci! how to solve expression an& which


operators to 'e operate& !irst.

Accor&ing to priorit set, the expression gets execute&.

Each operator is associate& with its prece&ence.

$ierarch o! commonl use& operator is shown 'elow,


Priority Operator
1
st
* ? %
2
nd
+ -
3
rd
=

Expression

An expression is an vali& com'ination o!


operators, constants, !unctions an& varia'les

Ex, c B a T ' S

Thus an expression consists o! operators an&


operan&s or varia'les .

Structure of 'C' program

Documentation Section: consists of set of comment Iines. It is ignored by the
compiIer.
Link Section : header fiIes are decIared here.
Definition Section : defines aII symboIic constants used in program
GIobaI DecIaration Section : decIares variabIes used in more than one function
main() function section : program execution starts with main() function.
{
DecIaration Part : DecIares aII variabIes used in execution part
Execution Part : contains set of statements
}
Subprogram section: contains user defined functions


function1()
function2()
function n()

Formatted Input
,ormatte& input means rea&ing &ata in !ormats which are &esire&
' the userA

?C? provi&es scanG# !unction which scans or rea&s !ormatte&


input.

Qeneral .ntax:
scan!GXcontrol stringX, arg", arg), arg3...argn#S

$ere control string will 'e &ata tpe speci!ier an& arguments will
'e the varia'les where the &ata is to 'e store&.

Comma separates the control string an& arguments.

The L is use& 'e!ore varia'le name.

Example, scan!GXK&K!X, Ln, Lm#S




Data Type Specifiers for scanf()

Data Type Specifier Meaning
%d Reads DecimaI Integer
%f Reads FIoating point vaIue
%c Reads Character
%s Reads String
%o Reads OctaI Integer
%x Reads HexadecimaI Integer
%i Reads DecimaI, HexadecimaI, OctaI Integers
%h Reads Short Integer
%u Reads Unsigned integer
%I Reads Long Integer

Formatted Output

?C? provi&es print!G# !unction use& !or printing


message an& numerical values o! varia'les.
"# print!G# !or printing message:
Qeneral .ntax:
print!GXcontrol stringX#S

$ere control string ma 'e a character, a string or a


message that ou want to 'e printe&.
Example: to print the message X* am *n&ianX,

print!GX* am *n&ianX#S


)# print!G# !or printing numerical values o! varia'les:
Qeneral .ntax:
print!GXcontrol stringX, arg", arg), arg3...argn#S

$ere control string will 'e &ata tpe speci!ier an&


arguments will 'e varia'les, whose value is to 'e
printe&.

Example: To print value o! varia'le aB"= an&


'BCD.-E,
print!GX K& K! X, a, '#S

Data Type Specifier for printf()

Data Type Specifier Meaning
%d Prints DecimaI Integer
%f Prints FIoating point vaIue
%c Prints Character
%s Prints String
%o Prints OctaI Integer
%x Prints HexadecimaI Integer
%i Prints DecimaI, HexadecimaI, OctaI Integers
%h Prints Short Integer
%u Prints Unsigned integer
%p Prints Pointer

BacksIash Characters

?C? provi&es 'ac(slash characters !or !ormatting the


output.
Bac(slash characters are use& in printG# statement
!or changing !ormat o! output

7i!!erent Bac(slash characters in ?C? are,


BacksIash Character Meaning
\n New Line
\t HorizontaI Tab
\v VerticaI Tab
\b Backspace
\ DoubIe Quote
\' SingIe Quote
\\ BacksIash

A SimpIe 'C' program


CompiIation (AIt F9)


Execution (CtrI F9)


RuIes for writing 'C' program

A ?C? program alwas starts with &eclaration o! hea&er !iles.

Ever in&ivi&ual statement must en& with a semicolon G S #


except loop

Ever ?C? program must have mainG # !unction.

?C? is case sensitive. Everthing shoul& 'e in lowercase.

Ye can use uppercase !or creating varia'le or constant name.

Extension o! ?C? program must 'e .c G Ex. program".c#

Ever !unction must 'e use& with parentheses G #

Yhile initiali:ing varia'le, the a character will 'e close& in single


@uotes G Ex. Char g B ?h ? # an& a string woul& 'e enclose& in
&ou'le @uotes G Ex. Char nameZ"=[ B computerA#

Steps to Write and Run 'C' program
". 4un TC.exe.
). Clic( on ,ile menu an& select 5ew.
3. Tpe our C program in e&ition win&ow o! TC so!tware.
). .ave it with ,). Qive &esire& !ile name with .c extension.
3. Compile program with Alt ,>.
C. *! an error occurs, remove it, save program an& compile
it.
D. *! .uccess message comes, execute program with Ctrl ,>.
E. A!ter execution, console screen will open.


FIowchart
,lowcharts are the &iagrams use& to show !low o!
execution o! program

7i!!erent &iagrams use& in ,lowchart are,


.TA4T 2 .T/9
*59FT 2 /FT9FT
QE5E4A%
94/CE..*5Q
7EC*.*/5
C/55ECT/4





































OraI Questions
". 7e!ine program.
). $ow a program is execute& W
3. Yho &evelope& ?C? an& in which ear W
C. Yhat is high level language an& low level language W
D. Yhat is a Compiler W
E. 7e!ine Constant.
-. Yhat are the tpes o! constants W
I. 7e!ine varia'le.
>. Yhat are to(ens W
"=. Yhat are (ewor&s W
"". %ist 'asic &ata tpes.
"). Explain structure o! ?C? programW

"3. $ow man 'tes are re@uire& to store integer,!loat an&
character &ataW
"C. 7e!ine /perator. Yhat are tpes o! /peratorsW
"D. .tate Arithmetic operators.
"E. .tate %ogical operators.
"-. .tate Bitwise operators.
"I. .tate special operators.
">. Explain con&itional ternar operator.
)=. Explain assignment operators.
)". Explain increment 2 &ecrement operators.
)). Explain !ormatte& input.
)3. Explain !ormatte& output.
)C. %ist &i!!erent 'ac(slash characters.
)D. Yhat is the use o! clrscrG# an& getchG# !unctions W

You might also like