Chapter Ten Developing UNIX Applications In C and
















































- Slides: 48

Chapter Ten Developing UNIX Applications In C and C++

Lesson A C Language Programming 2

Objectives Create simple C programs Debug C programs Use the make utility to revise and maintain source files Identify program errors and fix them Create a simple C programming application 3

Introducing C Programming C is the language in which UNIX was developed and refined C programming may be described as a language that uses relatively short, isolated functions to break down large complex tasks into small and easily resolved subtasks This function-oriented design allows programmers to create their own program functions to interact with the predefined system functions to create powerful and comprehensive solutions to applications 4

Creating a C Program A C program consists of separate bodies of code known as functions When these functions are put together in a collection, they become a program Within the program, the functions call each other as needed and work to solve the problem for which the program was originally designed 5

6

Creating a C Program 7

Creating a C Program The C library consist of functions that perform file, screen, and keyboard operations and when you need to perform one of these operations, you place a function call to it In terms of program format, every C function must have a name, and every C program must have a function called main To include comments in a C program, begin the comment line with /* and end the comment with */ 8

Creating a C Program Using the preprocessor #include directive allowed for the output shown here 9

Creating a C Program 10

Creating a C Program 11

Creating a C Program Characters are represented internally in a single byte of computer memory, and when representing data in a program as a character constant, enclose it in singe quotes A string is a group of characters, like a name, which are stored in memory in consecutive locations, and when used as constants in a program, must be enclosed in double quotes 12

Creating a C Program Identifiers are meaningful names given to variables and functions Variables must be declared before using them in a program, and 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 defined – if this is done in a function, it is an automatic variable and if done outside a function, it is an external or global variable 13

Creating a C Program 14

15

16

Generating Formatted Output with printf The output of a simple C program 17

if Statements and C Loops C programs can use if statements to allow the program to make decisions depending on whether a condition is true or false C provides three looping mechanisms: the for loop, the while loop, and the do-while loop 18

if Statements and C Loops The C loop generated the output here 19

Defining Functions When a function is defined, its name is declared and its statements are written If a function is to return a value to the code that called it, the data type of the return value must be declared, too Sometimes it is necessary to pass data to a function and this data is called an argument 20

Defining Functions This function received an argument and returned a value as a result of processing the argument 21

Working with Files in C Files are continuous streams of data and they are 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 22

Working with Files in C File input and output (I/O) is performed with many functions in C – 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 23

Using the Make Utility to Maintain Program Source Files Some programs have many files of source code which must be compiled and linked together Once compiled, the separate object files are linked together into the executable code 24

Using the Make Utility to Maintain Program Source Files Two files were compiled and linked to form this source code 25

Using the Make Utility to Maintain Program Source Files These multimodule source files can become quite difficult to maintain, especially when only a subset of the files are updated and need compiling This is where the make utility helps in that it tracks what needs to be recompiled by using the time stamp field for each source file All that is necessary is a control file, called the makefile, which lists the source files and their relationship to each other 26

Using the Make Utility to Maintain Program Source Files The make utility helped in generating the program seen here 27

Using the Make Utility to Maintain Program Source Files The make utility helped in creating this multimodule program 28

Debugging Your Program There are many opportunities to make errors in your C programs and the compiler will identify errors made Common errors include: incorrect syntax, missing semicolons, case-sensitive errors To correct syntax errors, follow these steps: – Write down the line number and a brief description – Edit the source file – Save and recompile the file 29

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 keep in mind that this usage can lead to difficult and cumbersome code 30

Creating a C Program to Accept Input 31

Creating a C Program to Accept Input 32

Creating a C Program to Accept Input An example of using C to accept keyboard input 33

Encoding and Decoding Programs Use the make command to help with encoding and decoding 34

Encoding and Decoding Programs This program requests the name of the file to encode 35

Encoding and Decoding Programs The file’s contents are encoded, which is what happens when you encrypt a file 36

Encoding and Decoding Programs Once files are encoded, or encrypted, they can be decoded or decrypted 37

Lesson B C++ Programming in a UNIX Environment 38

Objectives Create a C++ program that displays information on the screen Create a C++ program to read a text file Create a C++ program with overload functions Create a C++ program that creates a new class object 39

Introducing C++ Programming C++ is a programming language that builds on C to add object-oriented capabilities C and C++ are similar in many ways C++ uses functions, as does C, but with added dimensions such as function overloading, which makes the functions respond to more than one set of criteria and conditions 40

Introducing C++ Programming The major differences between C and C++ languages are: – C follows procedural principles, whereas C++ follows object-oriented principles – C++ introduces a new data class called object, which is a collection of data and a set of operands called methods which manipulate the data 41

Creating a C++ Program Using C++ instead of C to create a program 42

Creating a C++ Program That Reads a Text File Using C++ instead of C to read text files 43

How C++ Enhances C Functions Function overloading allows C++ to take C functions farther 44

Setting Up a Class A class data structure lets you create abstract data types, such as a class for an object called Cube 45

Chapter Summary The C language concentrates on how best to create commands and expressions that can be elegantly formed from operators and operands C programs often consist of separate files called program modules that are compiled separately into object code and linked to other objects that make up the program The C program structure begins with the execution of instructions located inside a main function that calls other functions that contain more instructions 46

Chapter Summary The make utility is used to maintain the application’s source files The major difference between C and C++ is that C follows procedural principles and C++ primarily follows object-oriented programming The standard stream library used by C++ is iostream. h C++ provides two statements for standard input and output: cin and cout respectively 47

Chapter Summary C++ offers a way to define a function so that it can handle multiple sets of criteria – this is called overloading endl skips a line like “n” does in the C language You should use a class in C++ when your program performs specific operations on the data 48