C-language notes.doc

Embed Size (px)

Citation preview

  • 8/13/2019 C-language notes.doc

    1/53

  • 8/13/2019 C-language notes.doc

    2/53

  • 8/13/2019 C-language notes.doc

    3/53

    5ata types in C language

    The t!pe o) data which a varia"le can store is called its data t!pe. #language supports )ollowing data t!pesC-

    1. %rimar! data t!pes. /erived ; ; ;

    '. =ser de)ined data t!pes

    )6 7rimary data typesThe! are )urther divided into int eger,)loat.dou"le.char void data t!pes.

    a6 Integer data typeIt is used to store integer value. It re4uired "!te memor! space. Integerare declared "! the e!word int. ange is )rom -' *6D to ' *6*. some4uali)iers7uch as short,long,unsigned are used with integer data t!pe can "e used as)ollowsCInt a,",c:

    7hort int 2,!, :Long int 2,!:=nsigned int length:86 'loat data typeIt is used to store )loating point constants. It re4uires 8 "!te memor!space. um"er o) digits a)ter decimal are 6. it is declared "! e!word )loat.ange is "Hw '.8 e -'D to'.8 e 'D.5loat p,r,t:C6 dou8le data typeit is used to represent real num"er in e2ponential )orm. It re4uires D "!tememor! space. ange is )rom 1.* e -'0D to 1.* e '0D. it is declared "!using the e!word dou"le. Long is used as 4uali)ier. /ou"le 2,!:d6 char data type

    '

  • 8/13/2019 C-language notes.doc

    4/53

  • 8/13/2019 C-language notes.doc

    5/53

    A )unction is a "loc o) statements,which do some use)ul wor . It ma!contain a num"er o) statements.c6 7ointer

    A pointer is a varia"le,which points to a location in memor!. It containsthe address o) another varia"le or arra! elemente.g.int iKE:int M:

    MK i:MK $ i& HHAsteri $ & K $1001& KE

  • 8/13/2019 C-language notes.doc

    6/53

    =nion is a data t!pe.it is similar to structure data t!pe. Li estructure,union can also contain mem"ers o) di))erent data t!pes.

  • 8/13/2019 C-language notes.doc

    7/53

  • 8/13/2019 C-language notes.doc

    8/53

    aonsole input output )unctions "&/is input output )unctionsc&%ort input output )unctionsa6Console input output functions

    These )unction receive input )rom the e!word and displa! output onmonitor. 5ollowing console is input output )unction.

    'ormatted function &nformatted functionT!pe input output t!pe input output#har scan)$& print)$& char getch$& putch$&Int scan)$& print)$& int getche$& putchar$&5loat scan)$& print)$& )loat getchar$&7tring scan)$& print)$& string gets$& puts$&

    //;rite a prog. #um ;ith 1 9aria8le.main$&

    int a,",sum,di)): print)$;enter the value o) a ">&:scan)$; d d>, a, "&:sumKaO":di))Ka-":

    print$;sumK dPndi))K d>,sum,di))&:Foutputenter the value o) a "K 0 10sumK'0di))K10

    &nformatted console input-output function

    )6getch>6

    It is used to read a single character )rom e!word. It is non "u))ered)unction. /ata is directl! assigned to varia"le without pressing enter e!.It also maintains the output on screen until we press enter e!.7!nta2:-#Kgetch$&:5or e.gC-+ain$&

    D

  • 8/13/2019 C-language notes.doc

    9/53

    #har ch:%rint)$;enter an! character>&:#hKgetch$&:H read a character )rom e!word H

    %rint)$;t!pe an! charcter>&:#hKgetche$&:%rint)$;u have enteredK>&:%utchar $ch&:Getch$&:H to maintain output on screen HF

    16getche>6It is used to read a single character )rom e!word. It is non "u))ered)unction. /ata is directl! assigned to varia"le without pressing entere!.when !ou enter character )rom e!word , it will "e seen on screen. Itis an echoed )unction. It also maintains the output on screen until we pressenter e!.7!nta2C-# Kgetche$&:5or e.g:-+ain$&

    #har ch:

    %rint)$;enter the an! charcter>&:#hKgetche$&:%rint)$;t!pe an! charcter>&:#hKgetche$&:%rint)$;u have enteredK>&:%utchar$ch&:Getche$&:

    F6

    It is used to read a single charcter )rom e!"oard.it is "u))ered )unction./ata is stored in memor! until u press enter e!. A)ter pressing entere!,data is assigned to the varia"le.

    s!nta2C-cKgetchar$&:)or e.gC-

    9

  • 8/13/2019 C-language notes.doc

    10/53

    main$&

    char ch: print)$;t!pe an! charcter>&:

    chKgetchar$&: print)$;u have entered>&: putchar$ch&:getch$&:F26putchar>6 and putch>6%utch$& and putchar$& print a charcter on screen. The! can output onl! onecharcter at a time.7!nta2C-putch$ch&:5or e.g.-+ain$&

    #har chKQ2Q:%utch$ch&:%utchar$ch&:F46puts>6 ? gets>6The )unction gets$& receive a string )rom the e!"oard. It is a "u))ered)unction. It can iHp multiword.

    7trings also.7!nta2C-gets$ch&:%uts$& is an oPp )unction. It is used to displa! a string on screen. Itautomaticall! appends a new line charcter to the oPp.7!nta2C-puts$ch&:5or eg.+ain$&

    #har ch 8J:%uts$;enter data>&:Gets$ch&:%uts$;(anglore>&:%uts$ch&:F

    865is@ input-output function

    10

  • 8/13/2019 C-language notes.doc

    11/53

    these )unction are used to do input output operations on )lopp! dis orhard dis . These )unctions are per)ormed on )iles.there are man! standardli"rar! )unctions to per)ormed dis or )ile input-output. These )unction can

    "e divided into two categoriesChigh level )ile iPp )unction low lwo level

    iPp )unction.c67ort input ? output functionsthese )unction are used to do input output operation on varia"le ports.

    Identifier:-

    These are the names o) varia"les,)unctions arra!s. It consists o)letters,digits underscore charcter$R&. Identi)ier are o) two t!pes Ce!word user de)ined identi)ier.

    Rules for identifier:-It must "e )rom the charcter set.5irst charcter must "e a letter.7pecial charcter can not "e used.Avoid single charcter identi)iers. =se com"ination o) letters,digits underscore charcter.Length must not "e more than D charcters. 7ome compiles allow 'charcter identi)iers.e!word cannot "e used in user de)ined identi)ier.

    e.gC-si,sum,7=+,aER8.

    Operators:-

    An operators is a symbol that tells the computer to performed certainmathematical or logical manipulation.The various operatore in c are as )ollowC-

    1. Arithmetic operators. Assignment operators

    '. incrementH decrement operators

    8. elational operatorsE. Logical operators6. conditional operators*. 7pecial operatorsD. (itwise operators

    11

  • 8/13/2019 C-language notes.doc

    12/53

    1) Arithmetic operators:- Arithmetic operators are the simplest "asic operators that do arithmetic operations. These operatorsre4uire two varia"le are called "inar! operation.

    Operator Meaning Associati9e

    O Addition L-S - 7u"tract L-S

    +ultiplication L-S H /ivide L-S

    +odule L-S

    16 Assignment Operator:- Assignment perators are used to assign theresult o) an e2pression to a varia"le.The most commonl! used assignment operator isKassignment e2pressionthat ma e use o) these operator are written in the )orm.7!nta2C-Identi)ierKe2pressionOperators eaningK Assign value on ight to Le)tOK L

  • 8/13/2019 C-language notes.doc

    13/53

    These operators are used )or comparing two values depending on theirrelation ta e certain decision.e.gC- 1ES E True8E 1E 5alseOperators +eaning

    Less thanS Greater thanK Less than e4ualSK Greater than e4ualKK 34ual toUK ot e4ual

    46 ogical OperatorsThese operators are re)ered to as (oolean operators. These Operators actupon Operators that are themselves logical e2pression.A)ter testing the value o) condition it gives result,which is either true or)alse.Operators eaning

    A /VV UU T 36Conditional operatorsThe conditional operators $W& is used to carr! out conditional operators. Itcan "e used in placed i)-else statement.7!nta2C-e2pression1W 32pression C e2pression'32pression1 is evaluated )irst. I) is true i.e.value is non ero.thene2pression is evaluated "ecomes value o) conditional e2pression.e.gC-$aS"&W HHaC"BK$!S1 &W HH C800

    //7rogram find the greatest of t;o no. ? y using conditionaloperators.main$&

    int 2,!, :clrscr$&:

    print)$;Pn enter the value o) 2 !K>&:

    1'

  • 8/13/2019 C-language notes.doc

    14/53

  • 8/13/2019 C-language notes.doc

    15/53

    /ecision control statement are used )or decision ma ing. 5ollowinginstruction are used )or decision ma ing.

    ). if statementIt has a condition in "rac ets. I) the condition is true,the statement

    "loc ,)ollowing i) statement is e2ecuted.i) the condition is )alse,statement "loc is not e2ecuted. #ontrol is trans)erred to ne2tinstruction.7!nta2C- i)$condition&5or e2ampleC-main$&

    int mar s: print)$;enter mar sK>&:scan)$; d>, mar s&:i)$mar sSK80&

    print)$; u r pass>&:getch$&:FH/output"nter mar@sE24& r pass

    16 if-else statement:-It has a condition inside i) "rac et. I) the condition is true, the statement

    "loc )ollowing i) statement is e2ecuted. I) the condition is )alse,thestatement "loc which )ollowing else, is e2ecuted.7!nta2C-I)$condition&32ecuted statement1:3lse32ecuted statement :'or e ample:-main$&

    int mar s:

    1E

  • 8/13/2019 C-language notes.doc

    16/53

    clrscr$&: print)$;enter mar sK>&:scan)$; d>, mar s&:i)$mar sSK80&

    print)$;u r pass>&:else

    print)$;sorr!U u r )ail>&:getch$&:Foutputenter mar@sE)sorryF u r fail

  • 8/13/2019 C-language notes.doc

    17/53

    i)$aS"&

    i)$aSc& print)$;largestK d>,a&:

    else print)$;largestK d>,c&:Felse

    i)$"Sc& print)$;largestK d>,"&:else

    print)$;largestK d>,c&:Fgetch$&:Foutputenter a 8 cE)* largestE)*enter a 8 cE )* largestE)*enter a 8 cE )*largestE)*

    26 else-if ladder:- t is decision control statement.

    7!nta2C-I)$condition&7tatement:3lseI)$condition&7tatement:3lseI)$condition&7tatement:3lse7tatement:

    1*

  • 8/13/2019 C-language notes.doc

    18/53

    'or e.g:-main$&

    int mar s:

    clrscr$&: print)$;enter mar sK>&:scan)$; d, mar s&:i)$mar sSK90&

    print)$;A grade>&:elsei)$mar sSKD0&

    print)$;( grade>&:elsei)$mar sSK*0&

    print)$;# grade>&:elsei)$mar sSK60&

    print)$;/ grade>&:else

    print)$;3 grade>&:getch$&:Foutput

    enter mar@sE 4 A grade

    46Conditional operators#-language has two conditional operator. These are W and C The! are alsocalled ternar! operators "ecause the! ta e three arguments.7!nta2C-32pression1W 32pression C 32pression'These are operatos are similar to i)-else statement.'or ".g:-main$&

    int 2,!:clrscr$&:

    print)$;enter the value o) 2K>&:scan)$; d>, 2&:

    1D

  • 8/13/2019 C-language notes.doc

    19/53

    !K$2SEW'C8& print)$;!K d>,!&:getch$&:F

    Outputenter the 9alue of E)*yE&: "rea :case'C

    print)$;wednesda!>&: "rea Ccase8C

    print)$;thrusda!>&: "rea :caseEC

    print)$;)rida!>&: "rea :case6C

    print)$;saturda!>&: "rea :

    19

  • 8/13/2019 C-language notes.doc

    20/53

    case*C print)$;sunda!>&: "rea :de)ault:

    print)$;enter correct da!>&:Fgetch$&:Foutputenter dayE)mondayenter dayEenter correct day

    oop control statement:-

    The concept of repeating the e!ecution of particular block of statementagainst a condition is called as looping.

    7hases of a oop:- "ooping construct have following three phases#). Initiali ation phase

    1. #ondition phase

  • 8/13/2019 C-language notes.doc

    21/53

    int i:)or$iK0:iSK1:i--&

    print)$; d>,i&:

    getch$&:FOutput)* 0 3 4 2 < 1 )

    1. ;hile loop:- n this loop,first the condition is checked then the statement is e!ecuted.#ynta D &'hile(test counter)* +tatement+tatement

    ncrement$-ecrement 'or egD-main$&

    intKi:while$i K10&

    print)$; dPt>,i&:iKiO1:Fgetch$&:FOutput) 1 < 2 4 3 0 )*

  • 8/13/2019 C-language notes.doc

    22/53

  • 8/13/2019 C-language notes.doc

    23/53

  • 8/13/2019 C-language notes.doc

    24/53

    FFgetch$&:F

    Output)) 1) 1 , student iJ MJ&:FF

    print)$;this is what !ou enterd>&:

    6

  • 8/13/2019 C-language notes.doc

    27/53

    )or$iK0:i K':iOO&

    print)$;Pn>&:)or$MK0:M :MOO&

    print)$; dPt>,student iJ MJ&:FFgetch$&:FOutput:-enter the roll no and mar@s) 4 1 * < 2 2 ) 41 *< 22 t;o-dimeentional array can 8e initili ad as gi9en 8elo;int studentJ2KJ1KELL) 4M L1 *M L< 2M L2 MMD

    t;o-dimeentional array can 8e initili ad as gi9en 8elo;int studentJ2KJ1KEL

    L) 4ML1 *M

    L< 2ML2 )**M

    MD

    An array can 8e tra9ersed:- Traversing is to visit each element of arraye!actly once. 'e can traverse an array with the help of loop control

    statements.5or 32ample:-main$&

    int mar s 6JK 10, 0,'0,80,E0,60F:int i:clrscr$&:)or$iK0:i KE:iOO&

    print)$; dPt>,mar s iJ&:getch$&:

    *

  • 8/13/2019 C-language notes.doc

    28/53

    Foutput:)* 1* &:)or$iK0:i KE:iOO&

    sacn)$; d>, mar s iJ&: print)$;elements o) arra! are Pn>&:)or$iK0:i E:iOO&

    print)$; dPt>,mar s iJ&:getch$&:FOutput/:"nter the elements of array )* 1*

  • 8/13/2019 C-language notes.doc

    29/53

    output: sumE1)

    WA7 to find the sum of elements of 1-5 array

    void main$&

    int a 8J 'JK 1, ,',8,E,6,*,D,9,E,*,'F:int i,M,sumK0:

    print)$;the elements o) arra! arePn>&:)or$iK0:i K':iOO&

    print)$;Pn>&:)or$MK0:M K :MOO&

    print)$; dPt>,a iJ MJ&:sumKsumOa iJ MJ:FF

    print)$;PnsumK d>,sum&:getch$&:Foutput: sumE3*

    9

  • 8/13/2019 C-language notes.doc

    30/53

    '&NC$ION

    A function a self contained block of statements that performa specific task. /unction are also called sub programs.

    -ividing a program into functions is one of the ma7or principles of top&down structured programming. sing functions it is possible to reduce the

    si%e of program by calling 0 using them at different places in a program.Thus a comple! problem may be decomposed into a smaller easilymanageable parts or modules called function.The main Advantages o) using areC-

    2asy to write a correct 0 small function. 2asy to reads ,write 0 debug a function.

    2asier to maintain or modify such a function.+mall functions tend to be self documenting 0 highly readable. t can be called any number of times in any place with different parameters.

    +9NTA:#&datatype functionname >datatype arg) datatype arg1 PPPP6L8ody of functionPPPPPPP.PPPPPPP.PPPPPPP.return >e pression6DM

    The first line of function defining (datatype) refers to type ofvalue it would return to the calling portion of the program. Any datatypeas int,float,char etc.

    e.g & int functionname(;;;;;;;) float functionname(;;;;;;;) void functionname(;;;;;;;) WA7 to find the factorial of a num8er

    '0

  • 8/13/2019 C-language notes.doc

    31/53

    Zinclude stdio.hSZinclude conio.hSlong int )actorial $int n&:

    int i:long int prodK1:i)$nS1&)or$iK :i Kn:iOO&

    prod Ki:return$prod&:Fvoid main$&

    int n: print)$;nK>&:scan)$; d>, n&:

    print)$;)actorial is K>, d )actorial$n&&: //calling the functiongetch$&:F

    output: nE4factorialE)1*

    5unction are o) two t!pesC-1. i8rary function:- These )unctions are prede)ined in the compiler.The! are ept in special li"rar! )iles. I) a program needs a )unction , thecorresponding li"rar! )ile is attached to the program during processing. #language has a no. o) li"rar! )unction.

    "ibrary function that carry out standard input output operation. 5ore.gC print)$&, scan)$&, getchar$&,putchar$&,)close,)open etc. the! areinclude a stdio.hS )ile under.

    "ibrary function which carry out of operation on characters. /or e.g#&tolower$c&,toupper$c&. the! are grouped under a ct!pe.hS )ile.

    "ibrary function that a performed operation on strings. /or e.g#&strcmp$&,strcp!$&,strlen$&,strcat$&. The! are grouped under astring.hS)ile.

    '1

  • 8/13/2019 C-language notes.doc

    32/53

    "ibrary function that carry out mathematical calculations. 2.!#& s4rt$&, pow$&,a"s$&. The! are grouped under math.hS)ile.

    . &ser defined functionsD- A )unction, which is de)ined "! the user

    does some use)ul wor ,is called user-de)ining )unction.5or e2C- a hil$&: student$&:etc.

    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[

    &se of functionLarge programs are di))icult to understanding so the! are dividedinto )unctions.

    5unction are eas! to write,understand de"ug./i))erent programs can use a single )unction. "ibrary function can be attached to the program.5unction can "e reused in other programs.

    "ibrary function s save time space. /unction are independent o) s!stem.%rogram design "ecome eas! and program can "e e2ecuted )ast.#oding "ecomes compact with the help o) )unctions./e"ugging o) program "ecomes eas!.

    +emor! is e))icientl! used.%rogram comple2it! is reduced.32ecution speed are increase.=ser de)ining )unction are more )le2i"le.Testing "ecomes eas!.%rogram relia"ilit! is high.

    %o; a function can 8e declared3ver! )unction must "e declared to the compiler "e)ore using it in the

    program.

    returnt!peS datat!peS )uctionnameS$arguments&:

    %o; ;ill you call a fumctionThe )unction can "e called "! giving its name )ollowed "! list o)arguments. Arguments are separated "! commas are enclosed in

    "rac ets. 5or e2C- calsum $a,"&:

    '

  • 8/13/2019 C-language notes.doc

    33/53

    'unction can 8e definedD- in the )unction de)ining contents o) arede)ined.returnt!peS datat!peS )uctionnameS$arguments&:Arguments declaration:

    5unction "od!FHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHZinclude stdio.hSZinclude conio.hSint calsum$int,int&:void main$&

    int a,",sum:clrscr$&:

    print)$;enter the a,"K>&:scan)$; d d>, a, "&:sumKcalsum$a,"&:getch$&:Fint calsum$2,!&:int 2,!:

    int c:cK2O!:return$c&:F

    7rototype:- It is )unction declaration in which we speci)! the data t!pe o)the arguments to "e passed to the called )unction. It is also return t!pe5or e2C-)loat sample$int a,int "&:

    5ifference 8/; arguments ? parameters

    Arguments:- these are variables, which are passed to the function at thetime of function call.

    7arameters:- These are the variables,which are present in the first line of function definition.

    ''

  • 8/13/2019 C-language notes.doc

    34/53

    /i))erence "Hw )ormal actual arguments:-

    'ormal arguments Actual arguments

    These are local to )unction in which the!are de)ined

    The! are glo"al. 7cope is in entire program.

    The! are written in )irst line o) )unctionde)ining.

    The! are written in )unction declaration.

    What is the purpose of main>6 functionThe )unction main$& invo e other )unctions in it. It is the )irst )unction to

    "e called when the program starts e2ecution. %rogram e2ecution alwa!sstarts with the )unction o) main$&.

    What is dummy functionA )unction,which per)orms no )unction, is nown as dumm! )unction. It isa valid )unction. The! ma! "e used as placeholders,which help to add new)unction later on.

    5efference 8/; main>6 ? user define functionmain>6 function user define function

    It is compulsor! )unction in a prog. It is not compulsor!.%rogram e2ecution starts with main$&. It is not true )or user de)ine )unction.+ain$& )unction is called once. There can "e man! user de)ined )unction in

    program.It is in)orms compiler to "egin compilation. It is used to per)orm a particular tas .It can "e the onl! )unction in a program. It cannot "e the onl! )unction in a program.

    Call 8y 9alueIn call "! value a cop! o) value stored in a actual argument varia"les are

    passed to )ormal argument varia"le within the called )unction.

    I) an! change is made in the value using these )ormal argumentvaria"le it will not a))ected the original value stored in actual argumentvaria"leHHT @ to 7wap the value o) two varia"le.

    '8

  • 8/13/2019 C-language notes.doc

    35/53

    Zinclude stdio.hSZinclude conio.hSvoid main$&

    int aK10,"K 0:swap v$a,"&:

    print)$;aK d "K d>,a,"&:Fswap v$int 2,int !&

    int temp:tempK2:2K!:!Ktemp:

    print$;2K d !K d>,2,!&:FoutputD- E1* yE)*aE)* 8E1*

    Call 8y !eference In call "! re)erence )ormal argument varia"le are ta en asre)erence o) actual argument varia"le i) an! change is made in the value

    using these re)erence varia"le will e))ect the original values o) the actualargument varia"le.

    The process o) giving alais name to e2isting varia"le is nown asre)erencing. e)erenc varia"le are created using sign.7rogram:-QincludeRstdio.hSQincludeRconio.hS9oid main>6Lint aE)* 8E1*Ds;ap 9>?a ?86Dprintf>TaE+d 8E+dU a 86DM

    'E

  • 8/13/2019 C-language notes.doc

    36/53

    s;ap 9>int int y6DLint tempDtempE D

    E yDyEtempDMoutput:-aE1* 8E)*VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

    %o; the elements of array can 8e passed on to a function The element o) arra! can "e passed to a )unction "! calling the)unction "! value or "! re)erence. In call "! value we pass the value o)arra! elements to the )unction. In call "! re)erence we pass addresses o)arra! elements to the )unction.HHpassing array elements to a function using call 8y 9alue. main$&

    int i:int num JK 10, 0,'0,80,E0F:)or$iK0:i KE:iOO&

    displa!$int n&: print)$; d>,n&:FOutput:-)* 1*

  • 8/13/2019 C-language notes.doc

    37/53

    Foutput:- )* 1* &:7can)$; d>, a&:5actKrec$a&:%rint)$;)actorialK d>,)act&:FInt rec $int 2&

    int ):

    I)$2KK1&eturn$1&:3lse5K2 rec$2-1&:eturn$)&:FOutput :-"nter any numE2'actorialE12

    !ec>26 return 2 times rec>16!ec>16 return 1 times rec>)6!ec>)6 return )

    Ad9antages of !ecrsion:-

    '*

  • 8/13/2019 C-language notes.doc

    38/53

    with the help o) recrsion, the program code is compact,each to writeand understand.

    %rogram code is compact. It can "e used to solve mathematical pro"lem. =se)ul in multiprocessing and multitas ing. #ompilation time o) the program is less. #an "e used to solve pro"lem o) data structures. It saves memor!.

    5isad9antage

    It ta es more stored space. It is less e))icient. It ma! ta e more time is s ome pro"lems. It re4uires some stopping condition.

    ocal 9aria8le:- The varia"le,which are declared in a )unction, are calledlocal varia"les o) that )unction. The! can "e used in that )unction.32ampleCmain$&

    int a,",sum:aK 0:

    "K10:sumKaO":

    print)$;sumK d>,sum&:getch$&:Foutput sumK'0

    Glo8al 9aria8le:- The varia"le,which are declared "e)ore )unctionde)ition,are called glo"al varia"les. The! can "e accessed "! all the

    )unction in the program.32ampleCint a,":main$&

    int sum:clrscr$&:

    'D

  • 8/13/2019 C-language notes.doc

    39/53

    aK 0: "K10:sumKaO":

    print)$;sumK d>,sum&:

    getch$&:utputCsumK'0 VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

    /i))erence "Hw e2ternal varia"le e2ternal declarationW" ternal 9aria8le definition " ternal 9aria8le declaration

    It is not necessar! to write e!worde2tern.

    It is necessar! to write e!worde2tern.

    7torage space is allocated at a timeo) de)inition.

    7torage space is not allocated at atime o) de)inition.

    It can include assignment o) initialvalues.

    It cannot include assignment o)initial values.

    5or e2ampleCint 2K 0: HHe2ternal varia"le de)initionmain$&

    e2tern int 2: HHe2ternal varia"le declaration print$; d>,2&:F\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

    " plain 9arious storage classes in c languages7torage class o) a varia"le gives the )ollowing in)ormation.-

    %lace where varia"le will "e stored. The varia"le can "e storedeither in memor! or #%= registers.

    The de)ault initial value o) the varia"le. 7cope o) the varia"le. Li)etime o) the varia"le.

    7torage classes are )our t!pes.1& Automatic storage classThe varia"le with automatic storage class has )ollowing propertiesC7torage +emor!

    '9

  • 8/13/2019 C-language notes.doc

    40/53

    /e)ault initial value =npredicata"le value7cope Local to the "loc in which the

    varia"le is de)ined.Li)etime Till the control remain with the

    "loc in which varia"le is de)ined.

    32ampleCmain$&

    auto int i,M: print)$; d d>,i,M&:Futput C 1 1 0?here is 1 1 0 gar"age value.

    16 " ternal storage class:-

    The varia"le with e2teranal storage class has )ollowing propertiesC7torage +emor!/e)ault initial value ]ero7cope Glo"alLi)etime Throughout the program

    " ample: main>6Le tern Dprintf>T+d 6DM

  • 8/13/2019 C-language notes.doc

    41/53

    +ain$&

    Increment$&:Increment$&:

    Increment$&:FIncrement$&

    static int iK1:%rint)$; d>,i&:IKiO1:FutputC 1 '

    26 !egister storage class The varia"le with register storage class has )ollowing propertiesC7torage #%= registers/e)ault initial value =npredicta"le7cope Local to the "loc in with varia"le is

    de)inedLi)etime Till the control remain with the "loc

    in which varia"le is de)ined

    )or e2ampleCmain$&

    register int i:)or$iK0:i K10:iOO&

    print)$; d>,i&:F

    HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

    81

  • 8/13/2019 C-language notes.doc

    42/53

    #$!&C$&!" 7tructure is a collection o) related data items o) di))erentdata t!pes. ne structure ma! contain integer,)loating point andcharacter elements. These elements are called mem"er o) thestructure is similar to a record , )or e2. 5or one student,we

    process data items such as roll num"er,name,su"Mect,mar s

    struct student

    char name 10J:)loat )ees:int roll no:F:struct student s1,s ,s':

    5ata can 8e entered into structure 9aria8les /ata can "e entered into structure varia"le "! using scan)statement and dot operator. /ata can "e displa!ed "! using print)statement and dot operator as shownCmain$&

    struct "oo

    8

  • 8/13/2019 C-language notes.doc

    43/53

    char name:)loat price:int pages:

    int pages:F:struct "oo "1," :

    print)$;enter names,price,and pages o) two "oo s>&:scan)$; c d )>, "1.name, "1.price, "1.pages&:scan)$; c d )>, " .name, " .price, " .pages&:

    print)$;this is what !ou enterd>&: print)$; c d )>,"1.name,"1.price,"1.pages&: print)$; c d )>,"1.name,"1.price,"1.pages&:Fgetch$&:F

    =T%=TC3nter names,price and pages o) two "oo sA 100.00 'E0( E0.00 800This what !ou enterdA 100.00 'E0

    ( E0.00 800

    #tructure element can 8e referenced The mem"er )unction o) the structure can "e re)erred as shownC7tructure varia"le.mem"er The mem"er is written a)ter a dot )ollowing the structure varia"le. Thisdot is called mem"er operator.32ampleC71.name:

    7 .roll no:7'.course

    #tructure containing array ?e can use arra! as structure mem"er.32ample:

    8'

  • 8/13/2019 C-language notes.doc

    44/53

    7truct student

    int roll no:char name 80J:

    int mar s 1 J:F:

    Array of structure ?e can declared arra! o) structures to declare man! structurevaria"le.32ample:7truct e2ample

    Int roll no:#har name 0J:5loat mar s 10J:F:7truct e2ample student 100J:It de)ine an arra! called student,which contain 100 students.

    #tructure ;ith in #tructure A structure can contain one or more structures as mem"ers. It iscalled nesting o) structure.32ampleCstruct date

    int da!:int month:int !ear:F:struct student

    struct student

    struct date doM:char name 0J:int roll no:char course 10J:F:

    88

  • 8/13/2019 C-language notes.doc

    45/53

  • 8/13/2019 C-language notes.doc

    46/53

    Array name representsaddress of starting elements.

    It is a short hand descriptionof the declaration.

    Array cannot have bit fields. Structure may contain bitfields.

    7OIN$"! %ointer is a memor! address varia"le,which points to a location in

    memor!. It contains memor! address o) another varia"le or arra! element.32ampleCint iKE:int M:

    MK i: here i is a varia"le which stored value E at some location inmemor!. Let the address o) this memor! location is 1001. M which is a

    pointer varia"le will store the address o) i. the operator is addressoperator. The statement MK i: tells that M contain the address o) i. %ointervaria"les are declared with the help o) asteris $ &.Asteris $ & means value at address.HE $ i& K $1001& KE.&ses of pointers

    %ointer can access elements easil!.

    %ointer allows us to access a varia"le,which is de)ined outside the)unction.The! reduce the length and comple2it! o) program.The! increase the speed o) e2ecution.=se o) pointer arra! saves the data storage space in memor!.The! can "e used to return multiple data items )rom a )unction.The! can used to represent multidimentional arra!s.5iles can "e handled e))icientl!..%ointers can "e used )or addressing "! using hash techni4ues.

    =se)ul in data structure manipulate.%ointers can "e used as scalar data t!pe or derived data t!pe.Arra! can "e easil! accessed "! using pointer.#all "! re)erence is achieved "! using pointer.#omple2 declarations are possi"le in pointers.

    86

  • 8/13/2019 C-language notes.doc

    47/53

    7ointer 9aria8le can 8e declaration and initiali ed. %ointer can "e declares in same wa! as other varia"les. %ointer can

    "e declared as )ollowCT!pe varia"le name:

    The asteris $ & in)orm the compile that the varia"le is pointer varia"le.The varia"le t!pe re)er to int char,)loat or doudle. %ointer varia"le in)ormthe compiler that it needs a memor! location.

    32ample:Int ptr:

    7ointer can 8e used ;ith array %ointer are closel! lin ed with arra!s. Arra! name is

    pointer to the )irst element o) arra!. ?hen we declare an arra!,compilerallots it su))icient memor! to stored the elements in contiguous memor!locations. 32ample:int a EJ:int ":

    "Ka: here a EJ is one dimentional arra! o) si e E. )irst element o) adeclared as a 0J. Address o) a 0J can "e stored in pointer varia"le ". otherelements o) arra! can "e represented.

    a 1JK $"O1&:a JK $"O &:a 'JK $"O'&:a 8JK $"O8&:let us e2plain this with a program.9oid main>6Lint aJ3KEL4 )* )4 1* 14 6D8EaDprintf>Tarray a contain follo;ing data nU6Dfor>iE*DiR3DiXX6printf>T+d tU >8X)66Dprintf>T n data in reser9e order isU6Dfor>iE3DiR3Di--6

    8*

  • 8/13/2019 C-language notes.doc

    48/53

  • 8/13/2019 C-language notes.doc

    49/53

    gets$str&: ptrKstr:while$ $ptrOi&UKQP0Q&iOO:

    print)$;length o) stringK d>,i&:getch$&:F=T%=T3nter the string Ka hileshLength o) stringKD

    89

  • 8/13/2019 C-language notes.doc

    50/53

    E0

  • 8/13/2019 C-language notes.doc

    51/53

    E1

  • 8/13/2019 C-language notes.doc

    52/53

    E

  • 8/13/2019 C-language notes.doc

    53/53