【文件属性】:
文件名称:C Programming
文件大小:371KB
文件格式:PDF
更新时间:2014-07-10 17:28:14
C Programming
C Programming.pdf
Contents
Computers 1
An Introduction to Computers........................................................................................1
Hardware and Software ....................................................................................1
Data and Information .......................................................................................2
Data Processing................................................................................................2
Programming Languages 4
What is Programming? ..................................................................................................4
From Problem to Program ................................................................................5
Programming Languages ...............................................................................................8
C 8
A look at C ....................................................................................................................8
Making C Run..................................................................................................9
Creating C Programs........................................................................................9
What Comprises a C Program?.......................................................................10
The Advantages of C ......................................................................................11
The Disadvantages of C..................................................................................11
A First C Program 11
The Program Example .................................................................................................11
#include ........................................................................................................12
.......................................................................................................12
void ...............................................................................................................12
main ..............................................................................................................13
( void )...........................................................................................................13
{ .....................................................................................................................13
float ...............................................................................................................13
height, width, area, wood_length................................................................13
; .....................................................................................................................14
scanf .............................................................................................................14
( .....................................................................................................................14
"%f", ..............................................................................................................14
&height..........................................................................................................15
) ; ...................................................................................................................15
scanf ( "%f", &width ) ;.................................................................................15
area = 2 * height * width ;............................................................................15
wood_length = 2 * ( height + width ) * 3.25 ;.................................................16
printf..............................................................................................................16
( "The area of glass needed is : %f metres.\n",.........................................16
area ) ; ..........................................................................................................17
printf ( "The length of wood needed is : %f feet.\n", wood_length ) ; ......17
} .....................................................................................................................17
Punctuation ....................................................................................................17
Variables 18
ii
Variables and Data .........................................................................................18
Types of Variables .......................................................................................................18
Declaration ..................................................................................................................18
int variables....................................................................................................19
float variables ................................................................................................19
char variables ................................................................................................19
Missing Types ................................................................................................19
Variable Declaration ....................................................................................................19
Giving Values to Variables ..........................................................................................20
Expressions ....................................................................................................21
Types of Data in Expressions..........................................................................22
Getting Values into the Program ..................................................................................23
Writing a Program 24
Comments......................................................................................................24
Program Flow ..............................................................................................................24
Conditional Execution - if...............................................................................25
Conditions and Relational Operators ..............................................................26
Combining Logical Operators.........................................................................27
Lumping Code Together.................................................................................27
Magic Numbers and #define..........................................................................28
Loops .............................................................................................................29
Breaking Out of Loops ...................................................................................32
Going Back to the Top of a Loop ....................................................................32
More Complicated Decisions ..........................................................................33
Complete Glazing Program ............................................................................33
Operator Shorthand........................................................................................34
Statements and Values....................................................................................35
Neater Printing...............................................................................................36
Functions 37
Functions So Far ..........................................................................................................37
Function Heading ...........................................................................................37
Function Body ................................................................................................38
return .............................................................................................................38
Calling a Function..........................................................................................38
Scope .............................................................................................................39
Variables Local to Blocks ...............................................................................40
Full Functions Example..................................................................................40
Pointers.......................................................................................................................42
NULL Pointers ...............................................................................................44
Pointers and Functions ...................................................................................44
Static Variables............................................................................................................45
Arrays 46
Why We Need Arrays ..................................................................................................46
Sorting ...........................................................................................................47
Array Types and Sizes..................................................................................................49
More Than One Dimension ............................................................................50
Switching 53
Making Multiple Decisions ..........................................................................................53
Strings 55
iii
How long is a piece of string?.......................................................................................55
Putting Values into Strings ..........................................................................................56
Using Strings ...............................................................................................................57
The String Library .......................................................................................................58
strcpy .............................................................................................................59
strcmp ............................................................................................................59
strlen..............................................................................................................59
Reading and Printing Strings .......................................................................................59
Bomb Proof Input.........................................................................................................60
Structures 61
What is a Structure?.....................................................................................................61
How Structures Work...................................................................................................63
Pointers to structures....................................................................................................63
Defining your own Types .............................................................................................64
Files 65
When do we use Files? .................................................................................................65
Streams and Files ...........................................................................................65
fopen and fclose ...........................................................................................................66
Mode String ...................................................................................................68
File Functions ..............................................................................................................70
fread and fwrite ..............................................................................................70
The End of the File and Errors.....................................................................................71
Memory 72
Fetching Memory.........................................................................................................72
malloc ............................................................................................................72
free.................................................................................................................73
The heap......................................................................................................................73
C and Large Programs 74
Building Large Programs in C .....................................................................................74
The Compile and Link Process .......................................................................74
Referring to External Items ............................................................................75
The Make Program.........................................................................................76
Projects ..........................................................................................................76
The C Pre-Processor.....................................................................................................77
The #include Directive ...................................................................................77
Conditional Compilation ................................................................................77
A Sample Project .........................................................................................................78
The Problem...................................................................................................78
The Data Structure .........................................................................................78
Program Files.................................................................................................79
The Most Important Bit!.................................................................................84
Glossary of Terms 93