Top Banner
Programming Language C Tutorial Introduction 主主主 主主主
162

Programming Language C Tutorial Introduction 主講人:虞台文.

Jan 01, 2016

Download

Documents

Eleanor Gaines
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

主講人:虞台文

Page 2: Programming Language  C Tutorial Introduction 主講人:虞台文.

Content Background Getting Started IDE (Integrated Development Environment) Main Parts of C Programs Variables and Arithmetic Expressions The for statement Symbolic Constants Character Input and Output Arrays Functions Character Arrays External Variables and Scope

Page 3: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Background

Page 4: Programming Language  C Tutorial Introduction 主講人:虞台文.

Textbooks

1. C How to Program, 5th Edition, by Harvey M. Deitel , Paul J. Deitel.

2. The C programming Language, 2nd Editon, by Brian W. Kernighan and Dennis M. Ritchie, Prentice-Hall in 1988.

Page 5: Programming Language  C Tutorial Introduction 主講人:虞台文.

Grading

1. Homework 40%

2. Midterm 30%

3. Final 30%

4. Other 10%

Page 6: Programming Language  C Tutorial Introduction 主講人:虞台文.

The C Language

Currently, the most commonly-used language for embedded systems– High-level assembly

Very portable– compilers exist for virtually every processor

Easy-to-understand compilation Produces efficient code Fairly concise

Page 7: Programming Language  C Tutorial Introduction 主講人:虞台文.

C History

Developed between 1969 and 1973 along with Unix

Due mostly to Dennis Ritchie

Designed for systems programming– Operating systems

– Utility programs

– Compilers

– Filters

Evolved from B, which evolved from BCPL

The Development of the C Language

Page 8: Programming Language  C Tutorial Introduction 主講人:虞台文.

Computer Architecture

ALUALU

ControlControl

CPU

InputInput OutputOutput

MemoryMemory

InputDevice

OutputDevice

Page 9: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Getting Started

Page 10: Programming Language  C Tutorial Introduction 主講人:虞台文.

Setting Program Development Environment

Page 11: Programming Language  C Tutorial Introduction 主講人:虞台文.

Setting Program Development Environment

Page 12: Programming Language  C Tutorial Introduction 主講人:虞台文.

Setting Program Development Environment

Page 13: Programming Language  C Tutorial Introduction 主講人:虞台文.

Setting Program Development Environment

Page 14: Programming Language  C Tutorial Introduction 主講人:虞台文.

Setting Program Development Environment

Page 15: Programming Language  C Tutorial Introduction 主講人:虞台文.

Setting Program Development Environment

%SystemRoot%\system32\cmd.exe/K "F:\VS6\VC98\Bin\VCVARS32.BAT"

%SystemRoot%\system32\cmd.exe/K "F:\VS6\VC98\Bin\VCVARS32.BAT"

Page 16: Programming Language  C Tutorial Introduction 主講人:虞台文.

Setting Program Development Environment

Page 17: Programming Language  C Tutorial Introduction 主講人:虞台文.

Our First C Program Hello World

Page 18: Programming Language  C Tutorial Introduction 主講人:虞台文.

Our First C Program Hello World

#include <stdio.h>

main(){

printf("hello, world\n");}

#include <stdio.h>

main(){

printf("hello, world\n");}

Page 19: Programming Language  C Tutorial Introduction 主講人:虞台文.

Our First C Program Hello World

#include <stdio.h>

main(){

printf("hello, world\n");}

#include <stdio.h>

main(){

printf("hello, world\n");}

include information about standard

library

include information about standard

library

define a function called mainthat received no argument

values

define a function called mainthat received no argument

values

Page 20: Programming Language  C Tutorial Introduction 主講人:虞台文.

Our First C Program Hello World

#include <stdio.h>

main(){

printf("hello, world\n");}

#include <stdio.h>

main(){

printf("hello, world\n");}

define a function called mainthat received no argument

values

define a function called mainthat received no argument

values

Page 21: Programming Language  C Tutorial Introduction 主講人:虞台文.

Our First C Program Hello World

#include <stdio.h>

main(){

printf("hello, world\n");}

#include <stdio.h>

main(){

printf("hello, world\n");}

statements of main are enclosed in

braces

statements of main are enclosed in

braces

Page 22: Programming Language  C Tutorial Introduction 主講人:虞台文.

Our First C Program Hello World

#include <stdio.h>

main(){

printf("hello, world\n");}

#include <stdio.h>

main(){

printf("hello, world\n");}

main calls library function printf

main calls library function printf

Page 23: Programming Language  C Tutorial Introduction 主講人:虞台文.

Our First C Program Hello World

Page 24: Programming Language  C Tutorial Introduction 主講人:虞台文.

Compiling and Linking

Page 25: Programming Language  C Tutorial Introduction 主講人:虞台文.

Compiling and Linking

Page 26: Programming Language  C Tutorial Introduction 主講人:虞台文.

Compiling and Linking

Page 27: Programming Language  C Tutorial Introduction 主講人:虞台文.

Compiling and Linking

Page 28: Programming Language  C Tutorial Introduction 主講人:虞台文.

Compiling and Linking

Page 29: Programming Language  C Tutorial Introduction 主講人:虞台文.

Compiling and Linking

