Programming Chapter 1 Week-c++

Embed Size (px)

Citation preview

  • 8/13/2019 Programming Chapter 1 Week-c++

    1/25

  • 8/13/2019 Programming Chapter 1 Week-c++

    2/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 2

    TOPICObjectives:

    TOPIC

    Objectives:

    To introduce C language and its history

    How to use the compiler for this course Writing and running a C program

    Handling errors

  • 8/13/2019 Programming Chapter 1 Week-c++

    3/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 3

    1.1IntroductiontoCLanguage

    1.1 Introduction to C Lan

    guage

    Types of programming languagesTypes of programming languages

    There are many different languages can be used to program a

    computer.

    The most basic of these is machine language. This is the natural

    dialect of the computer.

    Usually, a computer program will be written in some high-level

    programming language, whose instruction set is more compatible

    with human languages.

    Most of these are general-purpose languages such as C.

    A program that is written in a high-level language, however, must

    be translated into machine languagebefore it can be executed.

    This is known as compilationor interpretation

    compiler

    interpreter

    C

  • 8/13/2019 Programming Chapter 1 Week-c++

    4/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 4

    C is high-level language,

    general-purpose programming language

    History of CHistory of C

    High-level language: instruction set is more compatible with human

    languages

    General-purpose: C, Pascal, Fortran, BASIC

    Special-purpose: specifically designed for some particular type of

    application.LISP, a list processing language that is widely used for artificial

    intelligence applications.

    main() { ... }

    Find other special-purpose programming language

    High-level

  • 8/13/2019 Programming Chapter 1 Week-c++

    5/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 5

    C was originally developed in the 1970s by Dennis Ritchie,

    at Bell Laboratories, Inc.and implemented on PDP 11

    Dennis RitchieBCPL

    It is an outgrowth oftwo earlier languages

    B

    By Martin Richards

    By Ken Thompson

    Typeless languagesUsed to create early version

    of the UNIX Operating

    System

    C initially become widely known as the development language of the

    UNIX operating system.

    Note: Programmed Data Processor model 11 (PDP-11)

  • 8/13/2019 Programming Chapter 1 Week-c++

    6/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 6

    Original C...

    1980 ...

    Traditional CClassic C

    Kernighan and Ritchie C (K&R)

    ANSI C

    Standard C

    ANSI:American National Standards Institute

    K&R/ANSI C

    Note:

    Because C is a standardized, hardware-independent, widely available language,applications written in C can often be run with little or no modifications on a widerange of different computer systems.

  • 8/13/2019 Programming Chapter 1 Week-c++

    7/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 7

    Advantages

    of C Language

    Small language - 32 keywords

    Native language of UNIX

    Used for systems programmingas well asapplications programming

    Portable

    Modular

    Subset of C++

    Efficient

    Advantages

    Systems programming: e.g., for writing operating systems

    Applications programming: e.g., for writing a program to solve a complicated

    system of mathematical equations, or for writing a program to bill customers

    Advantages of C Language

  • 8/13/2019 Programming Chapter 1 Week-c++

    8/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 8

    32 ANSI C Standard Keywords

    auto double int struct

    break else long switch

    case enum register typedefchar extern return union

    const float short unsigned

    continue for signed void

    default goto sizeof volatile

    do if static while

    ANSI C Keywords32 ANSI C Standard Keywords

    Note: C and C++ keywords

  • 8/13/2019 Programming Chapter 1 Week-c++

    9/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 9

    Programming Preparation

    A Compiler translates Source codeto Object codethat isexecutable.

    On MS-DOS systems this compiled code is automaticallycreated in a file with the same name as the .Cfile, but with the.exeextension replacing the .C extension.

    Source/Object CodeProgramming Preparation

    Source Code: C Program

    main()

    { }

    Mycode.c

    Object Code: Machine Language

    10110 ..

    01101 ..

    ...

    Mycode.exe

    How to run the object codefrom C prompt

  • 8/13/2019 Programming Chapter 1 Week-c++

    10/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 10

    Editor/Compiler

    Write, edit, save,

    retrieve, ...

    Translate program toobject code/

    executable code

    Borland Turbo C/C++integrate the Editor and the Compiler

    C CompilerC Compiler

    How to run the compiler ?

    1. Change directory to

    2. Type tc

    3. Press ENTER

    c:\tc\bin

  • 8/13/2019 Programming Chapter 1 Week-c++

    11/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 11

    Borland Turbo C/C++ IDE

    IDE

    EditingWindow

    SystemMenu

    OutputWindow

    Borland Turbo C/C++ IDE

    Note: IDE :Integrated Development Environment

  • 8/13/2019 Programming Chapter 1 Week-c++

    12/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 12

    Borland Turbo C/C++ IDE:

    System Menu

    Press Alt+F

    Press AltorAlt+Red Character

    to walk aroundthe system menu

    Borland Turbo C/C++ IDE: System Menu

    Menu A:Menu A:

    Menu B:Menu B:

    System Menu

  • 8/13/2019 Programming Chapter 1 Week-c++

    13/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 13

    Getting HelpGetting Help

    You can learn about the compiler through the online Help system.

    Online Help

    Select

    to get

    help

  • 8/13/2019 Programming Chapter 1 Week-c++

    14/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 14

    Compile SuccessCompile Success Select Compile to compile

    No errorNote:

    You can ignore the warnings.

  • 8/13/2019 Programming Chapter 1 Week-c++

    15/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 15

    Compile With ErrorCompile with error

    Select Compile

    to compile

    Two syntaxerrors

    Message Window

    describes the

    Err ors & Warnings

  • 8/13/2019 Programming Chapter 1 Week-c++

    16/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 16

    10110 ..

    01101 ..

    ...

    How the compiler works

    Source Code Object Code

    Correct the syntax errors.

    Correct the logic errors.

    Edit/Modifyto meet system requirements.

    Source/Object

    main()

    { }

    C / C++

    Compiler

    How the compiler works

    Make sure to save your edited program before

    recompile it.

  • 8/13/2019 Programming Chapter 1 Week-c++

    17/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 17

    Basic C Program Structure

    Every C program contains main function called: main()

    Program execution start from this main function.

    main(){ }

    Type using text editor (from your compiler) Save with any name and with .cor .cpp

    extension.

    Compileandrun

    main() functionBasic C Program Structure

    main() function has two parts:

    main( ){ ...}

    (i) Head

    (ii) BodyFunction

    Note:

    curly braces {}

  • 8/13/2019 Programming Chapter 1 Week-c++

    18/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 18

    Program saved

    with sample.cname.

    EditingWindow

    Is there anything you can see from your output window

    Screen A:

    Screen B:

    Screen A:

    Screen B:

    Steps of compiling

  • 8/13/2019 Programming Chapter 1 Week-c++

    19/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 19

    Program That Generate Output C ProgramProgram That Generate output

    /* This is simple program that print a message*/

    #include

    main()

    {

    printf(C is my first programming language.\n);

    }

    Output:C is my first programming language.

  • 8/13/2019 Programming Chapter 1 Week-c++

    20/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 20

    Descriptions

    Program Descriptions

    Descriptions/* This is simple program that print a message*/

    #include

    main()

    {

    printf(C is my first programming language.\n);

    }

    Anything written between the

    /*and */is a commentand is

    ignored by the compiler.

    The line that started with#is

    called as preprocessor directive. Here we have processor directive #includewhich include the stdio.hfile into source program. stdio.his one of compiler

    components which called as header file or library file. In this example, we

    include the stdio.hbecause this file define the printf()function.

    Indicate the beginning of main program.

    The printf()is one of the library function that defined in stdio.h

    header file. printf() is an output function that prints the strings inside the

    quotations.

    Indicate end of main program.Note:

    Any code/statements mustbe written inside thebody.

  • 8/13/2019 Programming Chapter 1 Week-c++

    21/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 21

    What format you should follow ?Program FormatWhat format you should follow ?

    /*

    Name: ABDULLAH BIN ISMAIL

    Subject Code: DIT 202

    Class Code: FT007

    Program file name:myprog.cpp

    Purpose: Print simple message on screen

    Start Date: August 31, 1999

    Complete Date: August 31, 1999

    */

    #include

    main(){ clrscr();

    printf(C is my first programming language.\n);

    }

    /* End of program: myprog.cpp*/

    All your programming exercises should follow the format described below.

    #include

    main(){

    printf(I love C language very much\n);

    }

    Comment that

    describe theprogrammerand theprogram.

    Comment toindicate the endof program.

    Code withindentation.

    http://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_10/indent.ppt
  • 8/13/2019 Programming Chapter 1 Week-c++

    22/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 22

    Header FilesHeader Files

    Header File/Library Function

    Header files contain definitions of library functionsand variables whichcan be incorporated into any C program by using the pre-processor #include

    statement (see 1.4).

    * Check yourcompiler on-line

    help for further

    details.

    stdio.h

    Header Files

    conio.h

    Library Functions

    scanf()

    printf()

    :getch()

    clrscr()

    textcolor()textbackground():

    Example:

  • 8/13/2019 Programming Chapter 1 Week-c++

    23/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 23

    List of Header Files (41)alloc.h assert.h bcd.h bios.h complex.h

    conio.h

    ctype.h dir.h dirent.h dos.h

    errno.h fcntl.h float.h fstream.h generic.h

    graphics.h io.h iomanip.h iostream.h limits.h

    locale.h malloc.h math.h mem.h process.h

    setjmp.h share.h signal.h stdarg.h stddef.h

    stdio.h stdiostr.h stdlib.h stream.h string.hstrstrea.h sys\stat.h sys\timeb.h sys\types.h time.h

    values.h

    Example:

    Standard header files are provided with each compiler, and cover a

    range of areas, string handling, mathematical, data conversion, printing

    and reading of variables.

    Note:Based on Borland Turbo C++ Compiler

  • 8/13/2019 Programming Chapter 1 Week-c++

    24/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 24

    Library Functions from stdio.hclearerr fclose fcloseall fdopen feof ferror

    fflush fgetc fgetchar fgetpos fgets fileno

    flushall fopen fprintf fputc fputchar fputs

    fread freopen fscanf fseek fsetpos ftellfwrite getc getchar gets getw perror

    printf putc putchar puts putw remove

    rename rewind rmtmp scanf setbuf setvbufsprintf sscanf strerror _strerror tempnam tmpfile

    tmpnam ungetc unlink vfprintf vfscanf vprintf

    vscanf vsprintf vsscanf

    Example:

    List of library functions from stdio.h header file.

    Note:printf()- output functionscanf()- input function

  • 8/13/2019 Programming Chapter 1 Week-c++

    25/25

    FCS 0084 Programming Chapter 1

    Introduction to C Week 1

    Management and Science University 25

    To use any of the standard functions, the appropriate header file should be

    included. This is done at the beginning of the C source file. For example, to use

    the functionprintf()in a program, the line

    #include

    should be at the beginning of the source file, because the definition forprintf()

    is found in the filestdio.hAll header files have the extension .h and generally

    reside in the /includesubdirectory.