chapter 10: developing unix/linux applications in c and c++ guide to unix using linux third edition

41
Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Upload: daniel-davis

Post on 21-Dec-2015

243 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Chapter 10:Developing UNIX/Linux Applications in C and

C++

Guide To UNIX Using Linux Third Edition

Page 2: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 2

Objectives

• Understand basic elements of C programming

• Debug C programs

• Create, compile, and test C programs

• Use the make utility to revise and maintain source files

Page 3: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 3

Objectives (continued)

• Identify differences between C and C++ programming

• Create a simple C++ program

• Create a C++ program that reads a text file

• Create a C++ program that demonstrates how C++ enhances C functions

Page 4: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 4

Introducing C Programming• C is the language in which UNIX was developed and

refined

• C uses relatively short, isolated functions to break down large complex tasks into small and easily resolved subtasks

• C’s function-oriented design allows programmers to create their own functions to interact with the predefined system functions

Page 5: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 5

Creating a C Program

• A C program consists of separate bodies of code known as functions

• The functions call each other as needed and work to solve the problem for which the program was originally designed

Page 6: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 6

Creating a C Program (continued)

Page 7: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 7

Creating a C Program (continued)

Page 8: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 8

The C Library

• The core C language is very small

• C library consists of functions for many common activities including:

– File, screen, and keyboard operations

– String operations

– Memory allocation and control

– Math operations

Page 9: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 9

Program Format

• A program includes 1 or more functions

• Each program must have a main() function

• Format for main is:

int main()

{

}

Page 10: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 10

Including Comments

• Comments begin with /* and end with */

• Compiler ignores everything in the comment

• Comments can be anywhere in the file

Page 11: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 11

Using the Preprocessor #include Directive

Using the preprocessor #include directive allowed for the output shown here

Page 12: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 12

Specifying Data Types

Page 13: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 13

Specifying Data Types (continued)

Page 14: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 14

Characters and Strings

• Characters are represented internally in a single byte of computer memory

• Character constants must be enclosed in single quotes

• Strings are represented as arrays of characters

• String constants must be enclosed in double quotes

Page 15: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 15

Variables

• Variables must be declared before use

– Declarations begin with a data type followed by one or more variable names

• The scope of a variable is the part of the program in which the variable is accessible

• Global variables can be accessed anywhere within a program

Page 16: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 16

Using Math Operators

Page 17: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 17

Generating Formatted Outputwith printf()

The output of a simple C program

Page 18: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 18

Generating Formatted Outputwith printf() (continued)

Page 19: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 19

Using the C Compiler

• Command for C compiler in Linux is gcc

– In some UNIX systems, it is cc

• Default executable file produced is a.out

– Specify another name using the –o option

• Information on options and use available at man gcc

Page 20: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 20

if Statements and C Loops

• Use if statements to allow the program to make decisions depending on whether a condition is true or false

• Three looping mechanisms:

– for loop

– while loop

– do-while loop

Page 21: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 21

Using C Loops

A C for loop generated the output in this program

Page 22: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 22

Defining Functions

• When a function is defined, its name is declared and its statements are written

• The data type of the return value must be declared in the function definition

– Or “void” if no return value

• Function prototypes must be included in programs to tell the compiler about functions that will be defined later

Page 23: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 23

Using Function Arguments

• A value passed to a function is an argument

• Arguments are stored in special automatic variables

• Can be referred to from anywhere in the function

Page 24: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 24

Using Function Return Values

• Functions can return values

• Returned values can be used in assignment statements such as y=triple(x)

• Must declare the type of the return value in the function definition

Page 25: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 25

Working with Files in C

• Files are continuous streams of data

– Typically stored on disk

– File pointers point to predefined structures that contain information about the file

– Before using a file, it must be opened

• The library function for this is fopen

– When done with a file, it must be closed

• The library function for this is fclose

Page 26: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 26

Working with Files in C (continued)

• File I/O uses various library functions

– fgetc performs character input

– fputc performs character output

• During an input operation, it is essential to test for the end of a file

– feof tests for the end-of-file marker

Page 27: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 27

Using the make Utility toMaintain Program Source Files

• Some programs have many files of source code

• Once compiled, the separate object files are linked into executable code

• As program is updated, only need to recompile files that have changed

Page 28: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 28

Using the make Utility toMaintain Program Source Files

(continued)

• make utility helps tracks what needs to be recompiled by using the time stamp field for each source file

• A control file, called the makefile, lists the source files and their relationship to each other

Page 29: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 29

Using the make Utility toMaintain Program Source Files

(continued)

• The make utility follows a set of rules

• General rule definition includes

– A target file

– One or more dependencies

– An action that creates the target

Page 30: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 30

Debugging Your Program

• The compiler identifies some types of errors in a program

• Common errors include: incorrect syntax, missing semicolons, case-sensitive errors

• Steps to correct syntax errors:

– Write down the error

– Edit the source file

– Save and recompile the file

Page 31: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 31

Creating a C Program to Accept Input

• Use the scanf function to accept input from the keyboard

• scanf uses a control string with format specified in a manner similar to printf

• scanf can accept multiple inputs, but remember that this can lead to difficult and cumbersome code

Page 32: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 32

Creating a C Program to Accept Input (continued)

Page 33: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 33

Creating a C Program to Accept Input (continued)

Page 34: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 34

Creating a C Program to Accept Input (continued)

An example of using C to accept keyboard input

Page 35: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 35

Introducing C++ Programming

• C++ adds object-oriented capabilities to C

• C and C++ are similar in many ways

• C++ uses functions, as does C, but includes the use of functions as methods for objects

Page 36: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 36

Introducing C++ Programming (continued)

• The major differences between C and C++

– C follows procedural principles, whereas C++ follows object-oriented principles

– C++ introduces “objects”• A collection of data and a set of operations

called methods to manipulate the data

Page 37: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 37

Creating a Simple C++ Program

Using C++ instead of C to create a program

Page 38: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 38

How C++ Enhances C Functions

Function overloading allows C++ to use the same function name for different types of arguments

Page 39: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 39

Chapter Summary

• C programs often consist of separate files called program modules that are compiled separately into object code and linked together to make up the program

• The core C language is small, relying on libraries for extended functionality

• UNIX was developed and refined in C

Page 40: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 40

Chapter Summary (continued)

• A compiler translates source code into object code

• A linker links all object code files plus library functions into an executable file

• The make utility maintains source files

– Only changed files need to be recompiled

Page 41: Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Guide to UNIX Using Linux, Third Edition 41

Chapter Summary (continued)

• C is procedural and C++ is object-oriented

• Function overloading: C++ offers a way to define a function to handle multiple sets of arguments