Page 30: Programming Language  C Tutorial Introduction 主講人:虞台文.

Execution

Page 31: Programming Language  C Tutorial Introduction 主講人:虞台文.

Execution

Page 32: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercises

1. Create some typo errors for Hello.c, and redo the compiling/linking process. Watch and understand the screen output reported by cl.

2. Add more printf statements in Hello.c and redo the compiling/linking/execution process.

Page 33: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

IDE

Page 34: Programming Language  C Tutorial Introduction 主講人:虞台文.

Visual C++ 6.0

Page 35: Programming Language  C Tutorial Introduction 主講人:虞台文.

Visual C++ 6.0

Page 36: Programming Language  C Tutorial Introduction 主講人:虞台文.

Visual C++ 6.0

Page 37: Programming Language  C Tutorial Introduction 主講人:虞台文.

Win32 Console Application

Page 38: Programming Language  C Tutorial Introduction 主講人:虞台文.

Win32 Console Application

Page 39: Programming Language  C Tutorial Introduction 主講人:虞台文.

Win32 Console Application

Page 40: Programming Language  C Tutorial Introduction 主講人:虞台文.

Hello_IDE

Page 41: Programming Language  C Tutorial Introduction 主講人:虞台文.

Hello_IDE

Page 42: Programming Language  C Tutorial Introduction 主講人:虞台文.

Hello_IDE

Page 43: Programming Language  C Tutorial Introduction 主講人:虞台文.

Hello_IDE

Page 44: Programming Language  C Tutorial Introduction 主講人:虞台文.

Hello_IDE

Page 45: Programming Language  C Tutorial Introduction 主講人:虞台文.

Hello_IDE

Page 46: Programming Language  C Tutorial Introduction 主講人:虞台文.

Hello.c

Page 47: Programming Language  C Tutorial Introduction 主講人:虞台文.

Building Application

Page 48: Programming Language  C Tutorial Introduction 主講人:虞台文.

Building Application

Page 49: Programming Language  C Tutorial Introduction 主講人:虞台文.

Execution

Page 50: Programming Language  C Tutorial Introduction 主講人:虞台文.

Execution

Page 51: Programming Language  C Tutorial Introduction 主講人:虞台文.

Files Generated by IDE

Page 52: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Main Parts of C Programs

Page 53: Programming Language  C Tutorial Introduction 主講人:虞台文.

Main Parts

Headers/Comments/Expressions

#include <stdio.h>

/* Our first C Program */

main(){

printf("hello, world\n");}

#include <stdio.h>

/* Our first C Program */

main(){

printf("hello, world\n");}

Page 54: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example Identify the three parts in the following program

Page 55: Programming Language  C Tutorial Introduction 主講人:虞台文.

Remark C++ also uses // to comment a single line.

Page 56: Programming Language  C Tutorial Introduction 主講人:虞台文.

The Syntax of C Language

Page 57: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Variables and Arithmetic Expressions

Page 58: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example:Fahrenheit-Celsius table

59 ( 32)C F

Page 59: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example:Fahrenheit-Celsius Table

59 ( 32)C F

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

Page 60: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example:Fahrenheit-Celsius Table

59 ( 32)C F

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

Page 61: Programming Language  C Tutorial Introduction 主講人:虞台文.

Statements59 ( 32)C F

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

Individual statements are terminated by semicolons.

Individual statements are terminated by semicolons.

Page 62: Programming Language  C Tutorial Introduction 主講人:虞台文.

Variable Declarations

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

In C, all variables must be declared before they are used.

– Usually declared at the beginning of the function before any executable statements.

A declaration announces the properties of variables; it consists of a name and a list of variables.

In C, all variables must be declared before they are used.

– Usually declared at the beginning of the function before any executable statements.

A declaration announces the properties of variables; it consists of a name and a list of variables.

59 ( 32)C F

Page 63: Programming Language  C Tutorial Introduction 主講人:虞台文.

Variable Declarations

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

59 ( 32)C F

• The integer data type.• What is its range?

• The integer data type.• What is its range?

Page 64: Programming Language  C Tutorial Introduction 主講人:虞台文.

Integer

The range of integer depends on

machine

– 16-bit machine

32768 +32767

– 32-bit machine

2147483646 +2147483647

int fahr, celsius;int lower, upper, step;

int fahr, celsius;int lower, upper, step;

Page 65: Programming Language  C Tutorial Introduction 主講人:虞台文.

Integer

The range of integer depends on

machineINT_MIN INT_MAX

int fahr, celsius;int lower, upper, step;

int fahr, celsius;int lower, upper, step;

Page 66: Programming Language  C Tutorial Introduction 主講人:虞台文.

Integer

The range of integer depends on

machineINT_MIN INT_MAX

int fahr, celsius;int lower, upper, step;

int fahr, celsius;int lower, upper, step;

Page 67: Programming Language  C Tutorial Introduction 主講人:虞台文.

Float

A float number is typically a 32-bit quantity, with at least six significant digits and magnitude generally between about 10-38 and 1038

float fahr, celsius;float lower, upper, step;

float fahr, celsius;float lower, upper, step;

FLT_MIN FLT_MAX

Page 68: Programming Language  C Tutorial Introduction 主講人:虞台文.

Fundamental Types

Type Size

char 1 byte

