c and c++ report

Upload: sumitbatra203

Post on 03-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 c and c++ report

    1/29

    Training report on c and c++

    Prepared By: Sumit Batra2809203

  • 7/28/2019 c and c++ report

    2/29

  • 7/28/2019 c and c++ report

    3/29

    Overview of C

    C is developed by Dennis Ritchie

    C is a structured programming language

    C supports functions that enables easy maintainability

    of code, by breaking large file into smaller modules Comments in C provides easy readability

    C is a powerful language

  • 7/28/2019 c and c++ report

    4/29

    Structure Of C Program

  • 7/28/2019 c and c++ report

    5/29

    Execution Of a C ProgramExecuting a C program involves following steps:

    Creating the program

    Compiling of Program Linking of Program

    Execution Of Program

  • 7/28/2019 c and c++ report

    6/29

    Operators & Expressions Operator:An operator is a symbol that tells the computer to

    perform certain mathematical or logical operations. C supports a set ofOperators.

    There are various Operators in C as Follows:1. Arithmetic Operators

    2. Assignment Operators

    3. Increment/Decrement Operators

    4. Relational Operators

    5. Logical Operators

    6. Conditional Operators

    7. Bitwise Operators

  • 7/28/2019 c and c++ report

    7/29

    FunctionsA Function is a self-contained program

    segment that carries out some specific, well-defined task.Each C program consists of one or more functions . One of

    these functions must be called main. Execution of theprogram will always begin by carrying out the instructionsin main.

    Types Of Functions:C functions can be classified into two categories:

    1. Library functions and

    2. User-defined functions..

  • 7/28/2019 c and c++ report

    8/29

    ArraysAn Array is a collection of identical data objects which are stored inconsecutive memory locations under a common heading or variable

    name. The objects are called elements of the array and are numberedconsecutively 0,1,2,3 and so on. These numbers are called Index Valuesor Subscripts of the Array.

    Types of Arrays:

    One Dimensional Array Multi Dimensional Array

  • 7/28/2019 c and c++ report

    9/29

    CS320 - C Programming 9

    Pointers

    A pointer is a variable that contains amemory address

    Typically, a pointer contains the addressof a variable

    Addressing operator (&) calculates the

    address a its operand.&x /* expression value == address of x */

  • 7/28/2019 c and c++ report

    10/29

    CS320 - C Programming 10

    Pointers

    Declaring a pointerint *iPtr;

    float *fPtr;

    iptr is a pointer to an integer

    fptr is a pointer to a float

    initializing pointers:int idx = 100;

    int *ptr = &idx;

    OR

    ptr = &idx;

  • 7/28/2019 c and c++ report

    11/29

    swap the value of two variables

  • 7/28/2019 c and c++ report

    12/29

    CS320 - C Programming 12

    Storage Classes

    Determines where the memory isallocated and how long it remains in

    existance storage classes:

    o auto

    o extern

    o static

    o register

  • 7/28/2019 c and c++ report

    13/29

    CS320 - C Programming 13

    auto Storage Class

    Default storage class for variablesdeclared in the body of a function

    Memory automatically release on exitfrom control block

    Example:main() {int age;

    auto int idx;

    }

  • 7/28/2019 c and c++ report

    14/29

    CS320 - C Programming 14

    extern Storage Class

    Default storage class for variables definedoutside a function body.

    Memory allocated for the life of process

    Initialized to zero or initial value.

    Visable to functions that followdefinition

    Example:int age;

    main() {...}

  • 7/28/2019 c and c++ report

    15/29

    CS320 - C Programming 15

    static Storage Class

    Memory allocated for the life of theprocess

    Initialized to zero or initial value.

    Visable to containing block

    Maintains value over invocations

    Example:myfunc() {

    static int age;

    }

  • 7/28/2019 c and c++ report

    16/29

    CS320 - C Programming 16

    register Storage Class

    Compiler recommendation use CPUregister

    otherwise set to auto

    can only be part of control block

    myfunc()

    {

    register int age;

    }

  • 7/28/2019 c and c++ report

    17/29

    Classifications of Data Types

    Built-in data types

    Fundamental data types (int, char, double,float, void,

    pointer) Derived data types (array, string, structure)

    Programmer-defined data types

    Structure Union

    Enumeration

  • 7/28/2019 c and c++ report

    18/29

  • 7/28/2019 c and c++ report

    19/29

    Global and Local Variables

    Local

    Variables These variables are

    declared inside some

    functions.

    Life time of a local

    variable is the entireexecution period of the

    function in which it is

    defined.

    Cannot be accessed by any

    other function.

    In general variablesdeclared inside a block

    are accessible only in

    that block.

    /* Compute Area and Perimeter of a

    circle */

    #include float pi = 3.14159; /* Global */

    main() {

    float rad; /* Local */

    printf( Enter the radius );

    scanf(%f , &rad);

    if ( rad > 0.0 ) {

    float area = pi * rad * rad;

    float peri = 2 * pi * rad;

    printf( Area = %f\n , area );

    printf( Peri = %f\n , peri );

    }else

    printf( Negative radius\n);

    printf( Area = %f\n , area );

    }

  • 7/28/2019 c and c++ report

    20/29

    Global and Local Variables

    Global

    Variables These variables are

    declared outside all

    functions.

    Life time of a global

    variable is the entireexecution period of the

    program.

    Can be accessed by any

    function defined below

    the declaration, in a

    file.

    /* Compute Area and Perimeter of a

    circle */

    #include float pi = 3.14159; /* Global */

    main() {

    float rad; /* Local */

    printf( Enter the radius );

    scanf(%f , &rad);

    if ( rad > 0.0 ) {

    float area = pi * rad * rad;

    float peri = 2 * pi * rad;

    printf( Area = %f\n , area );

    printf( Peri = %f\n , peri );

    }else

    printf( Negative radius\n);

    printf( Area = %f\n , area );

    }

  • 7/28/2019 c and c++ report

    21/29

    An Overview of C++

  • 7/28/2019 c and c++ report

    22/29

    Introduction

    C++ is the C programmers answer to Object-Oriented Programming (OOP).

    C++ is an enhanced version of the C

    language. C++ adds support for OOP without

    sacrificing any of Cs power, or flexibility.

    C++ was invented in 1979 by BjarneStroustrup at Bell Laboratories in MurrayHill, New Jersey, USA.

    22

  • 7/28/2019 c and c++ report

    23/29

    What is OOP?

    OOP is a powerful way to approach the taskof programming.

    OOP encourages developers to decompose a

    problem into its constituent parts. Each component becomes a self-contained

    object that contains its own instructions anddata that relate to that object.

    So, complexity is reduced and theprogrammer can manage larger programs.

    23

  • 7/28/2019 c and c++ report

    24/29

    -

    24

    #include

    int main()

    {/* program code */return 0;

    }

    Sample of C++ program

  • 7/28/2019 c and c++ report

    25/29

    Classes: A First Look

    General syntax -

    25

    classclass-name{

    //private functions and variablespublic:

    //public functions and variables}object-list (optional);

  • 7/28/2019 c and c++ report

    26/29

  • 7/28/2019 c and c++ report

    27/29

  • 7/28/2019 c and c++ report

    28/29

    C++ Program#include#includeclass programming

    {private:int variable;

    public:void input_value()

    { cout > variable; }

    void output_value()

    { cout

  • 7/28/2019 c and c++ report

    29/29

    Thank You