Programmin in C

download Programmin in C

of 19

Transcript of Programmin in C

  • 7/27/2019 Programmin in C

    1/19

    PROGRAMING IN C

    Tutorial

  • 7/27/2019 Programmin in C

    2/19

    A Simple C program

    #include

    int main( ) {

    printf("hello, world");

    return 0;

    }

  • 7/27/2019 Programmin in C

    3/19

    Variables; Types and Type Declarations

    #include

    int main( ) {

    int a, b, c, sum;

    a = 1; b = 2; c = 3;

    sum = a + b + c;

    printf("sum is %d", sum);

    return 0;

    }

  • 7/27/2019 Programmin in C

    4/19

    Variables; Types and Type

    Declarations

  • 7/27/2019 Programmin in C

    5/19

    Simple I/O getchar(), putchar(), printf ()

    int main( ) {char c;

    c = getchar( );

    putchar(c);

    getchar();

    }

    printf("hello, world");

    printf("sum is %d", sum);

  • 7/27/2019 Programmin in C

    6/19

    Simple I/O getchar(), putchar(), printf()

    n = 511;

    printf("What is the value of %d in octal?", n);

    printf("%s! %d decimal is %o octal\n", "Right", n, n);

    What is the value of 511 in octal? Right! 511 decimal is 777 octal

  • 7/27/2019 Programmin in C

    7/19

    If - relational operators and compound

    statements

    if(expression) statement

    == equal to!= not equal to

    > greater than

    < less than

    >= greater than or equal to

  • 7/27/2019 Programmin in C

    8/19

    If - relational operators and compound

    statements

    C guarantees that `&&' and `||' are evaluated left to right --we shall soon see cases where this matters.

    if( c==' ' || c=='\t' || c=='\n' ) ...

    Grouped together by {}

    if (a < b) {

    t = a;

    a = b;

    b = t;

    }

  • 7/27/2019 Programmin in C

    9/19

    Else Clause; Conditional Expressions

    if (expression) statement1 else statement2

    if (a < b)

    x = a;

    elsex = b;

    expr1 ? expr2 : expr3

    a

  • 7/27/2019 Programmin in C

    10/19

    a common programming structure

    if(...)

    {...}

    else if(...){...}

    else if(...)

    {...} else {...}

  • 7/27/2019 Programmin in C

    11/19

    The Switch Statement ; Break ;

    Continueswitch( c ) {

    case 'a':

    aflag++;

    break;

    case 'b':

    bflag++;break;

    case 'c':

    cflag++;

    break;

    default:printf("%c?\n", c);

    break;

    }

  • 7/27/2019 Programmin in C

    12/19

    While Statement; Assignment within

    an Expression; Null Statement

    while (expression) statement

    int main( ) {char c;

    while( (c=getchar( )) != '\0' )

    putchar(c);

    return 0;

    }

  • 7/27/2019 Programmin in C

    13/19

    for Statement

    for( initialization; expression; increment)

    statement

    initialization;

    while( expression ) {

    statement

    increment;

    }

  • 7/27/2019 Programmin in C

    14/19

    for Statement

    sum = 0;

    for( i=0; i

  • 7/27/2019 Programmin in C

    15/19

    Arithmetic

    x = a%b;

    c = c + 'A' - 'a';

    main( ) {

    char c;

    while( (c=getchar( )) != '\0' )

    if( 'A'

  • 7/27/2019 Programmin in C

    16/19

    Increment and Decrement Operators

    int k=5;

    x = ++k;

    x = k++;

    main( ) {

    int c,n;

    n = 0;

    while( (c=getchar( )) != '\0' )

    if( c == '\n' )

    ++n;printf("%d lines\n", n);

    }

  • 7/27/2019 Programmin in C

    17/19

    Arrays

    int x[10];

    x[0], x[1], x[2], ..., x[9]

    main( ) {

    int n, c;

    char line[100];n = 0;

    while( (c=getchar( )) != '\n' ) {

    if( n < 100 )

    line[n] = c;

    n++;}

    printf("length = %d\n", n);

    }

  • 7/27/2019 Programmin in C

    18/19

    Character Arrays; Strings

    We can copy a character array s into another t like this:

    main( ) {

    int n;

    char line[100];n = 0;

    while( (line[n++]=getchar( )) != '\n' );

    line[n] = '\0';

    printf("%d:\t%s", n, line);

    }

  • 7/27/2019 Programmin in C

    19/19

    Example to count letters, digits and

    others in a filemain( ) {

    int let, dig, other, c;

    let = dig = other = 0;

    while( (c=getchar( )) != '\0' )

    if( ('A'