+ All Categories
Home > Documents > Review of c Concept

Review of c Concept

Date post: 29-May-2018
Category:
Upload: kusuma-sanjeev
View: 215 times
Download: 0 times
Share this document with a friend

of 33

Transcript
  • 8/9/2019 Review of c Concept

    1/33

    1Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    2/33

    DATA TYPES:

    Definition : Thetypeof thedatathata variable can store in the memory can bedefined using data types.

    Significance :1. Memory allocation is done according to the

    data type

    2. Permissible operations3. Range of data

    2Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    3/33

    DATA TYPES (CONTD.) :

    DATA TYPES

    PRIMARY DERIVEDUSER

    DEFINEDEMPTYPRIMARY DERIVED

    3Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    4/33

    DATA TYPES (CONTD.) :

    Primary type :int , float , double, char

    Derived type : (derived from primary)

    arrays , pointers

    User-defined :

    structures , unions

    Empty :

    void 4Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    5/33

    DATA TYPES (CONTD.) :

    QUALIFIERS

    SIGN

    SIZE

    SIGN QUALIFIERS : Signed , unsigned (for primary

    d a t a t y p e s )

    SIZE QUALIFIERS :Short , long

    5Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    6/33

    DATA TYPES (CONTD.) :

    PRACTICE PROGRAM : To add 2 numbers

    #include

    void main( )

    {

    int a , b , sum ;

    printf(Enter 2 numbers \n );

    scanf(%d %d ,&a , &b);

    sum= a +b ;

    printf(Sum = %d , sum );

    }

    CASE1 :

    If input is 123 , 24

    Output : 147

    CASE2 :If input is

    123567+24111

    Program does not

    work as input is out of therangefor

    integer data type

    6Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    7/33

    VARIABLES :

    Definition : Identifier whosevaluecanbe changed during program execution

    Remember :

    1. Important to choose name of variable

    appropriately.(useful in long programs)

    2.Choose variable name as per the rulesdefined

    7Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    8/33

    OPERATORS :TYPES

    ARITHMETIC

    ASSIGNMENT

    Increment/Decrement

    RELATIONAL

    LOGICAL

    CONDITIONAL

    BITWISE

    SPECIAL 8Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    9/33

    OPERATORS (CONTD.):Arithmetic: + , - , / , * . %

    Assignment : =,{+= , *= etc ->shorthand operators)}

    Increment/Decrement : ++ , --

    Relational : < , , >= , == , !=

    Logical : ! , && , | |

    Conditional : ?

    Bitwise : ~ , > , & , ^ , |

    Special operators : ,(comma) , sizeof , & (address

    operator)

    9Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    10/33

    OPERATORS (CONTD.):

    IMPORTANT: PRECEDENCE AND

    ASSOCIATIVITY

    Simple examples to demonstrate the above :

    if a=2 , b=5 , c=3 , m=8

    1. a+b+c = 10

    L to R associativity for same operatorThus, ((a+b)+c)= ((2+5)+3)= (7+3)= 10

    10Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    11/33

    OPERATORS (CONTD.):2. a+b*c=17

    Precedence of * operatorThus , (a+(b*c)) = (2+(5*3)) = (2+15) = 17

    3 . d= a >b ? c > m? a : b : c

    R to L associativity for conditional operator

    Thus , d = (a>b?(c>m?a:b):c)= (2>5?(3>8?2:5):3)

    = (2>5?5:3)

    d = 3 11Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    12/33

    OPERATORS (CONTD.):

    WHAT IS THE OUTPUT ??????

    i=5;

    printf ( %d , %d , %d , %d , i++ , i , i++ , ++i );

    7 , 7 , 6 , 6 OR 7 , 8 , 6 , 6

    (R to L execution) (PRECEDENCE OF UNARY(++) OPERATOR )

    NOTE : COMPILER DEPENDENT 12Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    13/33

    Set of similar elements

    Homogenous

    Continuous memory

    Derived data type

    ARRAY :

    KEY POINTS :-

    13Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    14/33

    ARRAY (contd .):

    TYPES

    1-DIMENSIONAL 2-DIMENSIONAL

    SYNTAX :

    datatype arrayname[size];

    Size should be a constant

    For eg : int arr[10]; 14Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    15/33

    STRUCTURES AND UNIONS :

    SIMILARITY :1.User-defined

    2.Collection of related items

    3.Heterogenous in nature

    DIFFERENCES :1.Memory allocation for variable

    2.Memory access15Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    16/33

    INPUT AND OUTPUT :

    DATA FLOW

    INPUT OPERATION OUTPUT OPERATION

    (Data flows from

    the externaldevice into the

    program)

    (Data flows from

    the program tothe external

    device)

    (Data flows from

    the externaldevice into the

    program)

    (Data flows from

    the program tothe external

    device)16Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    17/33

    INPUT AND OUTPUT (contd.):

    Formatted functions

    scanf

    printf

    (use format specifiers)

    (Input function)

    (Output function)

    UNFORMATTED FUNCTIONS :

    gets

    puts17Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    18/33

    IMPORTANT CONCEPT :

    Width specification ( %wd)Mainly used for beautification of output

    Example : int i=284;

    printf(%d , %2d , %3d,i);

    Output : 284,284,284Excercise :

    See the difference in the output when youexecute the following 2 codes :

    INPUT AND OUTPUT (contd.):

    for (i=1; i

  • 8/9/2019 Review of c Concept

    19/33

    INPUT AND OUTPUT (contd.):

    WILL THE PROGRAM WORK ?????

    void main( )

    {

    char *p;

    gets ( p ) ;

    puts ( p );

    }

    19Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    20/33

    FUNCTIONS :

    ADVANTAGES :

    1.Debugging

    2.Reusability

    3.Readability

    4.Memory

    DISADVANTAGES :

    Execution time ismore

    A subprogram/module which performs a

    specific task

    20Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    21/33

    STORAGE CLASSES :

    Talk about the SCOPE (who can access) & LIFETIME (how long it is alive) of a VARIABLE

    TYPES

    AUTO STATIC EXTERN REGISTER

    21Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    22/33

    STORAGE CLASSES (Contd.):

    KEY POINTS : Auto local and static local have a function scope

    Register mainly used for faster access of the

    variable Extern declaration more important across files

    Auto global variables can be used in more than

    one file using extern declaration

    Static global variables can be accessed only by

    functions in the file

    22Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    23/33

    STORAGE CLASSES (Contd.):Programs to understand key concepts of storage classes :

    1.

    void fn (int n)

    { n++; }

    void main()

    { autoint i =5;

    fn(i);

    printf(%d\n,i);fn(i);

    printf(%d,i);

    }

    Output :

    5

    5

    23Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    24/33

    STORAGE CLASSES (Contd.):2.

    void fn (int n)

    { n++; }

    void main()

    { staticint i =5;

    fn(i);

    printf(%d\n,i);

    fn(i);

    printf(%d,i);

    }

    Output :

    6

    7

    24Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    25/33

    STORAGE CLASSES (Contd.):

    Program to illustrate importance of extern declaration

    int i; //global

    void main( )

    {

    printf( %d , i ) ;}

    voidf1( )

    { i++; }

    voidf2( )

    { i++; }

    OUTPUT: 0

    void main( )

    {

    printf( %d , i ) ;

    }

    int i; //global only to f1,f2

    voidf1( )

    { i++; }

    voidf2( )

    { i++; }

    Thus,undefined symbol i

    (error)

    void main( ){

    extern int i;

    //declaration

    printf( %d , i) ;

    }

    int i; //definition

    voidf1( )

    { i++; } voidf2( )

    { i++; }

    25Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    26/33

    POINTERS : A pointer variable stores address of

    another variable

    int i =5;

    int *p;

    p=&i;

    printf(%d\n,i);

    printf(%d\n,*p);

    *p =8;

    5

    i

    1000

    1000

    p

    2000

    5

    5

    i8 1000 26Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    27/33

    POINTERS (contd.) :Program to illustrate importance of pointers :

    To swap contents of two variables using functions

    Method 1 : GLOBAL VARIABLES

    int a=2,b=3;

    void swap( );

    void main( )

    { swap( );

    printf(After swapping :\n a=%d , b=%d,a , b);

    }

    voidswap() { int t;

    t=a ; a =b ; b = t ; }

    OUTPUT :

    After swapping :a=3 , b =2

    27Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    28/33

    POINTERS (contd.) :METHOD2: PASSBYVALUE

    void swap (int , int );void main( )

    { int a=2,b=3;

    swap(a,b);

    printf (After swapping :\n a=%d , b=%d,a , b);

    }

    void swap (int a , int b)

    {

    int t;

    t=a ;a =b ;

    b = t ;

    }

    Output :

    After swapping:

    a=2 , b =3

    28Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    29/33

    POINTERS (contd.) :METHOD 3 : PASS BY REFERENCE

    voidswap(int *, int * );void main( )

    { int a=2,b=3;

    swap(&a,&b);

    printf (After swapping :\n a=%d , b=%d,a , b);

    }

    voidswap(int *i , int *j)

    {

    int t;

    t=*i ;*i =*j ;

    *j =t;

    }

    Output :

    After swapping:

    a=3 , b =2

    29Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    30/33

    POINTERS (contd.) :

    Why do we havepointers to

    int , float etc ?????

    1. To retreive / access

    value

    2. Pointer arithmetic

    FOREG:

    float *p;

    i=*p;

    // FETCH4 BYTESOFDATAANDASSIGNTOi

    30Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    31/33

    POINTERS (contd.) :ARRAYS AND POINTERS : Arrays are constant pointers

    Example program:

    void main( ){

    int a[3] = {5 , 6 , 7 } ;

    f1 ( a , 3 ) ;

    // a++ ; (array constant , thus , statement is incorrect)

    }

    void f1 (int *p , int n )

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

    p++;

    printf( %d\n,*p) ;

    }

    OUTPUT :

    56

    31Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    32/33

    POINTERS (contd.) :PRACTICE PROGRAM : To read one students marks and add a

    grace of 5 marks using pass by reference

    void main( )

    {

    float m;

    scanf( %d ,&m);grace(&m);

    printf( %d ,m);

    }void grace (float *p)

    { *p = *p + 5 ; }

    Input: 85

    Output :

    90

    EXCERCISE :

    REPEAT THE SAMEPROCEDURE FOR

    MANY STUDENTS

    32Prof. Shylaja S S, Head, ISE, PESIT

  • 8/9/2019 Review of c Concept

    33/33

    33Prof. Shylaja S S, Head, ISE, PESIT


Recommended