chapter two introduction to the programming language c

36
1 Chapter Two Chapter Two Introduction to Introduction to the Programming the Programming Language C Language C

Upload: scarlet-pennington

Post on 30-Dec-2015

64 views

Category:

Documents


1 download

DESCRIPTION

Chapter Two Introduction to the Programming Language C. The only way to learn a programming language is by writing programs in that language. The Programming Language C. C is a general-purpose programming language C is developed by Dennis Ritchie at Bell Laboratories - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter Two Introduction to the Programming Language C

1

Chapter TwoChapter Two

Introduction toIntroduction tothe Programming the Programming

Language CLanguage C

Page 2: Chapter Two Introduction to the Programming Language C

2

The only way to learn

a programming language

is by

writing programs in that language

Page 3: Chapter Two Introduction to the Programming Language C

3

The Programming The Programming Language CLanguage C

• C is a general-purpose programming language

• C is developed by Dennis Ritchie at Bell Laboratories

• C has become one of the most widely used languages in the world

Page 4: Chapter Two Introduction to the Programming Language C

4

The C Programming The C Programming SystemSystem

• C programming language

– A set of notations for representing programs

• C standard library

– A set of well-developed programs

• C programming environment

– A set of tools to aid program development

Page 5: Chapter Two Introduction to the Programming Language C

5

Editor

The C Programming The C Programming SystemSystem

Compiler

Debugger

library programsin machine language

user-written programs in C

user-written programsin machine language

language

environment

library

Page 6: Chapter Two Introduction to the Programming Language C

6

The First C ProgramThe First C Program

Print the following words

hello, world

Page 7: Chapter Two Introduction to the Programming Language C

7

The First C ProgramThe First C Program

#include <stdio.h>

main( ) { printf(“hello, world\n”);}

Page 8: Chapter Two Introduction to the Programming Language C

8

ProgramsPrograms

• A C program consists of functions and variables

• A function contains instructions that specify the operations to be done during the computation

• Variables denote memory locations that store data used during the computation

Page 9: Chapter Two Introduction to the Programming Language C

9

FunctionsFunctions

• Functions can be either user-defined functions or library functions

• A C program begins the execution at the beginning of the function named main

• A function may call other functions to help it perform one of its subtasks

Page 10: Chapter Two Introduction to the Programming Language C

10

The First C ProgramThe First C Program

#include <stdio.h>/* include library information

*/main( ) /* name of starting function */{ /* beginning of instructions */

printf(“hello, world\n”);/* call library function */

} /* end of instructions */

Page 11: Chapter Two Introduction to the Programming Language C

11

ArgumentsArguments

• One method of communicating data between functions is for the calling function to provide a list of values, called arguments, to the called function

• The parentheses after the function name surround the argument list

Page 12: Chapter Two Introduction to the Programming Language C

12

StringsStrings• A character string or string constant is

a sequence of characters enclosed in double quotes

• The backslash \ in a string is used as an escape character. It is used for representing invisible characters

• Some common escape sequences\\ backslash \b backspace\n newline \t tab\" double quote \' single quote

Page 13: Chapter Two Introduction to the Programming Language C

13

C Programming C Programming EnvironmentEnvironment

Edit

Compile

Link

Execute

prog1.c, prog1.h, prog2.c, prog2.h

prog1.obj, prog2.obj

prog.exe

lib.h

lib.obj

input outputDebug

Page 14: Chapter Two Introduction to the Programming Language C

14

C Programming C Programming EnvironmentEnvironment

• Edit: create the high-level language program files using the editor

• Compile: translate the high-level language program into the machine language program using the compiler

• Link: combine user's machine language program and the library using the linker

Page 15: Chapter Two Introduction to the Programming Language C

15

C Programming C Programming EnvironmentEnvironment

• Execute: run the combined machine language program

• Debug: correct the errors in the program– syntax errors

– linking errors

– runtime errors

– logical errors

Page 16: Chapter Two Introduction to the Programming Language C

16

Microsoft Visual C++Microsoft Visual C++

Page 17: Chapter Two Introduction to the Programming Language C

17

Creating the Source Creating the Source FileFile

Page 18: Chapter Two Introduction to the Programming Language C

18

Creating the Source Creating the Source FileFile

Page 19: Chapter Two Introduction to the Programming Language C

19

Editing the Source FileEditing the Source File

Page 20: Chapter Two Introduction to the Programming Language C

20

Saving the Source FileSaving the Source File

Page 21: Chapter Two Introduction to the Programming Language C

21

Saving the Source FileSaving the Source File

Page 22: Chapter Two Introduction to the Programming Language C

22

Saving the Source FileSaving the Source File

Page 23: Chapter Two Introduction to the Programming Language C

23

Compiling the Source Compiling the Source FileFile

Page 24: Chapter Two Introduction to the Programming Language C

24

Compiling the Source Compiling the Source FileFile

Page 25: Chapter Two Introduction to the Programming Language C

25

Compiling the Source Compiling the Source FileFile

Page 26: Chapter Two Introduction to the Programming Language C

26

Linking the Object FilesLinking the Object Files

Page 27: Chapter Two Introduction to the Programming Language C

27

Linking the Object FilesLinking the Object Files

Page 28: Chapter Two Introduction to the Programming Language C

28

Executing the Executing the Executable FileExecutable File

Page 29: Chapter Two Introduction to the Programming Language C

29

Executing the Executing the Executable FileExecutable File

Page 30: Chapter Two Introduction to the Programming Language C

30

Executing the Executing the Executable FileExecutable File

Page 31: Chapter Two Introduction to the Programming Language C

31

Debugging Syntax Debugging Syntax ErrorsErrors

Page 32: Chapter Two Introduction to the Programming Language C

32

Debugging Linking Debugging Linking ErrorsErrors

Page 33: Chapter Two Introduction to the Programming Language C

33

Debugging Runtime Debugging Runtime ErrorsErrors

Page 34: Chapter Two Introduction to the Programming Language C

34

Debugging Logical Debugging Logical ErrorsErrors

Page 35: Chapter Two Introduction to the Programming Language C

35

Debugging Logical Debugging Logical ErrorsErrors

• All programmers make logic errors (bugs). In particular, you will make logic errors

• Good programmers is ones who take pains to minimize the number of bugs that persist in the finished code

• Always be skeptical of your own programs and test them as thoroughly as you can

Page 36: Chapter Two Introduction to the Programming Language C

36

Software MaintenanceSoftware Maintenance• Software requires maintenance

– Bug repair– Feature enhancement

• The cost of software maintenance constitutes between 80 and 90 percent of the total cost

• Software engineering is the discipline of writing programs so that they can be understood and maintained by others

• Good programming style requires developing an aesthetic sense