You are on page 1of 34

C

1 ...................................................................................................................... 3
.............................................................................................................................. 3
.............................................................................................................................. 3
.................................................................................................................. 3
.................................................................................................................. 4
...................................................................................................................... 4

2 .......................................................................................................................... 5
.......................................................................................................................................... 5
.......................................................................................................................... 5
...................................................................................................................................... 6
.............................................................................................................................. 6

3 .................................................................................................................. 7
...................................................................................................................................... 7
.................................................................................................................................. 7
.............................................................................................................................. 7
.................................................................................................................................. 7

4 .......................................................................................................... 8
.................................................................................................................................. 8
(Escape Sequence) ............................................................................................................ 8
.............................................................................................................................................. 8
.............................................................................................................. 9
.................................................................................................................................. 9

5 ................................................................................................................ 10
.................................................................................................................................... 10
.........................................................................................................................11

6 ........................................................................................................................ 12
if ............................................................................................................................................. 12
switch ..................................................................................................................................... 12
.................................................................................................................................... 13

7 ........................................................................................................................ 14
for / while / dowhile........................................................................................................... 14
........................................................................................................................................ 14
............................................................................................................................................ 14
........................................................................................................................................ 15
.................................................................................................................................... 15

8..................................................................................................................................... 16
............................................................................................................................ 16
(Function) ........................................................................................................ 16
(Prototype) .................................................................................................................... 16
.................................................................................................................................... 16

-1-

Printed by Apric 2004/5/1

(Recursive Function)..................................................................................................... 16
(Storage Classes)....................................................................................................... 17
(Scope) .......................................................................................................................... 17
............................................................................................................ 18

9 ........................................................................................................................ 19
#define ....................................................................................................................... 19
#include ...................................................................................................................... 19

10................................................................................................................................... 20
........................................................................................................................................ 20
........................................................................................................................................ 20
.................................................................................................................................... 21

11................................................................................................................................... 22
........................................................................................................................................ 22
/ ................................................................................................................... 22
........................................................................................................................................ 23

12................................................................................................................................... 24
........................................................................................................................................ 24
.................................................................................................................................... 24
.................................................................................................................................... 25
........................................................................................................................ 25
.................................................................................................................................... 26
........................................................................................................................ 26
........................................................................................................................................ 27
V.S. ...................................................................................................... 27
...................................................................................................... 27
............................................................................................................................ 28

13 ...................................................................................................... 29
(Structure) ............................................................................................................................. 29
........................................................................................................................................ 29
........................................................................................................................................ 30
........................................................................................................................................ 30
.................................................................................................................................... 30
(typedef)........................................................................................................................ 31

14................................................................................................................................... 32
........................................................................................................ 32
................................................................................................................................ 32
........................................................................................................ 33
........................................................................................................................ 34

1. C / / 90
2. C / / 90

-2-

Printed by Apric 2004/5/1

1. Defining the program


2. Planning the solution
3. Coding the program
4. Testing the program
5. Documenting the program

1. (Machine Language)

(0 1)
2. (Assembly Language)
(assembler)

3. (High-level Language)

4. (Very High-level Language)


SQL (Structual Quary Language)
5. (Natural Language)

Interpreter

Compiler

z z

(.obj)
z

-3-

BASIC
z
HTML

C/C++
COBOL
PASCAL

Printed by Apric 2004/5/1

1. (Procedure-Oriented Programming)
2. (Object-Oriented Programming)
C++JavaVisual BASIC

FORmula TRANslator (1954)

COBOL

COmmon Business-Oriented Language (1959)

BASIC

Beginner's All-purpose Symbolic Instruction Code

Pascal

named after French inventor Blaise Pascal (1971)

Ada

named after Ada, the Countess of Lovelace (1980)

evolved from the language B, from Bell Labs (1972)

FORTRAN

-4-

Printed by Apric 2004/5/1

C (Bell Laboratory) Dennis Ritchie 1972


B DEC PDP-11 C
(ANSI) 1989
1999

C
1. (editor) C (source code)
2. (preprocessor) #
(preprocessor directives)#include <stdio.h>
stdio.h
3. (compiler) (object code)
4. (linker) (.obj) (.lib)
(.exe)
5. (loader) (.exe) (.dll)

6. CPU

edit

.c

preprocess

.h

compile

.obj

.obj

-5-

link

.lib

.exe

load

execute

.dll

Printed by Apric 2004/5/1

(Sequence Structure)