short 2 bytes

int 4 bytes

long 4 bytes

float 4 bytes

double 8 bytes

Machine Dependent

Page 69: Programming Language  C Tutorial Introduction 主講人:虞台文.

Type Size

char 1 byte

short 2 bytes

int 4 bytes

long 4 bytes

float 4 bytes

double 8 bytes

Type Size

char 1 byte

short 2 bytes

int 4 bytes

long 4 bytes

float 4 bytes

double 8 bytes

Fundamental Types in Microsoft C++

Page 70: Programming Language  C Tutorial Introduction 主講人:虞台文.

Assignment Statements

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

Assign a value computed from an expression to a variable.

Page 71: Programming Language  C Tutorial Introduction 主講人:虞台文.

While Loop

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

The body of a while loop is repeatedly executed as long as the condition expression is tested true.

The body of a while loop is repeatedly executed as long as the condition expression is tested true.

while( cond_expression )statement

Page 72: Programming Language  C Tutorial Introduction 主講人:虞台文.

Arithmetical Operations

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

59 ( 32)C F

Can it be “celsius = 5/9*(fahr-32);

”?Why?

Can it be “celsius = 5/9*(fahr-32);

”?Why?

Page 73: Programming Language  C Tutorial Introduction 主講人:虞台文.

printf General-Purpose Output Formatting Function

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

Page 74: Programming Language  C Tutorial Introduction 主講人:虞台文.

printf General-Purpose Output Formatting Function

printf("%d\t%d\n", fahr, celsius);

format string

decimal tab decimal newline

Page 75: Programming Language  C Tutorial Introduction 主講人:虞台文.

printf General-Purpose Output Formatting Function

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}}

Left alignedLeft aligned

Page 76: Programming Language  C Tutorial Introduction 主講人:虞台文.

printf General-Purpose Output Formatting Function

Synopsis

#include <stdio.h>int printf(const char *format, ...);

Description

printf writes to the standard output stream using putchar, under control of the string pointed to by format that specifies how subsequent arguments are converted for output.

If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated but are otherwise ignored.

printf returns number of characters transmitted, or a negative value if an output or encoding error occurred.

Synopsis

#include <stdio.h>int printf(const char *format, ...);

Description

printf writes to the standard output stream using putchar, under control of the string pointed to by format that specifies how subsequent arguments are converted for output.

If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated but are otherwise ignored.

printf returns number of characters transmitted, or a negative value if an output or encoding error occurred.

Page 77: Programming Language  C Tutorial Introduction 主講人:虞台文.

printf General-Purpose Output Formatting Function

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%3d %6d\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr-32) / 9;printf("%3d %6d\n", fahr, celsius);fahr = fahr + step;

}}

Right alignedRight aligned

Page 78: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example:Fahrenheit-Celsius Table (Float)

59 ( 32)C F

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

Page 79: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example:Fahrenheit-Celsius Table (Float)

59 ( 32)C F

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

Page 80: Programming Language  C Tutorial Introduction 主講人:虞台文.

More on printf

Page 81: Programming Language  C Tutorial Introduction 主講人:虞台文.

Statements

Simple Statementslower = 0;

upper = 300;

step = 20;

fahr = lower;

Null Statement; // a null statement

Compound Statements{

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}

4 simple statements

1 compound statement

Page 82: Programming Language  C Tutorial Introduction 主講人:虞台文.

While Statements

while( cond_expression )

statement

The statement can be:

a simple statement

a null statement

a compound statements

Page 83: Programming Language  C Tutorial Introduction 主講人:虞台文.

Program Indentation

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

Page 84: Programming Language  C Tutorial Introduction 主講人:虞台文.

Program Indentation

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

StatementStatementStatementStatementStatementStatement

Statement

Page 85: Programming Language  C Tutorial Introduction 主講人:虞台文.

Program Indentation

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

StatementStatementStatementStatementStatementStatement

StatementStatementStatement

Page 86: Programming Language  C Tutorial Introduction 主講人:虞台文.

Program Indentation

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

StatementStatementStatementStatementStatementStatement

Compound Statement

Page 87: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

The for Statement

Page 88: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example:Fahrenheit-Celsius Table

59 ( 32)C F

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

float fahr, celsius;float lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

There are many ways to do the same task.

Page 89: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example:Fahrenheit-Celsius Table

59 ( 32)C F

#include <stdio.h>

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = 0; fahr <= 300; fahr = fahr + 20)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-

32));

}

#include <stdio.h>

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = 0; fahr <= 300; fahr = fahr + 20)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-

32));

}

Page 90: Programming Language  C Tutorial Introduction 主講人:虞台文.

The for Statement

#include <stdio.h>

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = 0; fahr <= 300; fahr = fahr + 20)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-

32));

}

#include <stdio.h>

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = 0; fahr <= 300; fahr = fahr + 20)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-

32));

}

for ( init-exp ; cond-exp ; loop-exp )statement

for ( init-exp ; cond-exp ; loop-exp )statement

Page 91: Programming Language  C Tutorial Introduction 主講人:虞台文.

for vs. while

The choice between while and for is arbitrary, based on which seems clearer.

The for is usually appropriate for loops

in which the initialization and increment are single statements and logically related.

Page 92: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercises3. Modify the temperature conversion program to print

