c lang for learners

Upload: cdteja

Post on 10-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 C Lang for Learners

    1/56

    REVIEW OF C

  • 8/8/2019 C Lang for Learners

    2/56

    Programming language category

    1. Problemorientedor Highlevellanguage:

    Better programming efficiency

    Faster program development

    FORTRAN,BASIC,PASCAL

    Machineorientedor Lowlevellanguages Better machine efficiency

    faster program execution

    Assembly language & machine language

  • 8/8/2019 C Lang for Learners

    3/56

    C is Middle level language

    Relativelygood

    programmingefficiency

    Relativelygood

    machineefficiency

  • 8/8/2019 C Lang for Learners

    4/56

    VARIABLES

    Avariablenameisanycombinationof1to 8 alphabets,digitsor

    Underscores

    Thefirstcharacterinthevariablenamemustbeanalphabet

    Nocommasorblanksareallowedwithinavariablename.

    Nospecialsymbolotherthananunderscorecanbeusedina

    variablename.

    Rules for constructing variables

  • 8/8/2019 C Lang for Learners

    5/56

    SCOPEOFVARIABLES

    LOCAL

    GLOBAL

    Availabilitywithinafunction

    Availabilityoutsideafunction

  • 8/8/2019 C Lang for Learners

    6/56

    C DATATYPES

    PRIMARY DATATYPES SECONDARY DATATYPES

    Character

    Integer

    Float

    Double

    Void

    Array

    Pointer

    Structure

    Union

    Enum

  • 8/8/2019 C Lang for Learners

    7/56

    STORAGECLASS

    AUTOMATIC REGISTER STATIC EXTERN

  • 8/8/2019 C Lang for Learners

    8/56

    AUTOMATIC

    Storage: memory

    Default initial value: garbage value

    Scope : local

    Life : within the block in which it is defined

  • 8/8/2019 C Lang for Learners

    9/56

    REGISTER

    Storage: CPU registers

    Default initial value: garbage value

    Scope : local

    Life : within the block in which it is

    defined

  • 8/8/2019 C Lang for Learners

    10/56

    STATIC

    Storage: memory

    Default initial value: zero

    Scope : local

    Life : value of the variable persists betweendifferent function calls.

  • 8/8/2019 C Lang for Learners

    11/56

    EXTERN

    Storage: memory

    Default initial value:zero

    Scope : global

    Life : as long as the programs executiondoesnt come to an end.

  • 8/8/2019 C Lang for Learners

    12/56

    OPERATORS

    ARITHMATIC

    INCREMENT & DECREMENT

    MODULO DIVISION

    RELATIONAL

    LOGICAL BITWISE

    CONDITIONAL

    ASSIGNMENT

  • 8/8/2019 C Lang for Learners

    13/56

    MEMORY MAP OFANARRAY

    3 4 5 6

    a[0] a[1] a[2] a[3]

    SINGLE DIMENSIONARRAY

    100 102 104 106

  • 8/8/2019 C Lang for Learners

    14/56

    DOUBLE DIMENSIONARRAY

    C A L C U T T A \0

    D E L H I \0

    M U M B A I \0

    C H E N N A I \0

    R[0]

    R[1]

    R[2]

    R[3]

    C[0] C[1] C[2] C[3] C[4] C[5] C[6] C[7] C[8]

    STRING1

    STRING2

    STRING3

    STRING4

  • 8/8/2019 C Lang for Learners

    15/56

    PONTERS

    THE & AND * OPERATORS

    15

    a

    1002

    Location name

    Value at

    location

    address

  • 8/8/2019 C Lang for Learners

    16/56

    ONTER ASSIGNMENT

    int *p; int a=15;

    p= &a;

    15

    a

    1002

    1002

    p

  • 8/8/2019 C Lang for Learners

    17/56

    USING POINTERWITHARRAYS

    3 4 5 6

    a[0] a[1] a[2] a[3]

    100 102 104 106

    P

    a[1] represents(a+1) i.e 102

    *(a+1)=4 eqv. to *p

    int *p

  • 8/8/2019 C Lang for Learners

    18/56

    POINTERTOARRAY

    *P

    p = (int *)malloc(sizeof(int[10]));

    for (i=0; i

  • 8/8/2019 C Lang for Learners

    19/56

  • 8/8/2019 C Lang for Learners

    20/56

    POINTERTO POINTER

    15

    int **p, * q ; int a=15;

    **p=a; q= *p

    *p

    **p *q

    q

    p

  • 8/8/2019 C Lang for Learners

    21/56

    main()

    {char temp[20]= KOLKATA;

    char *s=KOLKATA;

    s++;

    temp++;

    printf(%s%s,s,temp);

    }

  • 8/8/2019 C Lang for Learners

    22/56

    main()

    {int b[]={10,20,30,40,50};int i,*k;

    k=&b[4]-4;

    for(i=0;i

  • 8/8/2019 C Lang for Learners

    23/56

    main()

    {int a[]={2,4,6,8.10};

    int i;

    for(i=0;i

  • 8/8/2019 C Lang for Learners

    24/56

    main()

    {int a[]={2,4,6,8,10};

    int i,b=5;

    for(i=0;i

  • 8/8/2019 C Lang for Learners

    25/56

    main()

    {

    intarr[]={0,1,2,3,4 };

    inti,*ptr;

    for(ptr=&arr[0] ; ptr

  • 8/8/2019 C Lang for Learners

    26/56

    main()

    {

    intarr[]={0,1,2,3,4 };

    inti,*p;

    for(p=arr,i=0 ; p+i

  • 8/8/2019 C Lang for Learners

    27/56

    int a[ ] = {0,1,2,3,4};

    int *p[ ]={a,a+1,a+2,a+3,a+4};

    int **ptr=p;

    printf( %d%d\n,a,*a);

    printf( %d%d %d \n,p,*p,**p);

    printf( %d%d %d \n,ptr,*ptr,**ptr);

  • 8/8/2019 C Lang for Learners

    28/56

    main()

    { int a[ ]={0,1,2,3,4};

    int *p[ ]={a,a+1,a+2,a+3,a+4};

    int **ptr ;

    ptr=p;

    **ptr++;

    printf(%d%d%d, ptr-p, *ptr-a, **ptr);

    *++*ptr;

    printf(%d%d%d, ptr-p, *ptr-a, **ptr);

    ++**ptr ;

    rintf %d%d%d, tr- , * tr-a, ** tr ;

  • 8/8/2019 C Lang for Learners

    29/56

    main( )

    { int n[3][3] = {2,4,3

    6,8,5

    3,5,1 };

    int *ptr= n;

    printf(%d,*(ptr+2));

    printf(%d,(*(*(n+1)+2));

    .

  • 8/8/2019 C Lang for Learners

    30/56

  • 8/8/2019 C Lang for Learners

    31/56

    POINTERTOFUNCTION

    A function has a physical location in memory that can be

    assigned to a pointer.this is an entry point of the function& it is the address used when the function is called.

    int(*cmp)(const char*,const char*);

  • 8/8/2019 C Lang for Learners

    32/56

    POINTERTOFUNCTION

    intmain()

    {chars1[80],s2[80];

    int(*p)(constchar*,constchar*);

    p=strcmp;

    gets(s1);gets(s2);

    check(s1,s2,p);

    }

    voidcheck(char*a,char*b,int(*cmp)(constchar*,constchar*))

    { if(!(*cmp)(a,b)) printf(equal);

    else printf(notequal);}

  • 8/8/2019 C Lang for Learners

    33/56

    STRUCTURESAND UNIONS

    structure contains heterogeneous data types.

    ifferent variables are stored at different places in memory .

    nions are derived data types like structures.

    nion enables to treat the same space in memory as a

    umber of different variables

  • 8/8/2019 C Lang for Learners

    34/56

    ARRAY OF STRUCTURES

    struct student

    { char name[20];

    int marks;

    };

    Struct student st [20]; Keeps record of 20 students

  • 8/8/2019 C Lang for Learners

    35/56

    STRUCTURE POINTERS

    name

    marks

    *st

    struct student

    { char name[20];

    int marks;

    };

    Struct student *st;

  • 8/8/2019 C Lang for Learners

    36/56

    SELFREFERENTIALSTRUCTURE

    struct student

    { char name[20];

    int marks;

    struct student *std;

    };

  • 8/8/2019 C Lang for Learners

    37/56

    DATASTRUCTURE

    Show how data structures are represented in the computer

    Identify linear and nonlinear data structures .

    Manipulate data structures with basic operations

    Compare different implementations of the same data

    structure

  • 8/8/2019 C Lang for Learners

    38/56

    TYPESOF DATASTRUCTURE

    ARRAYS

    LINKLIST

    STACK

    QUEUE

    TREES

    GRAPH

  • 8/8/2019 C Lang for Learners

    39/56

    DATASTRUCTUREOPERATIONS

    SIXOPERATIONS PLAYA MAJORROLE

    1.TRAVERSING:accessing & processingeach record exactly

    once.

    2.SEARCHING:Findingthelocation ofall records whichsatisfy

    one or moreconditions.

    3.INSERTING:addinganew record to thestructure.

    4.DELETING:removinga record from thestructure.

    5.SORTING: arrangingthe recordsinsomelogical.

    6.MERGING:combiningthe recordsintwo differentsorted files

    into asinglesorted file.

  • 8/8/2019 C Lang for Learners

    40/56

    LINKLIST

    CUSTOMER SALESMAN

    Adams

    Brown

    Clerk

    Farmer

    Hill

    Jones

    Smith

    Ray

    Jones

    Jones

    1

    2

    3

    4

    5

    CUSTOMERPOINTER

    Adams

    Brown

    Clerk

    Farmer

    Hill

    1

    2

    3

    1

    1

    1

    2

    3

    4

    5

  • 8/8/2019 C Lang for Learners

    41/56

    SALESMAN PONTER

    JONES1,4,5

    Theset of pointers will changeas customersareadded & deleted

    (dangling pointer)

  • 8/8/2019 C Lang for Learners

    42/56

    customer pointer

    Adams

    Brown

    Clerk

    Farmer

    Hill

    Sales person pointer

    JONES 1

    4

    5

    0

    Keepstheaddressof

    firstpointedrecord

  • 8/8/2019 C Lang for Learners

    43/56

    CIRCULARLINK LIST

    A linked list whose last node points back to the first node

    instead of containing the null pointer,called a circular list .

    HEADERLINK LIST

    It is a linked list which always contains a special

    node,called header node,at the beginning of the list.

    1.Agrounded header list is a list where the last nodecontains the null pointer.

    2. Acircular header list is a list where the last node points

    back to the header node.

  • 8/8/2019 C Lang for Learners

    44/56

    DOUBLELINK LIST

    A linked list, each node having two pointers left & right

    to point previous & next nodes respectively.

  • 8/8/2019 C Lang for Learners

    45/56

    AVAILABLESPACE

    A special list is maintained which consists of unused

    memory cells. This list,which has its own pointer,is called

    the list of available space or the free-storage list or free

    pool.

    GARBAGECOLLETION

    The operating system of a computer may periodically

    collect all the deleted space onto the free-storage list.The

    technique which does this collection is called garbage

    collection.

  • 8/8/2019 C Lang for Learners

    46/56

    problems

    Model

    abstraction

    ABSTRACT DATATYPES

  • 8/8/2019 C Lang for Learners

    47/56

    Abstract data structure

    operations

    interface

    PROPERTIESABSTRACT DATATYPES

  • 8/8/2019 C Lang for Learners

    48/56

    STACK

    PROPERTIES:elementsmaybeinsertedordeletedonlyatone

    end,calledtopofthestack(LIFO order).

    BASIC OPERATIONS:

    PUSH : insertanelementintoastack.

    POP: deleteanelementfromstack.

    1 2 3 4 5 6 7

    AAA BBB CCC

    TOP 3 MAX 7

  • 8/8/2019 C Lang for Learners

    49/56

    POLISH (INFIX)TO REVERSE POLISH (POSTFIX)

    CONVERSION

    1.insert (intostack )attheendoftheinfixexpression.

    2.scantheexpressionfromlefttoright&repeatstep3to 6 until

    stackisempty.

    3. Ifanoperandisencounteredthenwritethatoperandontothe

    postfixstring.

    4. Ifa( isencounteredthenpushitontostack.

    5.ifanoperatorisencounteredthenrepeatedlypopfromstackthoseoperatorswhoseprecedenceisgreaterorequaltothe

    precedenceofencounteredoperatorsandwritethemonthe

    postfixstring.

    6.if)isencounteredthenrepeatedlypopfromstack&writethem

  • 8/8/2019 C Lang for Learners

    50/56

    INFIX TO PREFIX CONVERSION

    1.insert )intostack (attheendoftheinfixexpression.

    2.scantheexpressionfromrighttoleft&repeatstep3to 6 until

    stackisempty.

    3. Ifanoperandisencounteredthenwritethatoperandontotheprefixstring.

    4. Ifa) isencounteredthenpushitontostack.

    5.ifanoperatorisencounteredthenrepeatedlypopfromstack

    thoseoperatorswhoseprecedenceisgreaterorequaltothe

    precedenceofencounteredoperatorsandwritethemonthe

    postfixstring.

    6.if(isencounteredthenrepeatedlypopfromstack&writethem

    onthepostfixexpressionuntilthe ) isencountered.

  • 8/8/2019 C Lang for Learners

    51/56

    EVALUATIONOFA POSTFIXEXPRESSION

    1.insert # attheendofthepostfixexpression.

    2.scanpostfixexpressionfromlefttorightandrepeatstep3to4

    until # isencountered.

    3. Ifanoperandisencounteredthenpushitontostack.

    4.ifanoperatorisencounteredthenpopthetopelement&nextto

    topelementandperformoperationwithencountered

    operation.pushresultontothestack.

    5.topelementofthestackgivestheresultofthepostfixexpression.

  • 8/8/2019 C Lang for Learners

    52/56

    EVALUATIONOFA PREFIXEXPRESSION

    1.insert # atthebeginning oftheprefixexpression.

    2.scanprefixexpressionfromrighttoleft andrepeatstep3to4

    until # isencountered.

    3. Ifanoperandisencounteredthenpushitontostack.

    4.ifanoperatorisencounteredthenpopthetopelement&nextto

    topelementandperformoperationwithencountered

    operation.pushresultontothestack.

    5.topelementofthestackgivestheresultoftheprefixexpression.

  • 8/8/2019 C Lang for Learners

    53/56

    QUEUE

    PROPERTIES:elementsmaybeinsertedonlyatoneend&

    deletedonlyattheotherend (FIFO order).

    BASIC OPERATIONS:

    REAR : insertanelementintoastructure.

    FRONT : deleteanelementfromstack.

    1 2 3 4 5 6

    AAA BBB CCCREAR : 3

    FRONT :1

  • 8/8/2019 C Lang for Learners

    54/56

    DEQUEUE

    linearlistinwhichelementscanbeaddedorremovedateither

    ndbutnotinmiddle.

    equeismaintainedbyacirculararraywithpointers LEFT&IGHT,whichpointtothetwoendsofthequeue.

    Twovariations ofdeque:

    Inputrestricteddeque: whichallowsinsertionsatonlyoneendofthelistbutallowsdeletionsatbothendsofthelist.

    Outputrestricteddeque:whichallowsdeletionsatonlyoneend

    ofthelistbutallowsinsertionsatbothendsof thelist.

  • 8/8/2019 C Lang for Learners

    55/56

    AAA BBBLEFT : 3

    RIGHT:4

    1 2 3 4 5

    1 2 3 4 5

    BBB CCCDDDLEFT : 4

    RIGHT:1

  • 8/8/2019 C Lang for Learners

    56/56