(Selection Structure)
z
z

(Iteration Structure)
z
z
z

ifelse
switchcase

for
while
dowhile

true

1
false

true

false

2
2

2
3

-6-

Printed by Apric 2004/5/1

(variable)

int i=3;
float f=15.7;
char ch='y';

(constant)
const int max=65536;

char

0~255

int

-32768~32767

int (qualifier)
unsignedshortlong

float

1.2e-38~3.4e38

double

ASCII

2.2e-308~1.8e308

1.
2.
3.
4.
5.
6.
7.

1.
2.
3.
4.

( x = 100; )
( y = i * 5 + 7 / 23; )
( i = (int) ( x + 0.9 ); )
( x = sum (a, b); )

-7-

Printed by Apric 2004/5/1

printf("", var1, var2, );

(Escape Sequence)
\n

\"

\x

ASCII (16 )

\f

\'

\d

ASCII (8 )

\t

\/

\b

\\

printf("\tThis line begins with tab.\n");

This line begins with tab.

printf("It\'s a \"C Tutorial\".\n");

It's a "C Tutorial".

printf("This is backslash: \\.\n");

This is backslash: \.

printf("\\101 is \101.\n");

\101 is A.

printf("\\x41 is \x41.\n");

\x41 is A.

-
+
%c
%s
%d
%f ()
%l du
%u
%e ( e )

-8-

Printed by Apric 2004/5/1

12345

%10d

12345

%+d

12345

%-10d

12345

% d

12345

%010d

123.456

%7.2f

123.456

%010.3f

123.456

%+10.4f

scanf("", &var1, &var2, );

1. &
2. &

%d
%f
%c
%s

int
float, double
char

int num1, num2;

printf("Enter 2 numbers: ");

Enter 2 numbers:

scanf("%d,%d",&num1,&num2);

103,227

-9-

Printed by Apric 2004/5/1

1.

int a=9,b=4

a+b

13

+=

a=a+b

a+=b

a-b

-+

a=a-b

a-=b

a*b

36

*=

a=a*b

a*=b

a/b

/=

a=a/b

a/=b

a%b

%=

a=a%b

a%=b

2.

3.

>

2>3

false

>=

2>=3

false

<

2<3

true

<=

2<=3

true

==

2==3

false

!=

2!=3

true

a&&b

a||b

&&

AND,

||

OR,

NOT,

- 10 -

Printed by Apric 2004/5/1

1. /

int i=3;
int a;

++

i++

--

i--

a = i++

a = ++i

a = i--

a = --I

i++, i 1
z ++i i 1,
z

2.
(?:)

? 1 : 2

if()
1;
else
2;

a = (x > 100) ? b : c;

if (x > 100)
a = b;
else
a = c;

abs = (a > 0) ? a : -a;

if (a > 0)
abs = a;
else
abs = -a;

- 11 -

Printed by Apric 2004/5/1

6
if
if
if ()
;

if

ifelse
if ()
1;
else
2;