the table in reverse order, that is, from 300 degrees to 0.

Page 93: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercises4. Using only the techniques you learned, write a

program that calculates the squares and cubes of the number from 0 to 10 and used tabs to print the following table of values.

number square cube0 0 01 1 12 4 83 9 274 16 645 25 1256 36 2167 49 3438 64 5129 81 71910 100 1000

Page 94: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Symbolic Constants

Page 95: Programming Language  C Tutorial Introduction 主講人:虞台文.

#define Text Substitution

#define name replacement list

#define PI 3.1415926

Example:

Page 96: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example:Fahrenheit-Celsius Table

#include <stdio.h>

#define LOWER 0 /* lower limit of table */

#define UPPER 300 /* upper limit */

#define STEP 20 /* step size */

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = LOWER; fahr <= UPPER; fahr = fahr +

STEP)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-

32));

}

#include <stdio.h>

#define LOWER 0 /* lower limit of table */

#define UPPER 300 /* upper limit */

#define STEP 20 /* step size */

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = LOWER; fahr <= UPPER; fahr = fahr +

STEP)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-

32));

}

Page 97: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Character Input and Output

Page 98: Programming Language  C Tutorial Introduction 主講人:虞台文.

getchar() & putchar()

Synopsis#include <stdio.h>int getchar(void);

Descriptiongetchar reads a single character from the standard input stream.If the stream is at end-of-file or a read error occurs, getchar returns EOF.

Synopsis#include <stdio.h>int getchar(void);

Descriptiongetchar reads a single character from the standard input stream.If the stream is at end-of-file or a read error occurs, getchar returns EOF.

Synopsis#include <stdio.h>int putchar(int c);

Descriptionputchar writes the character c to the standard output stream. putchar returns the character written. If a write error occurs, putchar returns EOF.

Synopsis#include <stdio.h>int putchar(int c);

Descriptionputchar writes the character c to the standard output stream. putchar returns the character written. If a write error occurs, putchar returns EOF.

#define EOF (-1)

Page 99: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Typewriter (Version 1)

#include <stdio.h>/* copy input to output; 1st version */

main(){ int c; c = getchar(); while (c != EOF) {

putchar(c); c = getchar();

}}

#include <stdio.h>/* copy input to output; 1st version */

main(){ int c; c = getchar(); while (c != EOF) {

putchar(c); c = getchar();

}}

!= not equal

Page 100: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Typewriter (Version 2)

#include <stdio.h>/* copy input to output; 2st version */

main(){

int c;c = getchar();while ((c = getchar()) != EOF)

putchar(c);}

#include <stdio.h>/* copy input to output; 2st version */

main(){

int c;c = getchar();while ((c = getchar()) != EOF)

putchar(c);}

The return value of an assignment statement is value of the variable after the assignment.

simple stateme

nt

Page 101: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Typewriter (Version 2)

#include <stdio.h>/* copy input to output; 2st version */

main(){

int c;c = getchar();while ((c = getchar()) != EOF) putchar(c);

}

#include <stdio.h>/* copy input to output; 2st version */

main(){

int c;c = getchar();while ((c = getchar()) != EOF) putchar(c);

}

The return value of an assignment statement is value of the variable after the assignment.

Page 102: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Character Counting (Version 1)

#include <stdio.h>/* count characters in input; 1st version */

main(){

long nc;nc = 0;while (getchar() != EOF)

++nc;printf("%ld\n", nc);

}

#include <stdio.h>/* count characters in input; 1st version */

main(){

long nc;nc = 0;while (getchar() != EOF)

++nc;printf("%ld\n", nc);

}

++ increment

simple stateme

nt

Page 103: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Character Counting (Version 1)

#include <stdio.h>

/* count characters in input; 1st version */

main()

{

long nc;

nc = 0;

while (getchar() != EOF) ++nc;

printf("%ld\n", nc);

}

#include <stdio.h>

/* count characters in input; 1st version */

main()

{

long nc;

nc = 0;

while (getchar() != EOF) ++nc;

printf("%ld\n", nc);

}

++ increment

Page 104: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Character Counting (Version 2)

#include <stdio.h>

/* count characters in input; 2nd version */

main(){ double nc;

for (nc = 0; getchar() != EOF; ++nc) ; printf("%.0f\n", nc);}

#include <stdio.h>

/* count characters in input; 2nd version */

main(){ double nc;

for (nc = 0; getchar() != EOF; ++nc) ; printf("%.0f\n", nc);}

null stateme

nt

Page 105: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Character Counting (Version 2)

#include <stdio.h>

/* count characters in input; 2nd version */

main()

{

double nc;

for (nc = 0; getchar() != EOF; ++nc);

printf("%.0f\n", nc);

}

#include <stdio.h>

/* count characters in input; 2nd version */

main()

{

double nc;

for (nc = 0; getchar() != EOF; ++nc);

printf("%.0f\n", nc);

}

Page 106: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Line Counting

#include <stdio.h>

/* count lines in input */main(){ int c, nl;

nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl);}

#include <stdio.h>

/* count lines in input */main(){ int c, nl;

nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl);}

== equals to

if ( expression ) statement

if ( expression ) statement else statement

if ( expression ) statement

if ( expression ) statement else statement

simple stateme

nt

