Download - C Programming Language
-
C Programming Language
ContentsChapter 01C Chapter 02C Chapter 03 Chapter 04 Chapter 05 Chapter 06 Chapter 07Chapter 08Chapter 09Chapter 10
ContentsChapter 11 Chapter 12 I/OChapter 13 Chapter 14 Chapter 15 C
Appendix A Appendix B
-
Chapter 01
C
C (I)C?UNIX
O
C (II)C
C UNIX () (Embedded System)
(I)1. 2. 3. 4. & 5. 6. 7.
(II)CompilerLinker (*.c & *.h) (*.obj) (*.exe)
-
Chapter 02
C
, Hello, World~!/* HelloWorld.c */#include
int main(void){printf(Hello, World~!\n);return 0;}
(I)#include #include . , .
main() C .
, , . () .
(II)( ) .
C . .
, , .
(III) .main() .
. . . : // : /* */
(IV)(ISO/ANSI C)
-
Chapter 03
(I)
* .
() short intO2-332768 ~ 32767intO4-2147483648 ~ 2147483647long intO4 -2147483648 ~ 2147483647unsigned short intX20 ~ 65535unsigned intX40 ~ 4294967295
(II)
printf() %d, %o, %x
int num;int a, b, c; // 3 int age = 30;// printf(I am %d years old. \n, age);printf(I am %o years old. \n, age);printf(I am %x years old. \n, age);
: -127 ~ 128 ( ) ASCII (0 ~ 127) ( ) (Escape Sequences)
printf() %c
char grade; char gradeA = A, gradeB = 66;// printf(My grade is %c. \n, gradeA);printf(Your grade is %c. \n, gradeB);
(I)
* .
(II)
printf() %f, %e, %a, %lf, %le
float a = 10, b = 20;double percent = a/100;double c = 0.3;printf(percent is %f. \n, percent);printf(percent is %e. \n, percent);printf(percent is %a. \n, percent);printf(a : %f , b : %e \n, a, b);
, . .1 sizeof(char) sizeof(short) sizeof(int) sizeof(long)sizeof(float) sizeof(double) sizeof(long double)sizeof .
( ) ( ) Type Promotion( ) .() bool, char, short -> intStandard Type Conversion( ) .() int double ( ) () float f = (float)100;
(I)? . , , , , . . . .; . . 0 .
(II)? . , , , .
int a = 123;//123 char gradeA = A// A double d = 3.141592// 3.141592
const int MAXLEN = 20; //MAXLEN 20
const double PI = 3.141592//PI 3.141592
-
Chapter 04
?C
getchar() (stdin) putchar() (stdout)
#include int getchar(void);int putchar(int c);// EOF
gets() (stdin) (\n) (\0) puts() (stdout) (\0) (\n) .
#include char* gets(char* s);int puts(const char* s);// EOF
(I) printf() ,
#include
int printf (const char *format, ...);
(II)printf()
%a, %A , 16%c %d, %i 10 %e , e, %E: , E%f , %g, %G %f %e, %E %o 8 %p%s%u 10 %x, %X 16 %%
(III)printf() () , .
- + , , , , # # , 0 0 . . %e, %E, %f , %g, %G , %s , l long int, unsigned long int L long double
(I) scanf() printf() () (, , )
#include
int scanf (const char *format, ...);
(II)scanf() & . & .%s .
.
(, , , ) .
fflush() .
1 (A-Z) 1, (a-z) 2, (0-9) 3 0 .(EOF )
A-Z .
-
Chapter 05
+, - , *, / , %%() .++, -- .=, -=, +=, . =, ==,!= 1 0 &&, ||, ! 1 0 &, |, ~, ^ 0 * . & ( type) . . , -> ?1:2 1 2 (),[],{} .
, , , ,
()1 -> +,-()2, -> *,/,%3, , -> +,-()4, ->
1
++a 1 a++ 1 --a 1 a-- 1
(=)
(+=, -=, *=, /=, %=, %=)
int num1, num2 = 0;num1 = 100;num1 = num1 + 20;num1 = num1 + num2;a += b;// a = a + b;a -= b;// a = a - b;a *= b;// a = a * b;a /* = b;// a = a / b;a %= b;// a = a % b;
() , , , true(=1), false(=0)
< ?= ?> ?!= ?
()
&& : AND true true || : OR true true ! : NOT true false, false true
(I) (0 1) (char )
~ (NOT)1 0, 0 1 & (AND) 1 1 | (OR) 1 1 ^ (Exclusive OR : XOR) 1
(II) : : 1 : 0 : 0 1, 1 0
,
: 15 2 (15
() ( ) : ()
int a;double b = 10.25;a = (int)b; //b a .
if~else 3 : ? 1 : 2
int a = 10, b;b = (a > 0) ? 1 : 0; //a 0 1, 0 a = (b == 1) ? 1 : 0; //b 1 1, 0
. . 3 . 3 0 1 0xF120 ^ ^ . . ( ) : . : .
-
Chapter 06
: ifswitchgoto
: whiledo whileforcontinue & break
if(I)
if (condition1)statement1;else if (condition2)statement2;elsestatement3;condition1statement1condition2statement2statement3truetruefalsefalsecondition1 true statement1 false condition2 condition2 true statement2 false statement3 .
if(II) if
if (condition)statement1;if (condition)statement1;elsestatement2;
if (condition1)statement1;else if (condition2)statement2;elsestatement3;
if (condition1)if (condition3)statement4;elsestatment5;else if (condition2)if (condition4)statement6;elsestatement7;elsestatement3;
switch(I)
switch (n){case value1 : statement1; break;case value2 : statement2; break;default : statement3;}n value1 statement1 . value2 statement2 . other value statement3 .
switch(II)switch
char ch = A;switch(ch){case A : printf (A...\n); break;case B : printf (B...\n); break;case C : printf (C...\n); break;case D : printf (D...\n); break;default : printf (!!\n);}
char ch = A;switch(ch){case A : printf (A...\n);case B : printf (B...\n);case C : printf (C...\n);case D : printf (D...\n);default : printf (!!\n);}
gotogoto
goto label;statement1;statement2;statement3;label : statement4;goto label statement1, statement2, statement3 label statement4 .if (num > 10)goto labelA;goto labelB;labelA: num += 100;labelB: num *= 2;if (num > 10)num += 100;elsenum *= 2;
while(I)
while (condition){statement1;statement2;statement3;}statement4;condition true { } statement1, statement2, statement3 .while true
while(II)while while while .
int i=1, j;while (i < 10){j = 1;while (j < 10){printf (i : %d,\t j : %d \n, i, j);j++;}i++;}
do-while
do{statement1;statement2;statement3;} while (condition)statement4;statement1, statement2, statement3 condition true statement1,2,3 .do-while false
for(I)
for (; ; ){statement1;statement2;}statement3; , true/false true statement1, statement2 . .for ,
for(II)for for for .
for ( int i = 1; i < 10; i++){for ( int j = 1; j < 10; j++){printf (i : %d,\t j : %d \n, i, j);}}
continue & breakcontinue , .switch break switch .continue .
break .break .
10 .1 100 2 .1 100 . . . (for ) . . (, 0 .) . (0 ) , , , . 2 2*2, 3 3*3, 4 4*4, , 9 9*9 break continue .
-
Chapter 07
. () ( ) .
(reusability) (divide-and-conquer approach) , . .
(I)#include int selectmenu(void);// int main(void){int sel, num1, num2, sum;sel = selectmenu();// if (sel == 1) sum = num1 + num2;else if (sel == 2) sum = num1 num2;else if (sel == 3) sum = num1 * num2;else if (sel == 4) sum = num1 / num2;}int selectmenu(void)// {int selmenu=1;printf(--- ---\n);printf( 1., 2., 3., 4. : );scanf(%d, &selmenu);return selmenu;}
(II)int sel, num1, num2, sum;sel = selectmenu();int selmenu=1;printf(--- ---\n);printf( 1., 2., 3., 4. : );scanf(%d, &selmenu);return selmenu;
if (sel == 1) sum = num1 + num2;else if (sel == 2) sum = num1 num2;else if (sel == 3) sum = num1 * num2;else if (sel == 4) sum = num1 / num2;
step1. main() , . step2. main() . step3. sel num sum .
(Naming Rule) . , .
: float f_add(float, float), int i_add(int, int)
: int GetNumber(void), char* GetLastName(char* FullName)
, , .
. , . ();
. . . return . ( void .)
(){ ; ;return ;}
.
, .
.
= ();
(I) . .
( ) (call by value) (C ) . (call by reference) ( ) .
(II)1#include 23void swap(int, int);45int main(void)6{7int i = 10; j = 20;8swap(i, j);9printf( i = %d, j = %d \n, i, j);10return 0;11}1213void swap(int n1, int n2)14{15int temp = n1;16n1 = n2;17n2 = temp;18printf( n1 = %d, n2 = %d \n, n1, n2);19}
(III) (call by value).step1. main() 7 step2. swap() (14 ) step3. swap() 15
0x1012n2200x1008n1100x1004j200x1000i10
0x1016temp100x1012n2200x1008n1100x1004j200x1000i10
(IV) (call by value) (contd)step4. swap() 17 step5. swap() main() 9
0x1004j200x1000i10
(V)1#include 23void swap(int*, int*);45int main(void)6{7int i = 10; j = 20;8swap(&i, &j);9printf( i = %d, j = %d \n, i, j);10return 0;11}1213void swap(int* n1, int* n2)14{15int temp = *n1;16*n1 = *n2;17*n2 = temp;18printf( n1 = %d, n2 = %d \n, *n1, *n2);19}
(VI) (call by reference).step1. main() 7 step2. swap() (14 ) step3. swap() 15
0x1004j200x1000i10
0x1004n2(j)200x1000n1(i)10
0x1008temp100x1004n2(j)200x1000n1(i)10
(VII) (call by reference) (contd)step4. swap() 17 step5. swap() main() 9
0x1008temp100x1004n2(j)100x1000n1(i)20
0x1004j100x1000i20
main(){fnA(); //-level 1 fnB(); //-level 1 }void fnA(){fnC(); //-level 2 fnF(); //-level 2 }void fnB(){fnC(); //-level 2fnE(); //-level 2}void fnC(){fnD(); //-level 3}mainfnAfnBfnCfnDfnEfnF
(Recursive Function) .
long factorial(int n){long res;
for (res=1; n>1; n--)res *= n;return res;}long factorial(int n){long res;
if (n > 0)res = n * factorial(n-1);elseres = 1;return res;}
(/)- . . : .
.
(n) 1 n .
3 .
1~10 .
2 .
-
Chapter 08
?
: : : ([ ]) : ( ) : 0
[]
(I)
, 0
int arr[10] = {1,2,3,4,5,6,7,8,9,10};int arr[10] = {1,2,3,4,5,6,7,8};int arr[10] = {1,2,3,4,5,6,7,8,0,0};// .int arr[10] = {1,2,3,4,5,6,7,8,9,10,11,12};
(II) (contd) .
const . . .int arr[] = {1,2,3,4,5,6,7,8};// 8 arr 8.const int arr[10] = {1,2,3,4,5,6,7,8,9,10};
. .
. : , .int arr[10] = {1,2,3,4,5,6,7,8,9,10};// 10// : 0 19
int arr[10] = {1,2,3,4,5,6,7,8,9,10};arr[0] arr[2] arr[4] arr[6] arr[8] arr[1] arr[3] arr[5] arr[7] arr[9]
(I)2
// , , , , 5 int jumsu[5];// 5 int jumsu[3][5]
(II)3 ( ........) 3 , 4 .
// 5 2 int jumsu[3][5] // 3 10 5 3 int jumsu[10][3][5]// 6 10 3 5 4 int jumsu[6][10][3][5]
(I) 1 .1 .
//1 int jumsu[5] = {90,80,75,95,70};//2 int jumsu[3][5] = { {90,80,75,95,70}, // {92,70,75,85,80}, // {95,88,75,75,90} // }* jumsu[3][5] 3 X 5 .
(II) (contd) . .int jumsu[3][5] = { {90,80,75}, // {92,70,75,85}, // {95,88} // }// 0 .int jumsu[3][5] = { 90, 80, 75, 92, 70, 75, 85, 95, 88 }// , 0 ?
int jumsu[3][5] = { {90,80,75,95,70}, // {92,70,75,85,80}, // {95,88,75,75,90} // }
jumsu[0]jumsu[1]jumsu[2]jumsu[0]jumsu[1]jumsu[2]jumsu[0][0]jumsu[1][0]jumsu[2][0]jumsu[0][2]jumsu[1][3]jumsu[2][4]
10 int arr[i] 0 i .getchar() 10 char . 2 . .5 ,, .8 X 9 2 .3 X 4 2 4 X 3 2 .
-
Chapter 09
? . .
int * pi; // char * pc; // double * pf;//
[] * [];
(*) , * .
(&) , & .int num1 = 20;int num2 = 30;int *pi;pi = &num1;printf(%d, %d\n, pi, *pi);0x10000x10040x10080x100cnum1 = 20num2 = 30pi = 0x1000
.() . . . . . .
(I) . . . . 1
int arr[6] = {1,2,3,4,5,6};int *parr = arr;
(II)
arr arr = arr[0] = &arr[0][0]arr + 1 (int [2]) . arr + 1 = arr[1] = &arr[1][0]arr 2 2 . arr[0][0] = *arr[0] = **arr
int arr[3][2] = {{1,2},{3,4},{5,6}};int (*parr)[2] = arr;
(III) int double .
.int num = 10;double dnum;int *pi = #double *pd = &dnum;dnum = num;// .pd = pi;//~!!
(IV), , (call by reference) . . ( ) . 1. 2. .1 //int 1 int arr_sum(int *arr, int size); //1int arr_sum(int *, int); //2int arr_sum(int arr[], int size);//3int arr_sum(int [], int);//4
(V), , (contd) //int 3 X 2 int arr_sum(int (*arr)[2], int size); //1int arr_sum(int arr[][2], int size);//2
//int 4 X 3 X 2 int arr_sum(int (*arr)[3][2], int size); //1int arr_sum(int arr[][3][2], int size);//2
(**) .int num = 10;int * pi = #int ** ppi =
printf(%d\n,&pi);printf(%d\n,ppi);
printf(%d\n, num);printf(%d\n,*pi);printf(%d\n,**ppi);
const(I) . .int num = 10;const int* pi = # *pi = 100; //~!!num = 100;//
const(II) (contd) .
//int 1 int arr_sum(const int *, int); // int arr_sum(const int *arr, int size)// {int i, sum = 0;for (i = 0; i < size; i++)sum += arr[i];for (i = 0; i < size; i++) arr[i]=0;//~!!return sum;}
const(III) .
. .
int num1 = 10;int num2 = 20;int* const pi = &num1; pi = &num2; //~!!*pi = 100;//int num1 = 10;int num2 = 20;const int* const pi = &num1; *pi = 100; //~!!num1 = 100;//pi = &num2;//~!!
NULL 0 NULL 0
int * pi = 0; //int * pi = NULL;char * pc = 0;//char * pc = NULL;double * pd= 0;//double * pd = NULL;
. . .typedef .
// int, int int (*fct1)(int);// , double void (*fct2)(double,double);
void ., , void . , .void . .
int num = 10;char ch = A;void *vp; //void vp = #//int vp = &ch; //char
3 X 4 2 4 X 3 2 . , .
int . .5 ,, 3X5 int[5] . double Add, Minus, Multiply, Divide . .
012312342345
012123234345
?
? (high-level) : (low-level) :
C 1972, ALGOL60 -> CPL -> BCPL -> B -> CC 1. : .2. : C .3. : C C .4. : C .5. : .
, , C & (Source File) C .( : *.c)(Compiler)C . (Object File) ( : *.obj) (Library File) .(Linker) . (Execute File) .( : *.exe)
/* Hello.c */ // . 2. include // .3.4. int main(void) //main() ! 5. { // .6. printf(Hello, World~!\n);// . 7. return 0; // 0 .8. } // .
:Hello, World~! #include : #include #include #include mystring.h
main() : int main(void)
: { ......... ( ) ......... }
( ) : int num; int a, b; char name[5]=hong; const int MAXLEN = 20;
: printf(Welcome to C World~!); strcpy(dest, hong gil dong); double tax = mytax(salary);
: salary = 100; int annualsalary = salary * 12; a = (b==c);
: int main() { return 0; } double mytax(salary) {return salary*0.7;}
: /* : mytax : . : 2005 1 1 : */ // .
C... . main . (;) . .int int . unsigned int = unsigned long int = long signed int = signed
signed/unsigned signed . signed int = int signed short int = short int signed long = long
. int int .
8 16 8 : 0 8 16 : 0x 16 10 : 8 16 10 :int a = 10; int b = 012; int c = 0xa;
[]
#include
int main(void){ int a = 30; int age = a; printf(I am %d years old.\n, age); printf(I am %o years old.\n, age); printf(I am %x years old.\n, age);} (Escape Sequences) \a : alarm \b : backspace \f : form feed \n : new line \r : carriage return \t : horizontal tab \v : vertical tab \\ : backslash \ : single quotation \ : double quotation \? : question mark \099 : 8 \x99 : 16
[]
#include
int main(void){ char gradeA = A; char gradeB = 66; printf(My grade is %c !! \n, gradeA); printf(Your grade is %c !! \n, gradeB);}
(floating-point number) . .
( ) , .
C99 . : float _Complex, double _Complex, float _Imaginary, double _Imaginary[]
#include
int main(void){ float a = 10, b = 20; double percent = a/100; double c = 0.3; printf(percent is %f . \n, percent); printf(percent is %e . \n, percent); printf(percent is %a . \n, percent); printf(a : %f , b : %e, a, b);}
[] sizeof
#include
int main(void){ char c = A; int i = 1; float f = 2.1; double d = 10.24;
printf(value of c : %c \t & size of c : %d bytes.\n, c, sizeof(c) ); printf(value of i : %d \t & size of i : %d bytes.\n, c, sizeof(i) ); printf(value of f : %f \t & size of f : %d bytes.\n, c, sizeof(f) ); printf(value of d : %f \t & size of d : %d bytes.\n, c, sizeof(d) );
printf(size of CHAR : %d bytes.\n, sizeof(char) ); printf(size of INT : %d bytes.\n, sizeof(int) ); printf(size of FLOAT : %d bytes.\n, sizeof(float) ); printf(size of DOUBLE : %d bytes.\n, sizeof(double) );
return 0;}[] ,
#include
int main(void){ int a=1, b=10;
int d1 = a/b; // double d2 = (float)a / (float)b; //
printf("value of d1 : %f \n", d1); printf("value of d2 : %f \n", d2); return 0;} ( ) ( : _ )
[] 0 #include
int main(void){ char ch;
while ((ch = getchar()) != '0') putchar(ch); return 0;}
[] EOF(Ctrl+Z) #include
int main(void){ int ch; while ((ch = getchar()) != EOF) putchar(ch); return 0;}[] gets() puts() #include
int main(void){ char answer[80];
puts("How are you, today?"); gets(answer); // . puts(answer); // .
return 0;}
printf() printf() f (formatted) .
[] printf() , , , #include
int main(void){ char c1 = 'A'; char c2 = 66; char s1[20] = "Hong Gil Dong"; char s2[20] = "Student"; double d1 = 1.5e-4; double d2 = 1.5e-5;
int i = 11;
printf("c1 %c, ASCII %d.\n", c1); printf("c2 %c, ASCII %d.\n", c2);
printf(" %s .\n", s1); printf(" %s .\n", s2);
printf("d1 : %g \n", d1); printf("d2 : %g \n", d2);
printf("i 8 : %o \n", i); printf("i 10 : %d \n", i); printf("i 16 : %x \n", i);
return 0;}[] printf() #include
int main(void){ int i = 10; double f = 1.5; char s[20] = "Hong Gil Dong";
printf(" i : %10d.\n", i); printf(" i : %-10d.\n", i); printf(" i : %010d.\n", i); printf(" f : %f \n", f); printf(" f : %3.2f \n", f);
printf(" s : %s. \n", s); printf(" s : %.10s. \n", s); printf(" s : %20s. \n", s); printf(" s : %-20s. \n", s);
return 0;}scanf() scanf() f (formatted) .
printf() %f, %e, %E, %g, %G float double scanf() float printf() , double l
gets() , scanf() .[] #include
int main(void){ int num1, num2;
printf("Enter the 1st number : "); scanf("%d", &num1);
printf("Enter the 2nd number : "); scanf("%d", &num2);
printf("summary : %d \n", num1+num2);
return 0;}
(operand) ,
.
[] #include
int main(void){ short a,b,c; a=20000; b=30000;
c=a+b; printf("%d + %d = %d\n",a,b,c); // c ??? c=a-b; printf("%d - %d = %d\n",a,b,c); // c ??? c=a*b; printf("%d * %d = %d\n",a,b,c); // c ??? return 0;}
[] char int + #include
int main(void){ int a,b,c; char ch1 = 'A', ch2 = 'B', ch3; a = 10; c=a+(b=(c=5)); printf("%d\n",c); ch3 = ch1 + ch2; // . printf("ch3 : %c %d \n", ch3, ch3); return 0;}[] #include
int main(void){ int a = 10, b, c = 10; printf("%d \n", a++); printf("%d \n", ++a); printf("%d \n", a--); printf("%d \n", --a); b = a++ + ++a; printf("b : %d \n", b);
c = c++ + c++ + c++ + c++; printf("c : %d \n", c);
return 0;} . (type promotion) (type demotion) .
[] #include
int main(void){ int a,b; const int c=30;
a=10; b=5;
c=a+b; //error C2166: l-value specifies const object printf("%d\n",c);
return 0;}
[ 100 1, 0 #include
int main(void){ int num1, num2;
num1 = 100;
printf("Enter one number : "); scanf("%d", &num2);
printf("%d \n", num1 == num2);
return 0;}
[] #include
int main(void){ int num1, num2, num3, num4;
num1 = 100;
printf("Enter 1st number : "); scanf("%d", &num2);
printf("Enter 2nd number : "); scanf("%d", &num3);
num4 = num1 0) ? 1 : 0; a = (b == 1) ? 1 : 0;
printf("a = %d\n", a); printf("b = %d\n", b);
return 0;}
[] if #include
int main(void){ int jumsu; printf( . : ); scanf(%d, &jumsu); if (jumsu >= 90) printf( : %d, \t : A \n, jumsu); else if (jumsu >= 80) printf( : %d, \t : B \n, jumsu); else printf( : %d, \t : F \n, jumsu);
return 0;}
[] , if #include
int main(void){ int num1, num2; char op; printf( . : ); scanf(%d, &num1); printf( . : ); scanf(%d, &num2); printf( .[+, -, *, /] : ); scanf(%c,&op); if (op == +) printf(%d + %d = %d \n, num1, num2, num1+num2); else if (op == -) printf(%d - %d = %d \n, num1, num2, num1-num2); else if (op == *) printf(%d * %d = %d \n, num1, num2, num1*num2); else if (op == /) printf(%d / %d = %f \n, num1, num2, num1/num2); else printf( .);
return 0;}
switch expression int char .switch default . expression case .
[] switch #include
int main(void){ char ch; printf( . : ); scanf(%c,&ch); switch (ch) { case A : printf(A .); break; case B : printf(B .); break; case C : printf(C .); break; case D : printf(D .); break; default : printf( .); }
return 0;}
[] switch #include
int main(void){ int num1, num2; char op; printf( . : ); scanf(%d, &num1); printf( . : ); scanf(%d, &num2); printf( .[+, -, *, /] : ); scanf(%c,&op); switch (op) { case + : printf(%d + %d = %d \n, num1, num2, num1+num2); break; case - : printf(%d - %d = %d \n, num1, num2, num1-num2); break; case * : printf(%d * %d = %d \n, num1, num2, num1*num2); break; case / : printf(%d / %d = %f \n, num1, num2, num1/num2); break; default : printf( .); }
return 0;}goto .
[] 10 labelA labelB #include
int main(void){ int num; printf( . : ); scanf(%d,&num); if (num >= 10) goto labelA; goto labelB;
labelA: printf(10 \n); num += 100; labelB: num *= 2;
printf(num : %d \n, num); //num ~!! return 0;}while . .
[] while 1 10 #include
int main(void){ int num = 1; int sum = 0; while (num