if ( 1)
{
1;
if ( 2)

if ()
{
1;

n;
}

if ()
{
1;
}
else
{
2;
}

{
2;
}

ifelse ifelse
if ()
{
1;
}
else if ()
{
2;
}

else
{
n;
}

switch
switch ()
{
case 1:
1;
break;
case 2:
2;
break;

case n:
n;
break;
default:
;
}

1.
2. break switch
3. default

- 12 -

Printed by Apric 2004/5/1

1. abc
2. (a+b, a-b, a*b, a/b, a%b)
3. switch ()
90~100:
80~89:
70~79:
60~69:
0~59:

A
B
C
D
E

#include <stdlib.h>
#include <time.h>
srand((unsigned)time(NULL));

/* srand() */

int num = a + rand() % b;

/* rand() */

a
b

(1) 0~9 rand() % 10


(2) 1~6 1 + rand() % 6
(3) 18~32 18 + rand() % 15
(4) 10000 + rand() % 90000

1. 7 10

2.

- 13 -

Printed by Apric 2004/5/1

7
for / while / dowhile
for
for (; ; )
{
1;
2;

n;
}

while
;
while ()
{
1;
2;

n;
;
}

dowhile

do
{
1;
2;

n;
;
} while ();

for (i=1,sum=0;i<=9;i+=2)
{
sum += i;
printf("i=%d\n", i);
printf("sum=%d\n",sum);
printf("\n");
}

i=1;
sum=0;
while(i<=9)
{
sum += i;
printf("i=%d\n", i);
printf("sum=%d\n",sum);
printf("\n");
i += 2;
}

i=1;
sum=0;
do
{
sum += i;
printf("i=%d\n", i);
printf("sum=%d\n",sum);
printf("\n");
i += 2;
} while (i<=9);

while (1)
{
;
}

for (; ; )
{}

for (; ; ) ;

- 14 -

Printed by Apric 2004/5/1

for
for ( 1; 1; 1)
{
for ( 2; 2; 2)
{

while
1;
while ( 1)
{
2;
while ( 2)
{

2;
}

1;
}

break()
for (; ; )
{
1;
2;

break;

n;
}

continue()
for (; ; )
{
1;
2;

continue;

n;
}

while / do while

1. n
(1) 1 + 2 + + n
(2) 1 * 2 * * n
(3) 1! + 2! + + n!
2. 2 n
(1) n
(2) n

- 15 -

Printed by Apric 2004/5/1

1.
2. (, abstraction)
3.
(Function)
()
{
;
;
return ;
}

int sum(int a, int b)


{
int num;
num = a + b;
return num;
}

(Prototype)
main()

();

int sum(int, int);


int sum(int a, int b);

(Header File, .h)


.h #include

1. (call by value)
2. (call by reference)
(Recursive Function)
1.
2. (stack)
3.

- 16 -

Printed by Apric 2004/5/1

(Storage Classes)

auto

z
auto float x,y;
z

auto

register

z register
register int counter = 1;
z

register

static
( 0 NULL)
z static
static int count = 1;
z

static

extern

(Scope)

1. (Global Variable)
2.
3.

1. (Local Variable)
{ }
2.

3.

1. goto
2. switch case

- 17 -

Printed by Apric 2004/5/1

1. (Fibonacci)
n 1 1 2 3 5 8 13 21 34
f(n) n f(n) = f(n-1) + f(n-2)
f(n)
int f(int n)
{
if (n==1 || n==2)
return 1;
else
return f(n-1)+f(n-2);
}

2. (Hanoi)
n ()
n A B
(1)
(2)

void hanoi(int n, int src, int dst, int tmp)


{
if (n==1)
printf(%d %d\n, src, dst);
else
{
hanoi(n-1, src, tmp, dst);

printf(%d ( %d\n, src, dst);


hanoi(n-1, tmp, dst, src);
}

[]
Input:(n,A,B)=(3,1,3)
Output: 13
12
32
13
21
23
13
:7

- 18 -

Printed by Apric 2004/5/1

9
#define
1.
2.
3.
4. (Macro)

#define MAX 32767


#define WORD "This is a test!"
#define AREA(w, h) ((w)*(h))
#define POWER(i) (i)*(i)*(i)
int main()
{

printf("%d \n", MAX);


printf(WORD);
printf("area = %d \n", AREA(5, 3));
printf("%d * %d * %d = %d \n", i+1, i+1, i+1, POWER(i+1));

#include
(.h)

< >

C:\Dev-C++\Include\

#include <stdio.h>

" "

#include "D:\myprog\area.h"

- 19 -

Printed by Apric 2004/5/1

10

[];

int score[10] = {80,90,95,75,100,80,85,95,90,60};


10

score

80

90

95

75

100

80

85

95

90

60

score[0]

........................................

score[9]

int (4 bytes)
sizeof(score) 4 * 10 = 40 bytes

int data[5] = {1};

/* 1 */

int num[] = {60,75,48,92};

/* */

int student[10] = {1,2,3,4,5};

/* 0 */

[][];

int sale[2][4] = {{10,20,30,40}, {50,60,70,80}};

4
0

10

20

30

40

50

60

70

80

sale[0][0] 10
sale[0][1] 20

sale[1][0] 50
sale[1][1] 60

2*4=8
sizeof(sale) 4*(2*4)=32bytes

- 20 -

Printed by Apric 2004/5/1

int temp[][4] = { {1,2,3,4},


{5,6,7,8},
{9,10,11,12} };

/* */

(Pointer)

#include <stdio.h>
#define SIZE 5

void print_matrix(int A[]);

/* */

Memory

int main(void)

In main()

{
int data[SIZE] = {23,25,28,29,27};

data

printf_matrix(data);

23

25

28

void print_matrix(int A[])

29

27

return 0;
}

0253FDB8

int i;
for (i=0; i<SIZE; i++)
printf("%d ", A[i]);

In print_matrix()

printf("\n");
return;

A 0253FDB8

- 21 -

Printed by Apric 2004/5/1

11

C
\0

char [];

char name[15] = "David Chen";

name

10

\0

11

12

13

14

char a[] = "My friend"; sizeof(a) 9 + \0 = 10 bytes

char b = c;

sizeof(b) 1 = 1 byte

char str[] = "c";

sizeof(str) 1 + \0 = 2 bytes

/
(1)

(2)

scanf("%s", );

gets();

printf("%s", );

puts();

- 22 -

Printed by Apric 2004/5/1

scanf() Enter 1. gets() Enter


\0
\0
2. puts()\0

#include <stdio.h>

#include <stdio.h>

int main(void)

int main(void)

char name[15];

char name[15];

printf("Whats your name?\n");

puts("Whats your name?");

scanf("%s", name);

gets(name);

printf("Hi! %s How are you?\n",

puts("Hi! ");
puts(name);

name);

puts(" How are you?");

return 0;

return 0;

}
}

char [][];

char name[3][10] = {"David", "Jane Wang", "Tom Lee"};

name[0] 0253FDB8

\0

name[1] 0253FDC2

name[2] 0253FDCC

\0

\0

name[0] name[1] 10 bytes


name[1] name[2] 10 bytes
string.h D-10

- 23 -

Printed by Apric 2004/5/1

12

(Pointer) Dev C++


4 (bytes)

0253FDD0

ex:15
0253FDD0

*;

int *ptri;

/* */

char *ptrch;

/* */

sizeof(ptri) 4 bytes
sizeof(ptrch) 4 bytes

1. &
2. *

int a = 10, b;
int *p;
p = &a;
b = *p;
*p = 20;

a = 20, b = 10

- 24 -

Printed by Apric 2004/5/1

1.
2. /
3.

int a=10, b=20;


int *p1, *p2;
char ch='a', *pch;
/* */
p1 = &a;

/* a p1 */

p2 = &b;

/* b p2 */

pch = &ch;

/* ch pch */

/* */
p1++;

/* p1 4 bytes (int ) */

pch--;

/* pch 1 byte (char ) */

/* */
/* p1 p2 ( int ) */

sub = p1 p2;

int X;
int A[5] = {10,20,30,40,50};
int *p = A + 2;

X = *(p++);

X = *p++;

X = *(++p);

X = *++p;

X = (*p)++;
X = ++(*p);
X = *(p--);

X = *p--;

X = *(--p);

X = *--p;

X = (*p)--;
X = --(*p);

X
p
p
X
X
*p
*p
X
X
p
p
X
X
*p
*p
X

=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=

*p;
p +
p +
*p;
*p;
*p +
*p +
*p;
*p;
p p *p;
*p;
*p *p *p;

1;
1;

1;
1;

1;
1;

1;
1;

- 25 -

X
*p
30

40

40

40

30

31

31

31

30

20

20

20

30

29

29

29

Printed by Apric 2004/5/1

return

return

void swap(int *, int *);

/* */

int main(void)
{
int a=3, b=5;
/* a b */

swap(&a, &b);
return 0;
}
void swap(int *x, int *y)

/* xy */

{
int temp = *x;
*x = *y;
*y = temp;
}

int a[3] = {5,7,9};

*(a+0)

a[0]

0253FDC8

&a[0]

a+0

*(a+1)

a[1]

0253FDCC

&a[1]

a+1

*(a+2)

a[2]

0253FDD0

&a[2]

a+2

a a a++

- 26 -

Printed by Apric 2004/5/1

*[];

int i=10,j=28,k=34;
int* a[3];

a[0]
a[1]
a[2]

a[0] = &j;

i
j
k

10
28
34

a[1] = &k;
a[2] = &i;

V.S.
(1) char name[3][10] = {"David", "Jane Wang", "Tom Lee"};
name[0] D

name[1] J

name[2] T

d \0

g \0

e \0

(2) char *name[3] = {David, Jane Wang, Tom Lee};


name[0] D

name[1] J

name[2] T

d \0

g \0

e \0

**;

0253FDA4

0253FDD0

ex: 15

0253FDA4

- 27 -

0253FDD0

Printed by Apric 2004/5/1

int n=5;
int* p;
p = (int*) malloc( n * sizeof(int) );
n

0x02E0
0x02E4
0x02E8
0x02EC
0x02F0

p 0x02E0

n int

int row=3,col=5,i;
int** array;
array = (int**) malloc(row * sizeof(int*));
for (i=0; i<row; i++)
array[i] = (int*) malloc(col * sizeof(int));

row
3
col
5
i
array 0x02A0

0x1A30
0x1A34
0x1A38
0x1A3C
0x1A40

col int

0x2D70
0x2D74
0x2D78
0x2D7C
0x2D80

col int

0xBDA0
0xBDA4
0xBDA8
0xBDAC
0xBDB0

col int

array[0] 0x1A30 0x02A0


array[1] 0x2D70 0x02A4
array[2] 0xBDA0 0x02A8

row int*
( int )

- 28 -

Printed by Apric 2004/5/1

13
(Structure)

struct
{
1;
2;

};

struct mydata
{
char name[15];
int score;
};

/* */
/* */
/* */

struct mydata
/* */
{
char name[15];
/* */
int score;
/* */
} teacher = {"Apric",90};
/* */

/* */
struct mydata teacher = {"Apric",90};

#include <stdio.h>
/* */
int main(void)
{
sturct mydata student;

/* */

printf("Name: ");
scanf("%s", student.name);
printf("Score: ");
scanf("%d", &student.score);
printf("%s got %d points! \n", student.name, student.score);
return 0;
}

struct date
{
int month;
int day;
};

/* */

- 29 -

Printed by Apric 2004/5/1

struct newdata
{
char name[15];
struct date birthday;
int score;
};

/* */

/* */

int main(void)
{
struct newdata student = {"Apric", {7, 10}, 90};
printf("%s's birthday is %d/%d \n",
student.name, student.birthday.month, student.birthday.day);
printf("He/She got %d points! \n", student.score);
return 0;
}

struct mydata student[10];


for(i=0; i<10; i++)
printf("%s got %d points! \n", student[i].name, student[i].score);

struct mydata student[10];


struct mydata *ptr = student;

/* */
/* student */

for (i=0;i<10;i++)
{
printf("Name, Score:");
scanf("%s, %d", (student+i)name, &(student+i)score);
printf("%s got %d points!\n", ptrname, ptrscore);
ptr++;
}

void get_data(struct mydata *p);


void print_data(struct mydata a);

/* */
/* */

int main(void)
{
struct mydata student;

- 30 -

Printed by Apric 2004/5/1

get_data(&student);
print_data(student);
return 0;
}
void get_data(struct mydata *p)
{
printf("Name:");
scanf("%s", pname);
printf("Score:");
scanf("%d", &pscore);
}
void print_data(struct mydata a)
{
printf("%s got %d !\n", a.name, a.score);
return;
}

(typedef)

typedef ;
typedef

#define

typedef int clock;

#define CLOCK int

clock hour, second;

CLOCK hour, second;

typedef

typedef struct
{
int minite;
float second;
} time;
time record = {3, 27.25};

- 31 -

Printed by Apric 2004/5/1

14

text file

binary file

ASCII

1
()
132956 6

132956 Dev C++


()

r+

w+

a+

rb

wb

ab

- 32 -

Printed by Apric 2004/5/1

FILE *file;
char ch;
char buffer[128];

fopen

file = fopen(C:\abc.txt, r);

fclose

fclose(file);

getc

ch = getc(file);

putc

putc(ch, file);

fgets

fget(buffer, 128, file);

fputs

fputs(buffer, file);

fprintf

fprintf(file, %c \n, ch);

fscanf

fscanf(file, %c, &ch);

fread

fread(buffer, sizeof(char), 128, file);

fwrite

fwrite(buffer, sizeof(char), 128, file);

feof

while( !feof(file) )
ch = getc(file);

ferror

if ( ferror(file) )
printf(error);

fseek

fseek(file, 128, SEEK_SET);

12-7, 12-8

- 33 -

Printed by Apric 2004/5/1

main(argc, argv)
int argc;
char *argv[];
{
...
}

main(int argc, char* argv[])


{
...
}

1. argc (argument count)


2. argv (argument value)
argv[0] argv[1]argv[2]...

type 123 abc.txt


argc = 3;
argv[0] = type ()
argv[1] = 123
argv[2] = abc.txt

#include <stdio.h>
int main (int argc, char* argv[])
{
int i;
printf(The value of argc is %d \n, argc);
for(i=0; i<argc; i++)
printf(argv[%d]=%s \n, i, argv[i]);
return 0;
}

- 34 -

Printed by Apric 2004/5/1

You might also like