Page 107: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Line Counting

#include <stdio.h>

/* count lines in input */main(){ int c, nl;

nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl);}

#include <stdio.h>

/* count lines in input */main(){ int c, nl;

nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl);}

simple stateme

nt

Page 108: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Line Counting

#include <stdio.h>

/* count lines in input */main(){ int c, nl;

nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl);}

#include <stdio.h>

/* count lines in input */main(){ int c, nl;

nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl);}

simple stateme

nt

Can we move to here?

Page 109: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Word Counting

OUT IN

visible

non-visible

visible

>

non-visible

This is a test case.OIIIIOIIOIOOOIIIIO OIIIII

Page 110: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Word Counting

#include <stdio.h>#define IN 1 /* inside a word */#define OUT 0 /* outside a word *//* count lines, words, and characters in input */main(){ int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) {

++nc;if (c == '\n') ++nl;if (c == ' ' || c == '\n' || c = '\t') state = OUT;

else if (state == OUT) { state = IN; ++nw;}

} printf("%d %d %d\n", nl, nw, nc);}

#include <stdio.h>#define IN 1 /* inside a word */#define OUT 0 /* outside a word *//* count lines, words, and characters in input */main(){ int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) {

++nc;if (c == '\n') ++nl;if (c == ' ' || c == '\n' || c = '\t') state = OUT;

else if (state == OUT) { state = IN; ++nw;}

} printf("%d %d %d\n", nl, nw, nc);}

OUT INvisible

non-visible

visible

>non-visible

Page 111: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercises

7. Write a program to print a histogram of the frequencies of different characters in its input.

Page 112: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Arrays

Page 113: Programming Language  C Tutorial Introduction 主講人:虞台文.

Arrays

Array is a data structure that stores contiguous data elements of the same type.

Examples:int score[50];char address[50];double distance[50];

score

address

distance

200

byte

s50

byte

s400

byte

s

Page 114: Programming Language  C Tutorial Introduction 主講人:虞台文.

ArraysExamples:int score[50];char address[50];double distance[50];

score

address

distance

...

score[0]

score[1]

score[49]

Page 115: Programming Language  C Tutorial Introduction 主講人:虞台文.

ArraysExamples:int score[50];char address[50];double distance[50];

score

address

distance

address[0]address[1]address[2]

address[49]

...

Page 116: Programming Language  C Tutorial Introduction 主講人:虞台文.

ArraysExamples:int score[50];char address[50];double distance[50];

score

address

distance ...

distance[0]

distance[49]

Page 117: Programming Language  C Tutorial Introduction 主講人:虞台文.

#include <stdio.h>

/* count digits, white space, others */main(){

int c, i, nwhite, nother;int ndigit[10];

nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;

while ((c = getchar()) != EOF)if (c >= '0' && c <= '9') ++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;else ++nother;

printf("digits =");for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);

}

#include <stdio.h>

/* count digits, white space, others */main(){

int c, i, nwhite, nother;int ndigit[10];

nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;

while ((c = getchar()) != EOF)if (c >= '0' && c <= '9') ++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;else ++nother;

printf("digits =");for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);

}

Example: Digit Counting

&& logical and operator|| logical or operator

Page 118: Programming Language  C Tutorial Introduction 主講人:虞台文.

#include <stdio.h>

/* count digits, white space, others */main(){

int c, i, nwhite, nother;int ndigit[10];

nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;

while ((c = getchar()) != EOF)if (c >= '0' && c <= '9') ++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;else ++nother;

printf("digits =");for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);

}

#include <stdio.h>

/* count digits, white space, others */main(){

int c, i, nwhite, nother;int ndigit[10];

nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;

while ((c = getchar()) != EOF)if (c >= '0' && c <= '9') ++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;else ++nother;

printf("digits =");for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);

}

Example: Digit Counting

Page 119: Programming Language  C Tutorial Introduction 主講人:虞台文.

#include <stdio.h>

/* count digits, white space, others */main(){

int c, i, nwhite, nother;int ndigit[10];

nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;

while ((c = getchar()) != EOF)if (c >= '0' && c <= '9') ++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;else ++nother;

printf("digits =");for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);

}

#include <stdio.h>

/* count digits, white space, others */main(){

int c, i, nwhite, nother;int ndigit[10];

nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;

while ((c = getchar()) != EOF)if (c >= '0' && c <= '9') ++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;else ++nother;

printf("digits =");for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);

}

Example: Digit Counting

Page 120: Programming Language  C Tutorial Introduction 主講人:虞台文.

#include <stdio.h>

/* count digits, white space, others */main(){

int c, i, nwhite, nother;int ndigit[10];

nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;

while ((c = getchar()) != EOF)if (c >= '0' && c <= '9') ++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;else ++nother;

printf("digits =");for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);

}

#include <stdio.h>

/* count digits, white space, others */main(){

int c, i, nwhite, nother;int ndigit[10];

nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;

while ((c = getchar()) != EOF)if (c >= '0' && c <= '9') ++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;else ++nother;

printf("digits =");for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);

}

Example: Digit Counting

Page 121: Programming Language  C Tutorial Introduction 主講人:虞台文.

if Statement

if (condition1)statement1

else if (condition2) statement2

else if (condition3) statement3

...else

statementn

if (condition1)statement1

