2 C Program Workshop

Embed Size (px)

Citation preview

  • 8/13/2019 2 C Program Workshop

    1/13

    THE C PROGRAMWORKSHOP

  • 8/13/2019 2 C Program Workshop

    2/13

  • 8/13/2019 2 C Program Workshop

    3/13

    A computer program written in C

    is like a workshop

    It brings data into the program from the outside world, performs some processing with it, makes some changes to it,and then returns it to the outside world in a different form

    main

    Current Data

    (Information)

    Transformed Data

    (Information)

  • 8/13/2019 2 C Program Workshop

    4/13

    C Program Entrances and Exits

    STANDARD

    OUT (stdout)

    STANDARDIN (stdin)

    STANDARDERROR (stderr)

    Constants and variables

    Operations and

    functions

    main

    X Y Z

    A B C D

    Instructions and

    operating procedures

    File and Device

    Input/Output

  • 8/13/2019 2 C Program Workshop

    5/13

    Data Types

    Integers ( int ) Numbers with decimal points ( float ) Characters ( char ) Words and Phrases (ex. char phrase[10] ) Tables (ex. int values[10] , float matrix[20][30] ) Records (ex. struct myRecordType )

  • 8/13/2019 2 C Program Workshop

    6/13

    Data Storage Locations

    int aNumber;

    float aValue;

    char aSymbol;

    char phrase[LENGTH];

    int values[15];

  • 8/13/2019 2 C Program Workshop

    7/13

    Simple Operations

    Storage ( =) Addition ( +) Subtraction ( - ) Multiplication ( * ) Division ( / )

    Modulo ( %) Comparison ( ==, != , < , , >=)

  • 8/13/2019 2 C Program Workshop

    8/13

    Instructions and Decisions

    variableLocation = expression Ex. count = 17 * MAX_LOOPS;

    if (Boolean condition) statement else statement Ex. if (numberIsValid)

    count++;elsecount--;

    while (Boolean condition) statement Ex. while (count > MAX_LOOPS)

    count = count 5; for (statement; condition; statement) statement

    Ex. for (i = 0; i < size; i++) printf(%d \ n, table[i]);

  • 8/13/2019 2 C Program Workshop

    9/13

    Technical Vocabulary:

    C Keywords

    auto double int struct

    break else long switchcase enum register typedefchar extern return unionconst float short unsignedcontinue for signed voiddefault goto sizeof volatiledo if static while

    (Note: Keywords in bold-face font are used with data types)

  • 8/13/2019 2 C Program Workshop

    10/13

    Standard C Library Functions

    Standard Input and Output: printf, scanf, getchar, gets

    Conversion of Data: isalpha, isdigit, tolower, toupper

    Math: sin, cos, tan, sqrt, log, exp

    Working with Files: fopen, fclose, fscanf, fprintf, fgets

  • 8/13/2019 2 C Program Workshop

    11/13

  • 8/13/2019 2 C Program Workshop

    12/13

    Example Program Output

    Ck Nbr Acct Tax Amount Balance------ ---- --- ------ -------

    234 A Z 34.56 2356.96

  • 8/13/2019 2 C Program Workshop

    13/13

    Example Program Exercise

    Using the current pattern of statements in the program,add the source code to display the last two lines of thereport shown below

    Also, change the order of the report columns

    Ck Nbr Amount Acct Balance Tax------ ------ ---- ------- ---

    234 34.56 B 2356.96 Z235 192.73 T 2164.23 W236 75.00 G 2089.23 X