CMP 101 Set 14 Programming in C

download CMP 101 Set 14 Programming in C

of 16

Transcript of CMP 101 Set 14 Programming in C

  • 8/4/2019 CMP 101 Set 14 Programming in C

    1/16

    CMP 101 Fundamentals of

    Computer and programming in CProgramming in C

    Textbook: E. Balagurusamy, Programming in

    ANSI C, Third Edition, Tata McGraw-HillPublishing company Limited, 2004

  • 8/4/2019 CMP 101 Set 14 Programming in C

    2/16

    Programming in C

    History of C

    Importance of C

    Structure of C Programs

    Character Set, tokens,

    key words & identifiers.

    Data Types

    Constants

    Symbolic class

    Variables Declarations

    Variables

    A variable as a constant

    A variable as volatile

    Storage class

    Data overflow &

    underflow

    Already covered

    Self study

    Self study

  • 8/4/2019 CMP 101 Set 14 Programming in C

    3/16

    Character Set, tokens, key words &

    identifiers.

    Character Set Letters (Uppercase A-Z, Lowercase a-z)

    Digits 0-9

    Special charactersComma

    Period

    Semicolon

    Colon

    Question mark

    Apostrophe

    Quotation mark

    Exclamation mark

    Vertical bar

    Slash

    Backlash

    Tilde

    Under score

    Dollar

    ,

    .

    ;

    :

    ?

    !

    |

    /

    \

    ~

    _

    $

    Percent sign

    Caret

    Asterisks

    Minus sign

    Plus sign

    Less than

    Greater than

    Left parenthesis

    Right parenthesis

    Left brace

    Right brace

    Number sign

    Left bracket

    Right bracket

    %

    ^

    *

    -

    +

    (

    )

    {

    }

    #

    [

    ]

    Equal

    WHITE SPACES

    Blank space

    Horizontal tab

    Carriage Return

    New line

    Form feed

    =

  • 8/4/2019 CMP 101 Set 14 Programming in C

    4/16

    Tokens

    Keywords

    Constants Strings

    Operators

    Identifiers Special symbols

    Words with fixed meaning. Must be written in lower case.

    Example: main, int, float

    Variable names symbolizing memory addresses.

    Rules for creating identifiers1. The first character must be an alphabet or underscore.

    2. Must consists of letters, digits, or underscore

    3. May have 256 characters but only the first 31 characters areimportant

    4. Should be a keyword

    5. Should contain white spaces

    Numeric constants like -59.67Character constants like Year 2010 Year 2010

    *. +. . -. (.

    +, -, *,

    /,

  • 8/4/2019 CMP 101 Set 14 Programming in C

    5/16

    Tokens

    key words or reserve words (Prepare a list of

    ANSI C Key words)

    Constants

    Numeric Character

    Integer Real Single String

    Decimal, Octal andHexadecimal

    Backslash

    \a bell sound

    \f backspace

    \n new line

    \t horizontal tab

    ..

  • 8/4/2019 CMP 101 Set 14 Programming in C

    6/16

    Example

    #include

    void main

    {

    printf (Integer values \n\n);

    printf (%d %d %d \n, 32767, 327671, 326710);

    printf (\n);

    }

    Integer Values

    3276 -32768 -32759

    Output

    Why negative?

  • 8/4/2019 CMP 101 Set 14 Programming in C

    7/16

    Data Types

    Primary DataTypes

    integer

    floating

    Pointsigned unsigned

    Character

    Int

    short int

    long int

    unsigned int

    unsigned short int

    unsigned long int

    float

    Double

    Long double

    Signed

    -128 to 127

    Unsigned

    0 to 255

    Exercise: Find the size and range of each data type

    void

  • 8/4/2019 CMP 101 Set 14 Programming in C

    8/16

    Variable Declaration

    Data Type Keyword Declaration in C

    Character (8 bits) char char a, b, c;

    Unsigned Character unsigned char unsigned char a, b, c;

    Signed Character signed char signed char a, b, c;

    Integer (16 bits) int int x, y, z;Short Integer (8 bits) short int short int x, y, z;

    Long Integer (32 bits) long int long int x, y, z;

    Real (32 bits) float Float x,y,z;

    Real (64 bits) double double x, y, z;

    Real (89 bits) long double Long double x, y, z;

  • 8/4/2019 CMP 101 Set 14 Programming in C

    9/16

    Example

    # include float x, y;int code;short int count;

    long int amount;double deviation;unsigned n;char c;

    /*--------------------*/

    Main (){

    }

    x: 4 Bytes

    y; 4 Bytes

    code; 2 Byte

    count; 1 Bytes

    amount; 4 Bytes

    deviation; 8 Bytes

    n; 2 Bytes

    c; 1 Byte

    Data Segment

  • 8/4/2019 CMP 101 Set 14 Programming in C

    10/16

    User defined Data types

    typedef

    User can define type using typedef feature.

    Example

    typedef int unit;

    typedeffloat marks;

    Unit symbolizes int and marks float.unitx, y, z; means x, y and z are integer type.

    float a, b, c; means a, b, and c are float type.

  • 8/4/2019 CMP 101 Set 14 Programming in C

    11/16

    User defined data type

    enum (enumerated type)

    enum identifier ( value1, value2, , valuen)

    ExampleDefinition

    enum day(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,

    Sunday);

    Variable Declaration

    enum day week_st, week_end;These declaration day week_st and week_end can take values in (Monday to

    Sunday) and we can use them as

    week_st = Monday;

    week_end=Friday;

  • 8/4/2019 CMP 101 Set 14 Programming in C

    12/16

    Storage Class

    Location and visibility of variables.

    # include float x, y;

    void main ()

    {int i;float b;-- - -functionx ();

    }float functionx ();{int i;float s;

    }

    y

    bi

    si

    xGlobal

    x, y are visible toMain and functionx

    Local to main

    The variables i and b are

    visible to main onlyLocal functionxThe variables i and s are

    visible to functionx only

    Data segment

  • 8/4/2019 CMP 101 Set 14 Programming in C

    13/16

    Storage class and their meaning

    auto : known within the function it is

    declared.

    static: a local variable that retains its velue

    even after the control is transferred to calling

    program.

    extern: global variable known to all functions.

    register: a local variable that is stored in a

    register.

  • 8/4/2019 CMP 101 Set 14 Programming in C

    14/16

    Assigning values to variables

    Assignment statement

    Assignment statement is defined as

    identifier= expression where expression give a value.

    Example

    fee = 20.5;

    rent = 56;

    expense = fee + rent;

  • 8/4/2019 CMP 101 Set 14 Programming in C

    15/16

    Programing example

    # include

    void main()

    {

    float x, p;

    double y, q;

    unsigned k;

    int m=54321;

    long int n=1234567890;

    x = 1.2345678900;

    y= 9.87654321;

    k= 54321;

    P=q=1.0;

    printf( m = %d \n, m);printf( n = %ld \n, n);

    printf( x = %.121f\n, x);

    printf( x = %f\n, x);

    printf( y = %.121f\n, y);

    printf( y = %f\n, y);

    printf( k = %u p = %f q = %.121f\n, k,p,q);

    }

  • 8/4/2019 CMP 101 Set 14 Programming in C

    16/16

    Defining Synbolic Constants

    #define directives

    #define PI 3.14

    #define PASS_MARK 50

    #define MAX = 100

    #define STRENGTH 200