else if (condition2) statement2

else if (condition3) statement3

...else

statementn

The dangling else problem

Page 122: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercises

5. Write a program that prints its input one word per line.

6. Write a program that prints only a single white character between words.

Page 123: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercises

7. Write a program to print a histogram of the frequencies of different characters in its input.

Page 124: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Functions

Page 125: Programming Language  C Tutorial Introduction 主講人:虞台文.

Functions we have used

printf

getchar

putchar

Page 126: Programming Language  C Tutorial Introduction 主講人:虞台文.

Functions

A way to encapsulate some computation.

Similar to a subroutine or function in Fortran, or a procedure or function in Pascal.

A properly designed function allow us to– know what is done, but

– ignore how a job is done

Page 127: Programming Language  C Tutorial Introduction 主講人:虞台文.

Function Definition

return-type function-name(parameter declarations, if an

y)

{

declarations

statements

}

return-type function-name(parameter declarations, if an

y)

{

declarations

statements

}

Page 128: Programming Language  C Tutorial Introduction 主講人:虞台文.

Function Definition

return-type function-name(parameter declarations, if an

y)

{

declarations

statements

}

return-type function-name(parameter declarations, if an

y)

{

declarations

statements

}

If absent, return type is considered int.

If absent, return type is considered int.

Called formal

parameters

Called formal

parameters

Page 129: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Power#include <stdio.h>int power(int m, int n);/* test power function */main(){ int i;

for (i = 0; i < 10; ++i) printf("%d %d %d\n", i, power(2,i), power(-3,i)); return 0;}

/* power: raise base to n-th power; n >= 0 */int power(int base, int n){ int i, p;

p = 1; for (i = 1; i <= n; ++i) p = p * base; return p;}

#include <stdio.h>int power(int m, int n);/* test power function */main(){ int i;

for (i = 0; i < 10; ++i) printf("%d %d %d\n", i, power(2,i), power(-3,i)); return 0;}

/* power: raise base to n-th power; n >= 0 */int power(int base, int n){ int i, p;

p = 1; for (i = 1; i <= n; ++i) p = p * base; return p;}

forward reference

Page 130: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Power#include <stdio.h>int power(int m, int n);/* test power function */main(){ int i;

for (i = 0; i < 10; ++i) printf("%d %d %d\n", i, power(2,i), power(-3,i)); return 0;}

/* power: raise base to n-th power; n >= 0 */int power(int base, int n){ int i, p;

p = 1; for (i = 1; i <= n; ++i) p = p * base; return p;}

#include <stdio.h>int power(int m, int n);/* test power function */main(){ int i;

for (i = 0; i < 10; ++i) printf("%d %d %d\n", i, power(2,i), power(-3,i)); return 0;}

/* power: raise base to n-th power; n >= 0 */int power(int base, int n){ int i, p;

p = 1; for (i = 1; i <= n; ++i) p = p * base; return p;}

Page 131: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Power (Version 2)

/* power: raise base to n-th power; n >= 0 */int power(int base, int n){ int i, p;

p = 1; for (i = 1; i <= n; ++i) p = p * base; return p;}

/* power: raise base to n-th power; n >= 0 */int power(int base, int n){ int i, p;

p = 1; for (i = 1; i <= n; ++i) p = p * base; return p;}

/* power: raise base to n-th power; n >= 0; version 2 */

int power(int base, int n){ int i, p;

for (p = 1; n < 0; --n) p = p * base; return p;}

/* power: raise base to n-th power; n >= 0; version 2 */

int power(int base, int n){ int i, p;

for (p = 1; n < 0; --n) p = p * base; return p;}

Page 132: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Power (Version 2)

/* power: raise base to n-th power; n >= 0; version 2 */

int power(int base, int n){ int i, p;

for (p = 1; n < 0; --n) p = p * base; return p;}

/* power: raise base to n-th power; n >= 0; version 2 */

int power(int base, int n){ int i, p;

for (p = 1; n < 0; --n) p = p * base; return p;}

. . . . . . . . . .main(){ int n, val;

n = 5; val = power(2, n); printf("val=%d n=%d %d\n", val, n);}. . . . . . . . . .

. . . . . . . . . .main(){ int n, val;

n = 5; val = power(2, n); printf("val=%d n=%d %d\n", val, n);}. . . . . . . . . .

n=?n=?

Page 133: Programming Language  C Tutorial Introduction 主講人:虞台文.

Call by Value

In C, all function arguments are passed “by value.”

– the called function is given the values of its arguments in

temporary variables rather than the originals.

– Modifying the values of formal parameters doesn’t effect

the value of originals

How about if a function has arrays as its

parameters?

Page 134: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

Character Arrays

Page 135: Programming Language  C Tutorial Introduction 主講人:虞台文.

Strings in C Null-terminated string In C, a string is stored as an array

of characters containing the characters in the string and terminated with a '\0' to mark the end.

char str[]="hello\n";

Example: h (68)

e (65)

l (6C)

l (6C)

o (6F)

\n (0A)

\0 (00)

str

Page 136: Programming Language  C Tutorial Introduction 主講人:虞台文.

Strings in C

Page 137: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

