Professional Documents
Culture Documents
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-
(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-
1. (Machine Language)
(0 1)
2. (Assembly Language)
(assembler)
3. (High-level Language)
Interpreter
Compiler
z z
(.obj)
z
-3-
BASIC
z
HTML
C/C++
COBOL
PASCAL
1. (Procedure-Oriented Programming)
2. (Object-Oriented Programming)
C++JavaVisual BASIC
COBOL
BASIC
Pascal
Ada
FORTRAN
-4-
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
(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-
(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-
(Escape Sequence)
\n
\"
\x
ASCII (16 )
\f
\'
\d
ASCII (8 )
\t
\/
\b
\\
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-
12345
%10d
12345
%+d
12345
%-10d
12345
% d
12345
%010d
123.456
%7.2f
123.456
%010.3f
123.456
%+10.4f
1. &
2. &
%d
%f
%c
%s
int
float, double
char
Enter 2 numbers:
scanf("%d,%d",&num1,&num2);
103,227
-9-
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 -
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;
if (a > 0)
abs = a;
else
abs = -a;
- 11 -
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 -
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() */
/* rand() */
a
b
1. 7 10
2.
- 13 -
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 -
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 -
1.
2. (, abstraction)
3.
(Function)
()
{
;
;
return ;
}
(Prototype)
main()
();
1. (call by value)
2. (call by reference)
(Recursive Function)
1.
2. (stack)
3.
- 16 -
(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 -
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)
[]
Input:(n,A,B)=(3,1,3)
Output: 13
12
32
13
21
23
13
:7
- 18 -
9
#define
1.
2.
3.
4. (Macro)
#include
(.h)
< >
C:\Dev-C++\Include\
#include <stdio.h>
" "
#include "D:\myprog\area.h"
- 19 -
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
/* 1 */
/* */
/* 0 */
[][];
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 -
/* */
(Pointer)
#include <stdio.h>
#define SIZE 5
/* */
Memory
int main(void)
In main()
{
int data[SIZE] = {23,25,28,29,27};
data
printf_matrix(data);
23
25
28
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 -
11
C
\0
char [];
name
10
\0
11
12
13
14
char b = c;
sizeof(b) 1 = 1 byte
sizeof(str) 1 + \0 = 2 bytes
/
(1)
(2)
scanf("%s", );
gets();
printf("%s", );
puts();
- 22 -
#include <stdio.h>
#include <stdio.h>
int main(void)
int main(void)
char name[15];
char name[15];
scanf("%s", name);
gets(name);
puts("Hi! ");
puts(name);
name);
return 0;
return 0;
}
}
char [][];
name[0] 0253FDB8
\0
name[1] 0253FDC2
name[2] 0253FDCC
\0
\0
- 23 -
12
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 -
1.
2. /
3.
/* a p1 */
p2 = &b;
/* b p2 */
pch = &ch;
/* ch pch */
/* */
p1++;
/* p1 4 bytes (int ) */
pch--;
/* */
/* 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
return
return
/* */
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;
}
*(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 -
*[];
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
name[1] J
name[2] T
d \0
g \0
e \0
**;
0253FDA4
0253FDD0
ex: 15
0253FDA4
- 27 -
0253FDD0
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
row int*
( int )
- 28 -
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 -
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;
}
/* */
/* 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++;
}
/* */
/* */
int main(void)
{
struct mydata student;
- 30 -
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
typedef struct
{
int minite;
float second;
} time;
time record = {3, 27.25};
- 31 -
14
text file
binary file
ASCII
1
()
132956 6
r+
w+
a+
rb
wb
ab
- 32 -
FILE *file;
char ch;
char buffer[128];
fopen
fclose
fclose(file);
getc
ch = getc(file);
putc
putc(ch, file);
fgets
fputs
fputs(buffer, file);
fprintf
fscanf
fread
fwrite
feof
while( !feof(file) )
ch = getc(file);
ferror
if ( ferror(file) )
printf(error);
fseek
12-7, 12-8
- 33 -
main(argc, argv)
int argc;
char *argv[];
{
...
}
#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 -