05 Program Structure and Construction

download 05 Program Structure and Construction

of 28

Transcript of 05 Program Structure and Construction

  • 8/12/2019 05 Program Structure and Construction

    1/28

    Introduction to C Programming

    Lexical Elements ,Data types and

    the C system

  • 8/12/2019 05 Program Structure and Construction

    2/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    Topic & Structure of the lesson

    In this chapter you will learn about:

    C program structureStandard library and function

    Types of errors

  • 8/12/2019 05 Program Structure and Construction

    3/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    Learning Outcomes

    If you have mastered this topic, you should be ableto use the following terms correctly in your

    assignments:

    Preprocessor directive

    Stdio.h

    Main function

    Comments

    Constants Expression

    Syntax, logical and runtime errors

  • 8/12/2019 05 Program Structure and Construction

    4/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    Constants

    Constantsareentities that appear in theprogram code as f ixed values.

    For Example:-

    const double taxrate=0.0175; Character Constant: Is acharacter enclosed in sing le quo tat ionmarks.

    Some examples of character constants are 1,aand A.

  • 8/12/2019 05 Program Structure and Construction

    5/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    Constants

    Example 1:-

    The output statement,

    printf(%c,T);

    will print the letter Ton the screen.

    Example 2:-

    char S=a;

    printf(%c,S);

    will print the letter S on the screen.

  • 8/12/2019 05 Program Structure and Construction

    6/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    String Constants

    String Constant:

    A string literal or string constant is a

    sequence of any number of characters

    surrounded by double quo tat ionmarks. Following are examples of String Constants.

    This is a String Constant

    125

    Is this Johns car?

  • 8/12/2019 05 Program Structure and Construction

    7/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Statements

    A statementis a Speci f ication of an act ionto be taken by the computer as the program

    executes.

    Each C statement must end with thepunctuator/ separator semicolon (;).

    For Example:-

    c= a+b; But, some of the statements should not end

    with semicolon.

    For Example, while ( I

  • 8/12/2019 05 Program Structure and Construction

    8/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Expressions

    An Expressionis a combination of bothOperatorsand Operands.

    For Example:-

    city_tax = taxrate * gross_income;

    c= a + b;

    area = 2 * pi * r;

  • 8/12/2019 05 Program Structure and Construction

    9/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Standard Output function printf()

    The Syntax for the print f()function is,

    printf(Format Control String);

    printf(Format Control String, print list);

    For Example :-

    printf(Enter your age:);

    printf(%d, age);

    printf(Your age of birth is %d, yob );

  • 8/12/2019 05 Program Structure and Construction

    10/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Standard Input function scanf()

    The Syntax for the scanf()function is,

    scanf (Format Control String, print list);

    Example 1:-

    printf(Enter your age:);

    scanf(%d,&age);

    printf(Type your weight in inches:);

    scanf(%d,&weight);

  • 8/12/2019 05 Program Structure and Construction

    11/28

  • 8/12/2019 05 Program Structure and Construction

    12/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Structure of a C Program

    C program consists of the followingcomponents:

    Program Comments

    Preprocessor directives

    Type declarations

    Named Constants

    Statements

    Function definitionsFunction Calls

  • 8/12/2019 05 Program Structure and Construction

    13/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Example for a C Program

    /* Program to find area of a circle*/

    #include

    main()

    {int radius;

    double area;

    const double pi=3.14;

    printf(\nEnter the radius value:);

    scanf(%d,&radius);

    area=2*pi*radius;Statements

    Program

    commentsPreprocessor

    Directives

    Type

    DeclarationsNamed

    Constants

  • 8/12/2019 05 Program Structure and Construction

    14/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Example for a C Program

    printf(\n Area of a Circle =

    %d,area);

    }

    OUTPUT:

    Enter the radius value: 2

    Area of a Circle = 12.56

  • 8/12/2019 05 Program Structure and Construction

    15/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Program Comments

    Commentsare explanat ionsor annotat ionsthat are included in a program for

    documentationand clarification purposes.

    They are completely ignoredby thecompiler dur ing compi lat ion,and they

    have no effect on program execution.

    There are two types of comments:

    Multiple line comment is represented

    by /**/

  • 8/12/2019 05 Program Structure and Construction

    16/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Program Comments

    Single line comment is represented by // For Example :-

    1. /* This Program is used to calculate

    the Factorial of a number */2. // Program to add two numbers

    3. // Program to find area of a Circle

    Comments are meant to aid,never to hamper, readability.

  • 8/12/2019 05 Program Structure and Construction

    17/28CT018-3-1 Introduction To C Programming Program Structure and Construction

    Preprocessor Directives

    preprocessor modifies the text of a Cprogram before compilation.

    A preprocessor directive starts with #

    #includeinstructs the preprocessorto include the contents of the standardinput .output(stdio.h), in the program, inorder to use certain functions printf andscanf.

    Constant double pi=3.14 associates theconstant macro pi with the meaning 3.14.

  • 8/12/2019 05 Program Structure and Construction

    18/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    main()Function

    Every C program has a mainfunction, whereprogram execution begins.

    A function definition consists of headerand a

    body. header:consists of the name of the function

    and the parameter list enclosed within the

    parenthesis().

    body: consists of declarat ionsand executablestatements both enclosed in braces {},known

    as block.

  • 8/12/2019 05 Program Structure and Construction

    19/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    main()Function

    main ()

    {

    float inches,cm;

    /* variable declaration */

    printf(Enter a length in inches:

    );

    /* ... rest of statements follow ...*/

    }

  • 8/12/2019 05 Program Structure and Construction

    20/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    Types of Errors

    Errorsare classified into three types:

    Syntax Errors

    Logical Errors

    Runtime Errors

  • 8/12/2019 05 Program Structure and Construction

    21/28

  • 8/12/2019 05 Program Structure and Construction

    22/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    Logical Errors

    Logic errorsgo unnoticed by the

    compiler, but they cause fatal

    errors in the outcome of aprogram. Exampleis not

    producing the required or

    expected output.

  • 8/12/2019 05 Program Structure and Construction

    23/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    Runtime Errors

    Runtime errorsattempt to

    perform some illegal operations

    when it executes. Exampleisdividing by zero.

  • 8/12/2019 05 Program Structure and Construction

    24/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    A Sample Program

    Problem: Conversion from inches to centimeters

    Recall the first step in problem solving:

    Understand the problem

    What is the Input?

    Length in inches.Lets call it inches.

    What is the Output?

    Length in centimeters.Lets call it cm.

  • 8/12/2019 05 Program Structure and Construction

    25/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    A Sample Program

    Step 2: Devise a p lan

    What is the relationship between the input and

    the output?

    one inch = 2.54 centimeters

    So the algorithm is:

    get the length in inches

    compute length in centimeters

    report length in centimeters

  • 8/12/2019 05 Program Structure and Construction

    26/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    A Sample Program

    Step 3:Carry out the plan

    Do I know how to compute the length in

    centimeters?

    Yes.compute length in centimeters

    length in centimeters is 2.54 *length in inches

    Is the plan(algorithm) correct?Work out on some examples.

    Now move from algorithm to writing program.

  • 8/12/2019 05 Program Structure and Construction

    27/28

    CT018-3-1 Introduction To C Programming Program Structure and Construction

    A Sample Program

    /* Program to convert from inches into centimeters*/#include

    #define CM_PER_INCH 2.54

    main()

    {

    float inches, cm;

    printf(Enter the length in inches:);

    scanf(%f,&inches);cm= CM_PER_INCH * inches;

    printf(That equals %.2f centimeters\n,cm);}

  • 8/12/2019 05 Program Structure and Construction

    28/28

    Output

    Enter the length in inches: 2

    That equals 5.08 centimeters