while (there's another line)

if (it's longer than the previous longest)

(save it)

(save its length)

print longest line

while (there's another line)

if (it's longer than the previous longest)

(save it)

(save its length)

print longest line

Page 138: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

while (there's another line)

if (it's longer than the previous longest)

(save it)

(save its length)

print longest line

while (there's another line)

if (it's longer than the previous longest)

(save it)

(save its length)

print longest line

Implement a getline() function which returns the length of the line read.

Implement a getline() function which returns the length of the line read.

Implement a copy() function to save string.

Implement a copy() function to save string.

Page 139: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

/* getline: read a line into s, return length */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)s[i] = c;

if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i;}

/* getline: read a line into s, return length */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)s[i] = c;

if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i;}

&& logical and operator

Page 140: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

/* getline: read a line into s, return length */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)s[i] = c;

if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i;}

/* getline: read a line into s, return length */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)s[i] = c;

if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i;}

&& logical and operator

Simplification

s[i++] = c;

s[i++] = c;

Page 141: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

/* getline: read a line into s, return length *//* simplified version */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i;}

/* getline: read a line into s, return length *//* simplified version */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i;}

&& logical and operator

Page 142: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

/* copy: copy 'from' into 'to'; assume to is big enough */

void copy(char to[], char from[])

{

int i;

i = 0;

while ((to[i] = from[i]) != '\0') ++i;

}

/* copy: copy 'from' into 'to'; assume to is big enough */

void copy(char to[], char from[])

{

int i;

i = 0;

while ((to[i] = from[i]) != '\0') ++i;

}

Page 143: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);void copy(char to[], char from[]);

/* print the longest input line */main(){

int len; /* current line length */int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

max = 0;while ((len = getline(line, MAXLINE)) > 0)

if (len > max) {max = len;copy(longest, line);

}if (max > 0) /* there was a line */

printf("%s", longest);return 0;

}. . . . . . . . . . . .

#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);void copy(char to[], char from[]);

/* print the longest input line */main(){

int len; /* current line length */int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

max = 0;while ((len = getline(line, MAXLINE)) > 0)

if (len > max) {max = len;copy(longest, line);

}if (max > 0) /* there was a line */

printf("%s", longest);return 0;

}. . . . . . . . . . . .

Page 144: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

Page 145: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

Page 146: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

Page 147: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercises

8. Write a function getlinestr() by modifying getline() described above so that the string read doesn’t include '\n'.

9. Write a function reverse(char s[]) that reverses the character string s.

10. Write a function int strlen(char s[]) that returns the number of characters in string s.

11. Write a program to demonstrate that the about three functions you wrote are correct.

Page 148: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercises

12. Write a function int atoi(char s[]) that converts a signed decimal numeric string, assumed the syntax is correct, to an integer, and write a program to verify the correctness of your program.

13. Write a program that reads two integers from the standard input stream and output their minimum and maximum to the standard output stream. You can’t use library function scanf() to read data.

14. (Take home) Write a function void itoa(int n, char s[]) that converts a signed integer to a signed decimal numeric string, and write a program to verify the correctness of your program.

Page 149: Programming Language  C Tutorial Introduction 主講人:虞台文.

Programming Language CTutorial Introduction

External Variables and Scope

Page 150: Programming Language  C Tutorial Introduction 主講人:虞台文.

Local (Private) Variables

#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);void copy(char to[], char from[]);

/* print the longest input line */main(){ int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */

max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len;

copy(longest, line); } if (max > 0) /* there was a line */

printf("%s", longest); return 0;}

#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);void copy(char to[], char from[]);

/* print the longest input line */main(){ int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */

max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len;

copy(longest, line); } if (max > 0) /* there was a line */

printf("%s", longest); return 0;}

/* getline: read a line into s, return length *//* simplified version */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i;}

/* copy: copy 'from' into 'to'; assume to is big enough */

void copy(char to[], char from[])

{

int i;

i = 0;

while ((to[i] = from[i]) != '\0') ++i;

}

/* getline: read a line into s, return length *//* simplified version */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i;}

/* copy: copy 'from' into 'to'; assume to is big enough */

void copy(char to[], char from[])

{

int i;

i = 0;

while ((to[i] = from[i]) != '\0') ++i;

}

Page 151: Programming Language  C Tutorial Introduction 主講人:虞台文.

Local (Private) Variables

#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);void copy(char to[], char from[]);

/* print the longest input line */main(){ int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */

max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len;

copy(longest, line); } if (max > 0) /* there was a line */

printf("%s", longest); return 0;}

#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);void copy(char to[], char from[]);

/* print the longest input line */main(){ int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */

max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len;

copy(longest, line); } if (max > 0) /* there was a line */

printf("%s", longest); return 0;}

/* getline: read a line into s, return length *//* simplified version */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i;}

/* copy: copy 'from' into 'to'; assume to is big enough */

void copy(char to[], char from[])

{

int i;

i = 0;

while ((to[i] = from[i]) != '\0') ++i;

}

/* getline: read a line into s, return length *//* simplified version */int getline(char s[],int lim){ int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i;}

/* copy: copy 'from' into 'to'; assume to is big enough */

void copy(char to[], char from[])

{

int i;

i = 0;

while ((to[i] = from[i]) != '\0') ++i;

}

Each local variable comes into existence only when the function is called, and disappears when the function is exited.

