Title: Chapter Ten
1Chapter Ten
- Developing UNIX Applications
- In C and C
2Lesson A
3Objectives
- 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
4Introducing 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
5Creating 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
6(No Transcript)
7Creating a C Program
8Creating 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 /
9Creating a C Program
Using the preprocessor include directive allowed
for the output shown here
10Creating a C Program
11Creating a C Program
12Creating 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
13Creating 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
14Creating a C Program
15(No Transcript)
16(No Transcript)
17Generating Formatted Outputwith printf
The output of a simple C program
18if 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
19if Statements and C Loops
The C loop generated the output here
20Defining 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
21Defining Functions
This function received an argument and returned a
value as a result of processing the argument
22Working 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
23Working 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
24Using the Make Utility toMaintain 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
25Using the Make Utility toMaintain Program Source
Files
Two files were compiled and linked to form this
source code
26Using the Make Utility toMaintain 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
27Using the Make Utility toMaintain Program Source
Files
The make utility helped in generating the program
seen here
28Using the Make Utility toMaintain Program Source
Files
The make utility helped in creating this
multimodule program
29Debugging 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
30Creating 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
31Creating a C Program to Accept Input
32Creating a C Program to Accept Input
33Creating a C Program toAccept Input
An example of using C to accept keyboard input
34Encoding and Decoding Programs
Use the make command to help with encoding and
decoding
35Encoding and Decoding Programs
This program requests the name of the file to
encode
36Encoding and Decoding Programs
The files contents are encoded, which is what
happens when you encrypt a file
37Encoding and Decoding Programs
Once files are encoded, or encrypted, they can be
decoded or decrypted
38Lesson B
- C Programming in a
- UNIX Environment
39Objectives
- 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
40Introducing 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
41Introducing 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
42Creating a C Program
Using C instead of C to create a program
43Creating a C Program That Reads a Text File
Using C instead of C to read text files
44How C Enhances C Functions
Function overloading allows C to take C
functions farther
45Setting Up a Class
A class data structure lets you create abstract
data types, such as a class for an object called
Cube
46Chapter 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
47Chapter Summary
- The make utility is used to maintain the
applications 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
48Chapter 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