P3_DataTypesAndStorageClassInC

download P3_DataTypesAndStorageClassInC

of 26

Transcript of P3_DataTypesAndStorageClassInC

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    1/26

    2/15/20122/15/2012 11

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    2/26

    2/15/20122/15/2012 22

    Primary Data typesPrimary Data types

    Integerlong

    short

    signed

    Unsigned

    CharSigned

    Unsigned

    Floats and Doubles

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    3/26

    2/15/20122/15/2012 33

    Note: use sizeof() function to check the bytes occupied by the data types

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    4/26

    2/15/20122/15/2012 44

    Types of C VariablesTypes of C Variables

    Particular type of variable can hold only

    the same type of constant.

    Integer variable holds only integerconstant

    Rules for constructing different types of

    constant varies whereas for constructing

    variable names of all types the same set of

    rules apply.

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    5/26

    2/15/20122/15/2012 55

    Rules for constructing variable namesRules for constructing variable names

    Variable name is combination of 1 to 31alphabets,digits or underscores.

    Maximum length is 31 characters

    First character must be an alphabet orunderscore

    No commas or blanks allowed

    No special symbol other than anunderscore can be used.

    Ex. : si_int , m_hra , si_intr_2008

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    6/26

    2/15/20122/15/2012 66

    Rules for constructing variable namesRules for constructing variable names

    It is compulsory to declare the type ofvariable before use in the program.This

    type declaration is done at the beginning.

    Example of type declaration.int si, m_hra ;

    float bassal ;

    char code ;

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    7/26

    2/15/20122/15/2012 77

    Storage ClassesStorage Classes--TypesTypes

    Automatic

    Register

    Static External

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    8/26

    2/15/20122/15/2012 88

    Storage ClassesStorage Classes

    All variables in C has a storage class.

    If u dont specify the storage class for the

    variable, compiler will assume the storageclass depending on the context in which

    the variable is used.

    Two types of locations where a value is

    stored in computer.Memory

    Register

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    9/26

    2/15/20122/15/2012 99

    Storage ClassesStorage ClassesStorage

    Classes and itsimportance

    Storage Initial value Scope Life

    Automatic Memory Garbage

    value

    Local to the

    block in which

    it is defined

    Till the control

    remain within

    the block

    Register CPURegisters

    Garbagevalue

    Local to theblock in which

    it is defined

    Till the controlremain within

    the block

    Static Memory Zero Local to the

    block in which

    it is defined

    Value persists

    between

    different

    function calls

    External Memory Zero Global As long as

    program is

    running

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    10/26

    2/15/20122/15/2012 1010

    Storage ClassStorage Class -- AutomaticAutomatic

    main( )

    {

    auto int i = 1 ;

    {

    auto int i = 2 ;{

    auto int i = 3 ;

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

    }

    printf ( "%d ", i ) ;}

    printf ( "%d", i ) ;

    }Output:321

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    11/26

    2/15/20122/15/2012 1111

    Storage ClassStorage Class -- RegisterRegister

    main( )

    {

    register int i ;

    for ( i = 1 ; i

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    12/26

    2/15/20122/15/2012 1212

    Storage ClassStorage Class -- StaticStatic

    Static variable persists between different function calls, whereas auto variable

    reinitialized to 1 every time the function is called.

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    13/26

    2/15/20122/15/2012 1313

    Storage ClassStorage Class -- ExternalExternalint i ;main( )

    {

    printf ( "\ni = %d", i ) ;

    increment( ) ;

    increment( ) ;

    decrement( ) ;

    decrement( ) ;}

    increment( )

    {

    i = i + 1 ;

    printf ( "\n on incrementing i = %d", i ) ;

    }

    decrement( ){

    i = i - 1 ;

    printf ( "\n on decrementing i = %d", i ) ;

    }

    i = 0

    onincre

    menti

    ng i =

    1

    on

    incrementi

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    14/26

    2/15/20122/15/2012 1414

    Storage ClassesStorage Classes--When and whereWhen and where

    to useto use

    Economise the memory space consumed

    by variables

    Improve the speed of execution ofprograms

    Static-only if want the value to persist

    between different function calls

    Register-only for those variables which are

    being used frequently in the program

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    15/26

    2/15/20122/15/2012 1515

    Storage ClassesStorage Classes--When and whereWhen and where

    to useto use

    Extern-Only for those variables which are

    being used by almost all the functions in

    the program.

    Auto-If dont have any of the needs

    specified above, then go for auto.

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    16/26

    2/15/20122/15/2012 1616

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    17/26

    2/15/20122/15/2012 1717

    Hierarchy of OperationsHierarchy of Operations

    Priority or precedence in which operations in

    arithmetic statements are performed.

    Within parenthesis hierarchy is same.But,if there is more than one set ofparenthesis, innermost parenthesis would be performed first.

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    18/26

    2/15/20122/15/2012 1818

    Hierarchy of OperationsHierarchy of Operations

    Determine hierarchy and evaluate the

    following expression

    i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8

    i = 6 / 4 + 4 / 4 + 8 - 2 + 5/ 8 operation: *

    i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: /

    i = 1 + 1+ 8 - 2 + 5 / 8 operation: /

    i = 1 + 1 + 8 - 2 + 0 operation: /

    i = 2 + 8 - 2 + 0 operation: +

    i = 10 - 2 + 0 operation: +i = 8 + 0 operation : -

    i = 8 operation: +

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    19/26

    2/15/20122/15/2012 1919

    Hierarchy of OperationsHierarchy of Operations

    Evaluate

    kk = 3 / 2 * 4 + 3 / 8 + 3

    kk = 1 * 4 + 3 / 8 + 3 operation: /kk = 4 + 3 / 8 + 3 operation: *

    kk = 4 + 0 + 3 operation: /

    kk = 4 + 3 operation: +kk = 7 operation: +

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    20/26

    2/15/20122/15/2012 2020

    Converting general arithmeticConverting general arithmetic

    statement to C statementstatement to C statement

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    21/26

    2/15/20122/15/2012 2121

    Associativity of operatorsAssociativity of operators

    When an expression contains two operators of equal

    priority, then it is solved using Associativity.

    It can be of two types.

    Left to Right

    - Left operand must be unambiguous

    Right to Left

    - Right operand must be unambiguous

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    22/26

    2/15/20122/15/2012 2222

    Associativity of operatorsAssociativity of operators

    Consider the expression

    a = 3 / 2 * 5 ; /* there is a tie between *

    and / operator */

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    23/26

    2/15/20122/15/2012 2323

    Associativity of operatorsAssociativity of operators

    Consider one more expression

    a = b = 3 ; /* both = has the same priority*/

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    24/26

    2/15/20122/15/2012 2424

    Associativity of operatorsAssociativity of operators

    Another expression

    z = a * b + c / d ; /* / and * has same priority

    and same Associativity */

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    25/26

    I request Electronics and communicationI request Electronics and communication

    ENGINEERING students to visit my blog forENGINEERING students to visit my blog for

    moremore

    abhishek1ek.blogspot.comabhishek1ek.blogspot.com

    awhen ineerin .blo s ot.comawhen ineerin .blo s ot.com2/15/20122/15/2012 2525

  • 8/3/2019 P3_DataTypesAndStorageClassInC

    26/26

    2/15/20122/15/2012 2626