Hence, also known as automatic variables.

Each local variable comes into existence only when the function is called, and disappears when the function is exited.

Hence, also known as automatic variables.

Page 152: Programming Language  C Tutorial Introduction 主講人:虞台文.

Global (Extern) Variables

Global variables that are defined external to all functions can be accessed by name by any function.

Furthermore, they remain in existence permanently.

Page 153: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

defined external to all functions

Page 154: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

defined external to all functions

/* print longest input line; specialized version */ main(){ int len; extern int max; extern char longest[];

max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0;}

/* print longest input line; specialized version */ main(){ int len; extern int max; extern char longest[];

max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0;}

Page 155: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

defined external to all functions

/* getline: specialized version */int getline(void){ int c, i; extern char line[];

for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i;}

/* getline: specialized version */int getline(void){ int c, i; extern char line[];

for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i;}

Page 156: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

defined external to all functions

/* copy: specialized version */void copy(void){ int i; extern char line[], longest[];

i = 0; while ((longest[i] = line[i]) != '\0') ++i;}

/* copy: specialized version */void copy(void){ int i; extern char line[], longest[];

i = 0; while ((longest[i] = line[i]) != '\0') ++i;}

Page 157: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

defined external to all functions

/* print longest input line; specialized version */ main(){ int len; extern int max; extern char longest[];

max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0;}

/* print longest input line; specialized version */ main(){ int len; extern int max; extern char longest[];

max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0;}

/* getline: specialized version */int getline(void){ int c, i; extern char line[];

for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i;}

/* getline: specialized version */int getline(void){ int c, i; extern char line[];

for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i;}

/* copy: specialized version */void copy(void){ int i; extern char line[], longest[];

i = 0; while ((longest[i] = line[i]) != '\0') ++i;}

/* copy: specialized version */void copy(void){ int i; extern char line[], longest[];

i = 0; while ((longest[i] = line[i]) != '\0') ++i;}

The declarations for referencing external variables are redundant if they are defined in the header part of the same file .

The declarations for referencing external variables are redundant if they are defined in the header part of the same file .

Page 158: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line (Multiple Source Files) I

/* main.c */#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* main.c */#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* functions.c *//* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

/* functions.c *//* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

Page 159: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line (Multiple Source Files) I

/* main.c */#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* main.c */#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* functions.c *//* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

/* functions.c *//* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

/* print longest input line; specialized version */ main(){ int len; extern int max; extern char longest[];

max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0;}

/* print longest input line; specialized version */ main(){ int len; extern int max; extern char longest[];

max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0;}

/* getline: specialized version */int getline(void){ int c, i; extern char line[];

for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i;}

/* getline: specialized version */int getline(void){ int c, i; extern char line[];

for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i;}

/* copy: specialized version */void copy(void){ int i; extern char line[], longest[];

i = 0; while ((longest[i] = line[i]) != '\0') ++i;}

/* copy: specialized version */void copy(void){ int i; extern char line[], longest[];

i = 0; while ((longest[i] = line[i]) != '\0') ++i;}

Must be kept.Must be kept.

Must be kept.Must be kept.

Page 160: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line (Multiple Source Files) II

/* main.c */#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* main.c */#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* functions.c */#include <stdio.h>#include "externdef.h"/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

/* functions.c */#include <stdio.h>#include "externdef.h"/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

/* externdef.h */#define MAXLINE 1000 /* maximum input line size */

extern int max; /* maximum length seen so far */extern char line[]; /* current input line */extern char longest[]; /* longest line saved here */

/* externdef.h */#define MAXLINE 1000 /* maximum input line size */

extern int max; /* maximum length seen so far */extern char line[]; /* current input line */extern char longest[]; /* longest line saved here */

Page 161: Programming Language  C Tutorial Introduction 主講人:虞台文.

Example: Print the Longest Line (Multiple Source Files) II

/* main.c */#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* main.c */#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

int getline(void);void copy(void);

/* print longest input line; specialized version */main(){ . . . . . . . . . . . . }

/* functions.c */#include <stdio.h>#include "externdef.h"/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

/* functions.c */#include <stdio.h>#include "externdef.h"/* getline: specialized version */int getline(void){ . . . . . . . . . . . . }

/* copy: specialized version */void copy(void){ . . . . . . . . . . . . }

/* externdef.h */#define MAXLINE 1000 /* maximum input line size */

extern int max; /* maximum length seen so far */extern char line[]; /* current input line */extern char longest[]; /* longest line saved here */

/* externdef.h */#define MAXLINE 1000 /* maximum input line size */

extern int max; /* maximum length seen so far */extern char line[]; /* current input line */extern char longest[]; /* longest line saved here */

/* getline: specialized version */int getline(void){ int c, i; extern char line[];

for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i;}

/* getline: specialized version */int getline(void){ int c, i; extern char line[];

for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i;}

/* copy: specialized version */void copy(void){ int i; extern char line[], longest[];

i = 0; while ((longest[i] = line[i]) != '\0') ++i;}

/* copy: specialized version */void copy(void){ int i; extern char line[], longest[];

i = 0; while ((longest[i] = line[i]) != '\0') ++i;}

Page 162: Programming Language  C Tutorial Introduction 主講人:虞台文.

Exercise

15. (Take home) Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments don't nest.