ita102 structure of c program

Upload: carlo-rosales

Post on 07-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 ITA102 Structure of C Program

    1/15

    Structure of

    C Program

    Carlo H. Coronel

    ITA102 - Fundamentals of Programming,

    Database Theory and Applications

  • 8/6/2019 ITA102 Structure of C Program

    2/15

    Data Types

  • 8/6/2019 ITA102 Structure of C Program

    3/15

    int (Integer)

    can store a value in the range -32768 to

    +32767. No fractional part.

    Format:

    int variable_name;

    float (Float)

    has about seven digits of precision and a

    range of about 1.E-36 to 1.E+36.

    Format:

    float variable_name;

  • 8/6/2019 ITA102 Structure of C Program

    4/15

    double (Double)

    double, or double precision, number has about

    13 digits of precision and a range of about 1.E-303 to 1.E+303.

    Format:

    double variable_name;

    char (Character)

    - to store one character to a variable name, the

    character should be enclosed in single quotes.

    Single character.Format:

    char variable_name;

    Example:

    char first_char = A;

  • 8/6/2019 ITA102 Structure of C Program

    5/15

    Layout of a Simple C Program

    pre-processor directives

    global declarations;

    main( )

    {

    local variables to function main ;

    statements associated with function main ;}

  • 8/6/2019 ITA102 Structure of C Program

    6/15

    Layout of a Simple C Program

    #include

    main ()

    {

    int number, product;int MULTIPLIER = 10;

    printf (Enter any number to be multiplied by 10\n);

    scanf (%d, &number);

    product = number * MULTIPLIER;

    printf (%d * %d = %d, number, MULTIPLIER, product);

    }

  • 8/6/2019 ITA102 Structure of C Program

    7/15

    Basic Input and Output codes

    The printf function

    Example:

    Printf (Hello!);

    The scanf function

    Example:

    scanf(%d, &num);

  • 8/6/2019 ITA102 Structure of C Program

    8/15

    Basic Input and Output codes

    Format Specifiers

    %c used for single charactersscanf (%c, &ch);

    printf (%c, ch);

    %d used for whole numbersscanf (%d, &num);printf (%d, num);

  • 8/6/2019 ITA102 Structure of C Program

    9/15

    Basic Input and Output codes

    Format Specifiers

    %f used for number with floating or decimal pointscanf (%f, &pesos);

    printf (%f, pesos);

    %s used for string of charactersscanf (%s, &str);

    printf (%s, str);

  • 8/6/2019 ITA102 Structure of C Program

    10/15

    Running C Program

    Developing a program in a compiled language

    such as C requires at least four steps:

    1. Editing (or writing) the program (source file)

    2. Compiling - source file must be translated

    into binary numbers understandable to the

    computer's Central Processing Unit.

    Compiling also produces an object file.

    3. Linking - after linking, the file extension is

    .exe which are executable files.

    4. Executing - running the program for

    production.

  • 8/6/2019 ITA102 Structure of C Program

    11/15

    Basic C Operators

    ( ) Parentheses (for grouping)

    * Multiplication

    / Division

    % Modulus (finding the remainder)

    + Addition

    - Subtraction

    = Assignment (assigning the valueto a variable)

  • 8/6/2019 ITA102 Structure of C Program

    12/15

    Basic C Operators

    Example:

    Finding the average of three input

    numbers:

    int num1, num2, num3, ave;

    ave = (num1 + num2 + num3)/3;

    ** Take note that the remainder is dropped

    when you declare the variable (storing

    the final result) as integer type.

  • 8/6/2019 ITA102 Structure of C Program

    13/15

    Unary Operators

    Increment: ++value

    #include

    main()

    {

    int num1 = 10;

    int num2 = 5;

    int total;

    total = num1 + ++num2;

    printf (total = %d, total);

    }

    Increment: value++

    #include

    main()

    {

    int num1 = 10;

    int num2 = 5;

    int total;

    total = num1 + num2++;

    printf (total = %d, total);

    }

    10 + 6 = 16 10 + 5 + 1 = 16

  • 8/6/2019 ITA102 Structure of C Program

    14/15

    Unary Operators

    Decrement: --value

    #include

    main()

    {

    int num1 = 10;

    int num2 = 5;

    int total;

    total = num1 + -- num2;

    printf (total = %d, total);

    }

    Decrement: value- -

    #include

    main()

    {

    int num1 = 10;

    int num2 = 5;

    int total;

    total = num1 + num2--;

    printf (total = %d, total);

    }

    10 + 4 = 14 10 + 5 - 1 = 14

  • 8/6/2019 ITA102 Structure of C Program

    15/15