C+Pres

download C+Pres

of 18

Transcript of C+Pres

  • 8/12/2019 C+Pres

    1/18

    1

    Brief Notes on C

    Structure of C program

    Include files#include

    //for input/output functions Define Constant

    #define const_name value main program

    main()

    { /* each declarations andstatements are separated by

    semi colon */// declarations variables arrays records function declarations etc

    // statements}

    // function definitions

  • 8/12/2019 C+Pres

    2/18

    2

    Comments are enclosed within

    /* comments on multiple lines */// single line comment

    Compiler Directives

    #includedirective is used to include

    the header file for input/output,the standard mathematics library

    mat h. hetc.These files are enclosed within < >

    #define directive helps in defining

    constant symbol

  • 8/12/2019 C+Pres

    3/18

    3

    Datatypes

    Standard:int, float, char, double

    User defined datatypes:structures, pointers, arrays,enumerated datatype etc.

    Declarationintx, y, z;

    floatp, q[3][4];charname[20]; array

    Expressionx = x + 2 x += 2i = i +1 i++ or ++ix = x +(++i); the value of x is

    added with the value of i afterincrementing it by 1.

  • 8/12/2019 C+Pres

    4/18

    4

    x = x + (i++); the value of x isadded with the value of i and then iis incremented by 1.Similarly, x = x+(--i); x = x+(i--);

    Example:

    /* include header file stdio.h thatcontains I/O functions. */#include // constant definition#define i 6

    main(){ // integer declaration

    int x, y;// Assignment statementsx=7; y= i + x;

    // output statementprintf("%d\n", y);

    }

  • 8/12/2019 C+Pres

    5/18

    5

    Conditional Expression

    exp ? exp1 : exp2

    /* An expression exp is evaluatedand if the value is nonzero (or true -

    represented by 1) then expressionexp1 is the final value otherwiseexp2 is the final value of entireexpression. */

    Basic Statements

    Assignment statement

    x = expression

    Compound statement{ s1;

    s2;. }

  • 8/12/2019 C+Pres

    6/18

    6

    /* Collection of statements, eachseparated by semi colon and

    enclosed in brackets */

    Conditional statements

    i. if (cond) statementii.

    if (cond) s1 else s2;

    /* Here cond is a boolean conditionwhich can have value 1 (for true) or0 (for false). */

    For statement

    for (i = m1; i

  • 8/12/2019 C+Pres

    7/18

    7

    While statement

    while (cond){ Body };

    Do-while statement

    do{Body }while cond;

    Logical Operators

    && AND|| OR! NOT

  • 8/12/2019 C+Pres

    8/18

    8

    Bitwise operations

    & bitwise AND| bitwise inclusive OR^ bitwise exclusive OR right shift

    ~ One's complement

    Relational Operators

    == equality

    < less than greater than>= greater than equal to

    != Not equality

  • 8/12/2019 C+Pres

    9/18

    9

    Switch statement

    switch (exp)

    { case v1 : s1 ; break;case v2 : s2 ; break;

    case vn : sn ; break;optional default : s}

    /* If the value of expis vjthen sjisexecuted and switch statement isexited using break statement.Execution always starts from 1 tolast. */

  • 8/12/2019 C+Pres

    10/18

    10

    Input/Output statement

    /* reads single character and

    stores in character variable x */x = getchar();

    /* prints single character stored in

    x */putchar(x);

    /* the following functions are instandard file named stdio.h */

    scanf(control, v1, v2, ..);printf(control, e1,e2,...);

    Control in input/output

    control = "sequence of formatdescriptor"

  • 8/12/2019 C+Pres

    11/18

    11

    Format descriptor Meaning

    %d a decimal integer%o a octal integer

    %c a single character%s a character string%f a decimal number

    (float or double)

    \n skip to new line

    Examples:

    i. printf("%4d%7.2f\n%c\n",x, y, z)ii. scanf("%4d%8.2f \n",&x, &y)

    Here & represents memory addresses

    Structure in C : Used for recorddeclaration

    struct person

    { char name[25];float salary;char address[35];

    } p_rec;

  • 8/12/2019 C+Pres

    12/18

    12

    Here p_rec is user defined

    structure datatypeDeclaration using above define

    structure is as follows:

    struct person x, y;

    ORp_rec x, y;

    Can define structure withinstructureFields of structure variable are

    accessed as:

    x . salary,y . name etc.

  • 8/12/2019 C+Pres

    13/18

    13

    Subprograms

    type fun_name(parameter along

    with type){ // local declaration;

    body;}

    type : is the type of value returnedby the function and can be basic typeor user defined.

    return:statement is used in the bodyof a function.

  • 8/12/2019 C+Pres

    14/18

    14

    Example:

    #include

    main(){ int i, x;

    // function declarationint power (x, n);

    for (i =0; i < 10; ++i){ x = power(2, i);printf("%d%d\n", i, x);

    }}

    // function definitionint power(int x, n){ int i, p;

    p = 1;for (i =1; i

  • 8/12/2019 C+Pres

    15/18

    15

    Parameter Passing

    Default parameter passing is by valueCall by reference can be achieved bypassing addresses as an actualparameters corresponding to pointervariables in formal parameter list.

    Pointer variable

    int *p, *q, i, t ;float *x, *y;

    p & q are pointer variables pointingto locations holding integer values.x & y are pointer variables pointing

    to locations holding real values.i and t are simple integer variables.

  • 8/12/2019 C+Pres

    16/18

    16

    Possible Assignments

    // initializing pointer variable.

    p = NULL/* address of t is stored in pointervariable p. */

    t = 2;

    p = &t;/* contents of t are added with thatpointed out by p and assigned to i */

    i = t + (*p);/* illegal as i is a simple variable and

    p is a pointer variable */i = p/* value pointed out by x is assignedto simple variable z */

    *x=34.5;

    z = *x;/* releasing the memory location

    pointed out by p */free(p);

  • 8/12/2019 C+Pres

    17/18

    17

    Illustration of pointer variables:

    p 20

    q 30

    p 30

    *p = *q q 30

    dangling

    p 20

    p = q

    q 30

  • 8/12/2019 C+Pres

    18/18

    18

    Example: Explainingcall by reference

    void swap (int *p,*q){ int t;

    t = *p;*p = *q;

    *q = t;}

    Void type means that the function isnot returning any value. So return

    statement is not used when return typeis void.

    Corresponding call statementx = 4;

    y = 5;// addresses are passedswap(&x, &y);