SlideShare a Scribd company logo
1 of 124
C  Programming [email_address]
Computing ,[object Object],Monitor Printer CPU RAM Hard Disk Keyboard Mouse Hardware Operating System Software 제 어 출 력 연 산 기 억 입 력 기  능
C Language ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Syntax ,[object Object],[object Object],[object Object],[object Object],[object Object],#include <stdio.h> // comments main( ) { printf (“Welcome”); }
Data Presentation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Constant 1 Byte * n 1 Byte 4 Byte 4 Byte ASCII * n 문자열 string ASCII 문자 char acter 문자상수 1s +  지수  +  가수 = -3.4E- 38  ~ 3.4E 38 실수 float ing number 1s + 2 31 = -21 억  ~ +21 억 정수 int eger 숫자상수
Variable ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],문자열 string 문자 char 문자변수 실수 float 정수 int 숫자변수
Address int  a; char b; a = 10; b = ‘a’; Memory(RAM) … 100 101 102 103 104 105 106 107 … a: 4 Byte b: 1 Byte 10 10 (1010 2 ) ‘ a’ (97 10 )
Standard I/O cf. gets vs scanf(“%s” …) All printf String puts One char putchar Output All scanf String gets One char getchar Input functions
printf (1) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
printf (1): practice ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Arithmetic Operation ,[object Object],[object Object],[object Object],나눗셈 :  나머지 % 나눗셈 :  몫 / 복합연산 +=, -=, *=, /=, %=, ++, -- 곱  셈 * 뺄  셈 - 덧  셈 + Operations Operands
Type Cast ,[object Object],[object Object],[object Object],[object Object],int  x, a, b=5, c=2; float  y, d, e=2.0; x = 5 / 2; a = b / c; d = b / e;
printf (2) ,[object Object],[object Object],[object Object],printf(“c:  program  test.c”); Backslash  printf(“ 특수문자는  !,@,#,    등이다 .”); Quotation(“)  printf(“ 선택하세요 .[ ]  ”); Backspace  printf(“Beep!!  ”); Alarm  printf(“ 국어  영어  수학” ) Tab  printf(“1 st  line   2 nd  line”); New line
scanf ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],int  a; char  c; printf (“input integer: “); scanf (%d”, &a); printf (“input character: “); scanf (“ %c ”, &c); 입력  buffer ,[object Object],[object Object],3  ,[object Object], ,[object Object],fflush (stdin); scanf  사용 시 주의점
[object Object],[object Object],[object Object],[object Object],scanf,  연산 , printf : practice (1) 소수점  3 자리 실수 입력 : 12.345 12,  12.3,  12.35 화씨온도 : 100 입력한 화씨  100 도는 섭씨  37.8 도 화씨온도 : 100 입력한 화씨  100 도는 섭씨  37.8 도
scanf,  연산 , printf : practice (2) ,[object Object],[object Object],[object Object],영문자 : C C 는  3 번째  alphabet 두 정수 : 1, 2 교환 결과 : 2, 1
1 2 3 4
제어문  ( 분기문 ): if ,[object Object],[object Object],[object Object],if ( 조건식 ) { 문장  t; } 조건식 문장  t true false
제어문  ( 분기문 ): if ,[object Object],[object Object],if ( 조건식 )  { 문장  t; } else { 문장  f; } 조건식 문장  t true false 문장  f
제어문  ( 분기문 ): if ,[object Object],[object Object],[object Object],if ( 조건식 1)  { if ( 조건식 2)  문장  tt; else   문장  tf; } else { 문장  f; } 조건식 1 조건식 2 문장  f 문장   tf 문장   tt false true true false
제어문  ( 분기문 ): if ,[object Object],[object Object],[object Object],[object Object],[object Object],if ( 조건식 1)  문장  t; else if ( 조건식  2) 문장  ft1;  … else if ( 조건식  n) 문장  ftn; else 문장  f; 조건식 1 문장  t false true 조건식 2 문장  ft1 true … 조건식 n 문장  ftn true 문장  f false false …
제어문  ( 분기문 ): if ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
제어문  ( 분기문 ): if ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],if: practice (1) 정수 : 3 3 은  odd number 시각  [0~23]: 15 오후  3 시 alphabet: a A
[object Object],if: practice (2) 당신의 학년은 ? [ 신입생 :1,  재학생 :2]: __ 당신의 성별은 ? [ 남자 :1,  여자 :2]: __ 재학생 전용 창구로 가세요 . 당신은 남자입니다 . 당신은 여자입니다 . 종 료 1 2 2 1
if: practice (3) ,[object Object],[object Object],[object Object],문자 : 6 number 문자 : A Capital letter 문자 : y Small letter 문자 : @ Symbol 첫번째 수 : 3 두번째 수 : 6 세번째 수 : 9 최대값  = 9
제어문  ( 분기문 ): switch ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],Algorithm ,[object Object],= + Variable ,[object Object],[object Object]
제어문  ( 반복문 ): while ,[object Object],[object Object],초기화 ; while (1) { 수행문 1~n; 누적처리 ; if ( 조건 ) break; } 초기화 ; while ( 조건 ) { 수행문 1~n; 누적처리 ; }
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],제어문  ( 반복문 ): while Start 초기화 더하는 숫자가 10 보다 큰가 ? 누적 합 합 출력 End No Yes
[object Object],[object Object],[object Object],while: practice (1)
while: practice (2) ,[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],while: practice (3)
제어문  ( 반복문 ): for ,[object Object],[object Object],초기화 ; while ( 조건 ) { 수행문 1~n; 누적처리 ; } while Loop for ( 초기값 ;  최종값 ;  증 / 감 ) { 수행문 1~n; } for Loop
제어문  ( 반복문 ): for ,[object Object],int no = 1, sum = 0; w hile (no <= 10) { sum += no; no ++; } int no, sum = 0; for (no = 1; no <= 10; no ++) sum += no; while Loop for Loop
[object Object],[object Object],[object Object],[object Object],for: practice (1)
[object Object],[object Object],[object Object],for: practice (2)
[object Object],[object Object],[object Object],for: practice (3) Advanced
main( ) { int row, col; for (row = 1; row < 10; row ++) { printf (&quot;&quot;); for (col = 1; col < 10; col ++) printf (&quot;%2dx%2d=%2d&quot;, row, col, row*col); } } 제어문  ( 반복문 ):  이중  Loop
main( ) { int count, i, j; printf (&quot;Repeat count ?&quot;); scanf (&quot;%d&quot;, &count); for (i = 1; i <= count; i ++) { for (j =  i ; j <=  count ; j ++) printf (&quot;*&quot;); printf (&quot;&quot;); } } 이중  Loop: practice (1)
main( ) { int count, i, j; printf (&quot;Repeat count ?&quot;); scanf (&quot;%d&quot;, &count); for (i = 1; i <= count; i ++) { for (j =  1 ; j <=  i ; j ++) printf (&quot;*&quot;); printf (&quot;&quot;); } } 이중  Loop: practice (2)
main( ) { int n, i, j, cnt=0, cnt2=0; printf (“Repeat count = &quot;); scanf (&quot;%d&quot;, &n); for (i = 0; i < n; i += 2) { for (j = 0; j <= n/2-cnt-1; j ++) printf (&quot; &quot;); for (j = 0; j < i+1; j ++) printf (&quot;*&quot;); cnt ++; printf (&quot;&quot;); } cnt2 = 1; for (i = n-1; i > 1; i -= 2) { for (j = 0; j < cnt2; j ++) printf (&quot; &quot;); for (j = 0; j < i-1; j ++) printf (&quot;*&quot;); cnt2 ++; printf (&quot;&quot;); } } Advanced 이중  Loop: practice (3)
이중  Loop: practice (4) ,[object Object],Advanced
Array ,[object Object],[object Object],int a; int b; … 1,000  문장 int a[1000]; 변수 사용 배열 사용 a . . b . . 100 . . 112 . . a[0] a[1] a[2] … a[999] 100 104 108 … 100+999*4 1  문장
Array ,[object Object],[object Object],int  a[5]; a[0] = 1; a[3] = 4; scanf(“%d”, &a[1]); printf(“%d”, a[1]); 1 4 a[5] a[0]  a[1]  a[2]  a[3]  a[4]
Array ,[object Object],[object Object],[object Object],[object Object],[object Object],sizeof (a) ? int a[ ] = { 1, 2, 3 };
Array ,[object Object],[object Object],[object Object],int  a[5], size; char  b; printf(“one char size=%d”, sizeof(b)); size = sizeof(a); printf(“ 배열의 크기는  %d Byte”, size);
Array ,[object Object],// 100 개의 data  입력 scanf(“%d”, &a[0]); scanf(“%d”, &a[1]); …… scanf(“%d”, &a[99]); // 100 개의  data  출력 printf(“%d”, a[0]); printf(“%d”, a[1]); …… printf(“%d”, a[99]); // 100 개의 data  입력 for (n=0; n<100; n++) scanf(“%d”, &a[ n ]); // 100 개의  data  출력 for (n=0; n<100; n++) printf(“%d”, a[ n ]); index( 첨자 ) 에 변수 사용
[object Object],Array int  a[3][2]; a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1] 100 104 108 112 116 120 논리적 표현 Memory 세로 3 가로 2 a[0][0] a[0][1] a[1][0] a[2][0] a[1][1] a[2][1]
[object Object],Array // 6 개의 data  입력 scanf(“%d”, &a[0]); scanf(“%d”, &a[1]); …… scanf(“%d”, &a[5]); // 6 개의  data  출력 printf(“%d”, a[0]); printf(“%d”, a[1]); …… printf(“%d”, a[6]); // 6 개의 data  입력 for (i=0; i<3; i++) { for (j=0; j<2; j++) scanf(“%d”, &a [i][j] ); } // 6 개의  data  출력 for (i=0; i<3; i++) { for (j=0; j<2; j++) printf(“%d”, a [i][j] ); } index( 첨자 ) 에 변수 사용
Array ,[object Object],int  a[3][2]; int  a[6]; a[0][0] a[0][1] a[1][0] a[2][0] a[1][1] a[2][1] a[0] a[1] a[2] a[4] a[3] a[5] a = &a[0] = &a[0][0]
[object Object],[object Object],[object Object],[object Object],[object Object],Array: practice (1) 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Array: practice (2) Advanced ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],Array: practice (3)
Array: practice (4) void main( )  { int buf[10][10] = {0}; int i, j, n, ncnt, xcnt, ycnt, x, y; ncnt = 1; n= 5; x = y = xcnt = ycnt = 0; for (i=0; i<=n*n; i++) { buf[x--][y++] = ncnt++; if (x<0 && y<n) { xcnt ++; x = xcnt; y = ycnt; } if (y>=n) { ycnt ++; x = xcnt; y = ycnt; } } for (i=0; i<n; i++) { for (j=0; j<n; j++) printf(&quot;%3d&quot;, buf[i][j]); printf(&quot;&quot;); } } Advanced
Array: practice (5) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Advanced
main( ) { int count, num, kor, eng, mat, sum = 0, class_sum = 0, no; float ave; printf(&quot; 학생수를 입력하시오  : &quot;); scanf(&quot;%d&quot;,&count); printf(&quot; 번호 국어 영어 수학  총점  평균 &quot;); for (no = 1; no <= count; no ++) {   scanf(&quot;%d%d%d%d&quot;,&num,&kor,&eng,&mat);   printf(&quot;&quot;);   sum = kor + eng + mat;   ave = sum / 3.0;   class_sum = class_sum + sum;   printf(&quot;%2d %3d %3d %3d  %3d %4.1f&quot;,num,kor,eng,mat,sum,ave); } printf(&quot; 학생 수  = %2d  반 평균  = %4.1f&quot;,count,class_sum/3.0/count); } 학생수 학번 3 과목별 점수 반총점 [ 누적 ] ,[object Object],Array: practice (scoring #1) 개인총점
#define MAX_ARRAY 100 int count,  num[MAX_ARRAY], kor[MAX_ARRAY], eng[MAX_ARRAY], mat[MAX_ARRAY], sum[MAX_ARRAY],   class_sum = 0, no; float  ave[MAX_ARRAY] ; main( ) { for (no = 0; no <= MAX_ARRAY-1; no ++) sum[no] = 0; printf(&quot; 학생수를 입력하시오  : &quot;); scanf(&quot;%d&quot;,&count); printf(&quot; 번호 국어 영어 수학  총점  평균 &quot;); for (no = 0; no <= count-1; no ++) {   printf(&quot; %d 번째 : &quot;,no+1);   scanf(&quot;%3d%4d%4d%4d&quot;, &num[no],&kor[no],&eng[no],&mat[no] );   sum[no]  = kor[no] + eng[no] + mat[no];   ave[no]  = sum[no] / 3.0;   class_sum = class_sum + sum[no]; } printf(&quot; 번호 국어 영어 수학  총점  평균 &quot;); for (no = 0; no <= count-1; no ++) printf(&quot;%3d  %4d %4d %4d  %4d  %4.1f&quot;, num[no],kor[no],eng[no],mat[no],sum[no],ave[no] ); printf(&quot; 학생 수  = %2d,  반 평균  = %4.1f&quot;,count,class_sum/3.0/count); } ,[object Object],Array: practice (scoring #2)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],String (1)
String (2) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],main( ) { char string[20] = &quot; 대한민국 만세 &quot;; int count = 0; } NULL 이 나올 때 까지 while 을 이용한  array  검사 String: practice (1)
main( ) { int i; char b[25]; char a[25] = &quot;Puff the magic dragon !&quot;; } ,[object Object],array a 를  array b 로  copy 하기 위한 for loop 의 반복 횟수 String: practice (2)
#define MAX_ARRAY 100 int count, num[MAX_ARRAY], kor[MAX_ARRAY], eng[MAX_ARRAY], mat[MAX_ARRAY], sum[MAX_ARRAY], class_sum = 0, no; float ave[MAX_ARRAY]; char name[MAX_ARRAY][10]; main( ) { for (no = 0; no <= MAX_ARRAY-1; no ++) sum[no] = 0; printf(&quot; 학생수를 입력하시오  : &quot;); scanf(&quot;%d&quot;,&count); printf(&quot; 번호 성 명 국어 영어 수학  총점  평균 &quot;); for (no = 0; no <= count-1; no ++) { printf(&quot; %d 번째 : &quot;,no+1); scanf(&quot;%3d %s %4d%4d%4d&quot;,&num[no], name[no] ,&kor[no],&eng[no],&mat[no]); sum[no] = kor[no] + eng[no] + mat[no]; ave[no] = sum[no] / 3.0; class_sum = class_sum + sum[no]; } printf(&quot; 번호  성  명  국어 영어 수학  총점  평균 &quot;); for (no = 0; no <= count-1; no ++) printf(&quot;%3d  %s %4d %4d %4d  %4d  %5.1f&quot;, num[no], name[no] ,kor[no],eng[no],mat[no],sum[no],ave[no]); printf(&quot; 학생 수  = %2d,  반 평균  = %4.1f&quot;,count,class_sum/3.0/count); } ,[object Object],String: practice (scoring #3)
[object Object],[object Object],[object Object],[object Object],현재 문자열 : 1. 복사  2. 추가  3. 종료 번호를 입력하세요 : 1 문자열 : my 현재 문자열 : my 1. 복사  2. 추가  3. 종료 번호를 입력하세요 : 2 문자열 : test 현재 문자열 : mytest 1. 복사  2. 추가  3. 종료 번호를 입력하세요 : 4 입력 오류입니다 .  다시 입력하세요 . 현재 문자열 : mytest 1. 복사  2. 추가  3. 종료 번호를 입력하세요 : 3 C:문자열 처리 프로그램 String: practice (4)
String: practice (5) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],book  책 computer  컴퓨터 program  프로그램 keyboard  키보드 mouse  마우스 영어 단어를 입력하세요  ? keyboard ‘ keyboard’ 는 한글로 ‘키보드’ 입니다 . 영어 단어를 입력하세요  ? monitor 존재하지 않는 단어입니다 . 입출력 예 C:영한사전 프로그램 Advanced String: practice (6)
[object Object],[object Object],문자열  ? ABC[DEF]GHI ABCGHI 입출력 예 C:문자열 프로그램 Library Function: practice (1)
[object Object],[object Object],char str[10]; int result = 12345; itoa (result, str, 10) ; printf (&quot;%s&quot;, str); char str[6] = “12345”; int result; result =  atoi (str) ; result ++; printf (&quot;%d&quot;, result); Library Function
[object Object],[object Object],[object Object],[object Object],[object Object],숫자열  : 132 출  력  :  1 1 1 1 1 33333 3 33333 3 33333 22222 2 2 2 22222 입출력 예 C:전광판 Library Function: practice (2)
[object Object],[object Object],[object Object],압축 (C),  해제 (U): C 압축할 문자열 : aaabbbb999999ff 압축된 문자열 : (3)a(4)b(6)9(2)f 압축 (C),  해제 (U): U 압축된 문자열 : (3)a(4)b(6)9(2)f 압축할 문자열 : aaabbbb999999ff 입출력 예 C:문자열 압축 Advanced Library Function: practice (3)
[object Object],[object Object],첫번째 수 입력 : MCCXXVI 첫번째 수 값은  1226 두번째 수 입력 : LXVIIII 두번째 수 값은  69 덧셈 결과는  MCCLXXXXV (1295) 입출력 예 C:로마식 숫자 계산 Advanced Library Function: practice (4) 표 기 법 I  1 V  5 X  10 L  50 C  100 D  500 M  1000
main( ) { int count, i, j; printf(&quot; 반복 횟수  ?&quot;); scanf(&quot;%d&quot;,&count); for (i = 1; i <= count; i ++) { for (j = 1; j <= i; j ++) printf(&quot;*&quot;); printf(&quot;&quot;); } } int count; int input_count( ),  print_asterisk( ); main( ) { input_count(); print_asterisk(); } input_count( )  { printf(&quot; 반복 횟수  ?&quot;); scanf(&quot;%d&quot;,&count); return(0); } print_asterisk( )  { int i, j; for (i = 1; i <= count; i ++) { for (j = 1; j <= i; j ++) printf(&quot;*&quot;); printf(&quot;&quot;); } return(0); } 사용자 함수 반복 횟수 입력 “  * ”  출력
int print_asterisk( int cnt ); main( ) { int count ; printf(&quot; 반복 횟수  ?&quot;); scanf(&quot;%d&quot;,&count); print_asterisk( count ); } print_asterisk( int count ) { int i, j; for (i = 1; i <= count; i ++) { for (j = 1; j <= i; j ++) printf(&quot;*&quot;); printf(&quot;&quot;); } return(0); } int count; int print_asterisk( ); main( ) { printf(&quot; 반복 횟수  ?&quot;); scanf(&quot;%d&quot;,&count); print_asterisk( ); } print_asterisk( ) { int i, j; for (i = 1; i <= count; i ++) { for (j = 1; j <= i; j ++) printf(&quot;*&quot;); printf(&quot;&quot;); } return(0); } Call By Value 사용자 함수
[object Object],[object Object],[object Object],void main ( ) { printf (“1 + 2 = %d”, plus(1,2)); } int plus (int a, int b) { return a+b ; } void main ( ) { int  result; plus (1, 2, &result); printf (“1 + 2 = %d”, result); } void plus (int a, int b, int *r) { *r = a+b ; } ,[object Object],[object Object],[object Object],[object Object],[object Object],사용자 함수
[object Object],int swap (int x, int y); main( ) { int num1, num2; printf(&quot; 첫번째 수 : &quot;); scanf(&quot;%d&quot;,&num1); printf(&quot; 두번째 수 : &quot;); scanf(&quot;%d&quot;,&num2); swap(num1, num2); printf(“ 복귀 결과 : %d  %d&quot;, num1, num2); } int swap (int a, int b) { int temp; temp = a; a = b; b = temp; printf(“ 교환 결과 :  %d  %d&quot;, a, b); return(0); } int swap (int *, int *); main( ) {   int num1, num2; printf(&quot; 첫번째 수 : &quot;); scanf(&quot;%d&quot;,&num1); printf(&quot; 두번째 수 : &quot;); scanf(&quot;%d&quot;,&num2); swap(&num1, &num2); printf(&quot;%d %d&quot;, num1, num2); } int swap (int *a, int *b) {   int temp;   temp = *a;   *a = *b;   *b = temp; printf(&quot;%d %d&quot;, *a, *b); return(0); }
1 에서  10 까지의 합 : Call by Value 와  Address  혼용 main( ) {   int count = 10, result; hap ( count ,  &result ); printf(“ %d&quot;, result);   } 1 부터  count 까지의 합을 구하여  result 에 저장 사용자 함수 : practice (1)
3 개의 수 가운데 최대값과 최소값을 찾는 함수 :  Call by Value 와  Address  혼용 main( ) {   int num1 = 10, num2 = 20, num3 = 30; int mx, mn; max_min (num1, num2, num3,  &mx, &mn ); printf(“max = %d, min = %d&quot;, mx, mn); } 사용자 함수 : practice (2)
[object Object],void count (void); int number = 0; void main (void) { char ch; while ((ch = getchar( )) != EOF) { } printf (&quot;count=%d&quot;, number); } void count (void) { } 사용자 함수 : practice (3)
Pointer ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
int a[ ] = { 1, 2, 3 }; int *pa = a; if (pa == &a[0]) printf (“same”); else printf (“different”); printf (“%d”, *a); Pointer: Pointer & Array … .. 1000 … .. 1 2 3 pa a[0]  a[1]  a[2] 1000  1004  1008 variable value address sizeof (int) = + 4
[object Object],int a[3], i; for (i=0; i<3; i++) scanf (“%d”,  a+i ); for (i=0; i<3; i++) printf (“%d”,  *(a+i) ); a[0] a[1] a[2] a  a+1  a+2  Q. *a+1 vs. *(a+1) Pointer:  산술연산
Pointer: char* 형  String ,[object Object],[object Object],[object Object],[object Object],char *str; int i = 0; str = &quot;test&quot;; while (str[i] != NULL) { printf(&quot;%d&quot;,&str[i]); printf(&quot;%c&quot;,str[i]); i ++; }
[object Object],[object Object],char a[2][9] = {“computer”, “hello”}; char *b[2] = {“computer”, “hello”}; int  i; for (i=0; i<2; i++) printf (“%s”, b[i]); ‘ c’ ‘ o’ ‘ m’ ‘ p’ ‘ u’ ‘ t’ ‘ e’ ‘ r’ ‘ ’ ‘ h’ ‘ e’ ‘ l’ ‘ l’ ‘ o’ ‘ ’ a[0] a[1] ‘ c’ ‘ o’ ‘ m’ ‘ p’ ‘ u’ ‘ t’ ‘ e’ ‘ r’ ‘ ’ ‘ h’ ‘ e’ ‘ l’ ‘ l’ ‘ o’ ‘ ’ 1000 1100 1000 1100 b[0] b[1] Q. 2 차원 배열의  CBR Pointer: char* 형 배열
[object Object],[object Object],[object Object],[object Object],int array[3] = {1,2,3}; func(array,3); func ( int a[ ] , int n) func ( int *a , int n) Pointer: pointer & array
[object Object],[object Object],int array[3][2] = {{1,2},{3,4},{5,6}}; func(array); func ( int a[ ][2] ) func ( int (*a)[2] ) Pointer: pointer & array
Pointer: array & module - #1 ,[object Object],int a[5] = { 1, 2, 3, 4, 5 }; mod (a[3]); printf(“%d”,a[3]); void mod (int num) { num ++; printf(“%d”,num); }
Pointer: array & module - #2 ,[object Object],int a[5] = { 1, 2, 3, 4, 5 }; mod (&a[3]); printf(“%d”,a[3]); void mod (int *num) { (*num) ++; printf(“%d”,*num); }
Pointer: array & module - #3 ,[object Object],int a[5] = { 1, 2, 3, 4, 5 }; mod (a); for (i=0; i<5; i++)  printf(“%d”,a[i]); void mod (int num[ ]) { for (i=0; i<5; i++) { printf(“%d”,num[i]); num[i] ++; } } a = &a[0] num[ ] = *num
[object Object],[object Object],[object Object],void my_toupper (char *str); void main(void) { char buf[20] = {0}; printf(&quot; 소문자 문자열 :&quot;); scanf(&quot;%s&quot;, buf); my_toupper (buf); printf(&quot; 결과 =%s&quot;, buf); } 사용자 함수 : practice (4)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],사용자 함수 : practice (scoring #4)
int  *score ; int i, num, sum;   printf (&quot; 학생수를 입력하세요  : &quot;); scanf (&quot;%d&quot;, &num); score  = (int *)  malloc  (num * sizeof(int)); if (score == NULL) { printf(&quot; 메모리가 부족합니다 .&quot;); exit(0); }   for (i=0; i<num; i++) { printf(&quot;%d 번 학생의 성적을 입력하세요  : &quot;, i+1); scanf(&quot;%d&quot;,  &score[i] );  //  score + i }   sum = 0; for (i=0; i<num; i++) sum +=  score[i] ;  //  *(score + i) printf(&quot; 총점은  %d 점이고 평균은  %d 점입니다 .&quot;, sum, sum / num); free  ( score ); ,[object Object],[object Object],[object Object],Dynamic Memory Allocation
[object Object],Record Structure int  age[10]; char name[10][20]; char address[10][100]; char phone[10][16]; struct   people  { int  age; char name[20]; char address[100]; char phone[16]; }; struct   people  addr_book[10] ; age name address phone age name address phone age name address phone age name address phone age age name name address address phone phone
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Structure: practice (1)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Structure: practice (2)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Structure 의  CBV & CBA (1)
int process (struct student); typedef struct student { int  num; char name[10]; int  kor, eng, mat, sum; float ave; }who; int count; void main( ) { struct student who; printf(&quot; 학생수를 입력하시오  : &quot;); scanf(&quot;%d&quot;,&count); printf(&quot; 번호  성 명  국어 영어 수학  총점  평균 &quot;); process(who); } process (struct student who2) { int no; for (no = 1; no <= count; no ++) {   scanf(&quot;%d%s%d%d%d&quot;,&who2.num,who2.name,&who2.kor,&who2.eng,&who2.mat);   who2.sum = who2.kor + who2.eng + who2.mat;   who2.ave = who2.sum / 3.0; printf(&quot;%2d %s %3d %3d %3d  %3d %4.1f&quot;,who2.num,who2.name,who2.kor,who2.eng,who2.mat,who2.sum,who2.ave); } return 0; }
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Structure 의  CBV & CBA (2)
int process (struct student*); typedef struct student { int  num; char name[10]; int  kor, eng, mat, sum; float ave; }who; int count; void main( ) { struct student who; printf(&quot; 학생수를 입력하시오  : &quot;); scanf(&quot;%d&quot;,&count); printf(&quot; 번호  성 명  국어 영어 수학  총점  평균 &quot;); process(&who); } process (struct student *who) { int no; for (no = 1; no <= count; no ++) {   scanf(&quot;%d%s%d%d%d&quot;,&who->num,who->name,&who->kor,&who->eng,&who->mat);   who->sum = who->kor + who->eng + who->mat;   who->ave = who->sum / 3.0;   printf(&quot;%2d %s %3d %3d %3d  %3d %4.1f&quot;,who->num,who->name,who->kor,who->eng,who->mat,who->sum,who->ave); } return 0; }
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Structure 의  CBV & CBA (3)
struct student { int  num; char name[10]; int  kor, eng, mat, sum; float ave; }; void input_score( struct student[ ] ), print_score( struct student[ ] ); int  class_sum, n; main() { struct student who[2] ; input_score(who); print_score(who); } void input_score( struct student who[ ] ) { printf(&quot; 번호 성 명 국어 영어 수학  총점  평균 &quot;); for (n=0; n<2; n++) { scanf(&quot;%3d%s%4d%4d%4d&quot;,&who[n].num, who[n].name, &who[n].kor, &who[n].eng, &who[n].mat); who[n].sum = who[n].kor + who[n].eng + who[n].mat; who[n].ave = who[n].sum / 3.0; class_sum = class_sum + who[n].sum; } } void print_score( struct student who[ ] ) { printf(&quot; 번호  성  명  국어 영어 수학  총점  평균 &quot;); for (n=0; n<2; n++)  printf(&quot;%3d  %s%4d %4d %4d  %4d  %5.1f&quot;,who[n].num,who[n].name,   who[n].kor,who[n].eng,who[n].mat,who[n].sum,who[n].ave); }
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Structure 의  CBV & CBA (4)
struct student { int  num; char name[10]; int  kor, eng, mat, sum; float ave; }; void input_score( struct student * ), print_score( struct student * ); int  class_sum, n; main() { struct student who[2] ; input_score(who); print_score(who); } void input_score( struct student *who ) { printf(&quot; 번호 성 명 국어 영어 수학  총점  평균 &quot;); for (n=0; n<2; n++) { scanf(&quot;%3d%s%4d%4d%4d&quot;, &(who+n)-> num, (who+n)->name, &(who+n)->kor, &(who+n)->eng, &(who+n)->mat); (who+n)->sum = (who+n)->kor + (who+n)->eng + (who+n)->mat; (who+n)->ave = (who+n)->sum / 3.0; class_sum = class_sum + (who+n)->sum; } } void print_score( struct student *who ) { printf(&quot; 번호  성  명  국어 영어 수학  총점  평균 &quot;); for (n=0; n<2; n++)  printf(&quot;%3d  %s%4d %4d %4d  %4d  %5.1f&quot;,(who+n)->num,(who+n)->name, (who+n)->kor,(who+n)->eng,(who+n)->mat,(who+n)->sum,(who+n)->ave); }
[object Object],[object Object],[object Object],[object Object],Structure: practice (3)
Structure: practice (4) ,[object Object],Advanced
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Structure: practice (5)
fopen fclose File I/O Memory variable array structure Hard disk data.txt fseek ftell printf putchar fscanf fread getc fgets fwrite fprintf putc fputs scanf getchar gets
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],File I/O: File read
main( ) { int buf = 0; FILE *fp; fp = fopen (&quot;fread.txt&quot;, &quot;r&quot;); fread (&buf, sizeof(int), 1, fp); printf (&quot;%d&quot;, buf); fclose (fp); } ,[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],File I/O: File write
[object Object],main( ) { int buf = 2929; FILE *fp; fp = fopen (&quot;fread.txt&quot;, &quot;w&quot;); fwrite (&buf, sizeof(int), 1, fp); fclose (fp); fp = fopen (&quot;fread.txt&quot;, &quot;r&quot;); fread (&buf, sizeof(int), 1, fp); printf (&quot;%d&quot;, buf); fclose (fp); }
main( ) { FILE *fp; int w_array[10], r_array[10], n; fp = fopen (&quot;fwrite.txt&quot;,&quot;w&quot;); for (n=0; n<10; n++) w_array[n] = n; fwrite (w_array, sizeof(w_array), 1, fp); fclose (fp); fp = fopen (&quot;fwrite.txt&quot;,&quot;r&quot;); fread (r_array, sizeof(r_array), 1, fp); for (n=0; n<10; n++) printf (&quot;%d &quot;, r_array[n]); fclose (fp); } ,[object Object],File I/O: practice (1)
#define COUNT 3 struct student { int  num; char name[10]; int  kor, eng, mat; } st[COUNT]; int main()  { FILE *fp; int n; for (n=0; n<COUNT; n++) { printf(&quot;%d  번째 번호 = &quot;,n+1); scanf(&quot;%d&quot;,&st[n].num); printf(&quot;%d  번째 이름 = &quot;,n+1); scanf(&quot;%s&quot;,st[n].name); printf(&quot;%d  번째 국어점수 = &quot;,n+1); scanf(&quot;%d&quot;,&st[n].kor); printf(&quot;%d  번째 영어점수 = &quot;,n+1); scanf(&quot;%d&quot;,&st[n].eng); printf(&quot;%d  번째 수학점수 = &quot;,n+1); scanf(&quot;%d&quot;,&st[n].mat); } fp  = fopen(&quot;score.txt&quot;,&quot;w&quot;); fwrite(st, sizeof(struct student), COUNT, fp); fclose(fp); fp  = fopen(&quot;score.txt&quot;,&quot;r&quot;); fread(st, sizeof(struct student), COUNT, fp); for (n=0; n<COUNT; n++) printf(&quot; 번호 : %d  이름 : %s  %3d %3d %3d&quot;, st[n].num, st[n].name,  st[n].kor, st[n].eng, st[n].mat); fclose(fp); } Q1. Size of student ? Q2. n 개의  struct 의  CBV ? File I/O: practice (2)
struct student { int  num; char name[10]; int  kor, eng, mat; } st; input_data (struct student[ ]); int n; int main( )  { FILE *fp; struct student buf[COUNT]; input_data (buf); fp  = fopen (&quot;score.txt&quot;,&quot;w&quot;); fwrite (buf, sizeof(struct student), COUNT, fp); fclose (fp); fp  = fopen (&quot;score.txt&quot;,&quot;r&quot;); fread (buf, sizeof(struct student), COUNT, fp); for (n=0; n<COUNT; n++) printf (&quot; 번호 : %d  이름 : %s  %3d %3d %3d&quot;, buf[n].num, buf[n].name,  buf[n].kor, buf[n].eng, buf[n].mat); fclose (fp); } input_data (struct student buf[ ]) { for (n=0; n<COUNT; n++) { printf (&quot;%d  번째 번호 = &quot;,n+1);  scanf (&quot;%d&quot;,&buf[n].num); printf (&quot;%d  번째 이름 = &quot;,n+1);  scanf (&quot;%s&quot;,buf[n].name); printf (&quot;%d  번째 국어점수 = &quot;,n+1); scanf (&quot;%d&quot;,&buf[n].kor); printf (&quot;%d  번째 영어점수 = &quot;,n+1); scanf (&quot;%d&quot;,&buf[n].eng); printf (&quot;%d  번째 수학점수 = &quot;,n+1); scanf (&quot;%d&quot;,&buf[n].mat); } }
main( ) { FILE *fp; char buf; fp = fopen (&quot;sample.txt&quot;, &quot; a &quot;); while (buf != '') { buf =  getchar (); putc  (buf, fp); } fclose (fp); } ,[object Object],[object Object],main( ) { FILE *fp; char buf; fp = fopen (&quot;sample.txt&quot;, “ r &quot;); while (buf != EOF) { buf =  getc (fp); putchar  (buf); } fclose (fp); } File I/O: practice (3)
[object Object],[object Object],source file buffer destination file putc  (buffer, *fp); buffer =  getc  (*fp);
main( ) { FILE *in, *out; char buf; in = fopen (&quot;test.txt&quot;, &quot;r&quot;); out = fopen (&quot;test_copy.txt&quot;, “w&quot;); while (buf != EOF) {   buf =  getc  (in); putc  (buf,out); } fcloseall (); } ,[object Object]
[object Object],[object Object],source file destination file fputs  (char *buffer, *fp); fgets  (char *buffer, int n, *fp); … buffer[n] n
main( ) { FILE *in, *out; char buf[100]; in  = fopen (“test.txt&quot;, &quot;r&quot;); out = fopen (“test_copy.txt&quot;, &quot;w&quot;); while ( fgets(buf,sizeof(buf),in)  != NULL) fputs (buf, out) ; fcloseall ( ); } ,[object Object]
[object Object],[object Object],main( ) { FILE *in, *out; char buf; int  line=1, count=0; in = fopen (&quot;test.cpp&quot;, &quot;r&quot;); out = fopen (&quot;test_line.cpp&quot;, &quot;w&quot;); while (buf != EOF) { if (count == 0) { fprintf  (out,&quot;%03d &quot;,line); count ++; }   buf = getc(in); putc(buf,out); if (buf == '') { line ++; fprintf  (out,&quot;%03d &quot;,line); } } fcloseall (); }
#define COUNT 3 struct student { int  num; char name[10]; int kor, eng, mat, total; float ave; } st[COUNT]; main( ) { FILE *in, *out; int i; in = fopen (&quot;score_input. txt&quot;, &quot;r&quot;); out = fopen (&quot;score_output.txt&quot;, &quot;w&quot;); for (i=0; i<COUNT; i++) { fscanf  (in, &quot;%d %s %d %d %d&quot;, &st[i].num, st[i].name, &st[i].kor, &st[i].eng, &st[i].mat); st[i].total = st[i].kor + st[i].eng + st[i].mat; st[i].ave = st[i].total / 3.0; } for (i=0; i<COUNT; i++)  fprintf  (out, &quot;%d %s %d %d %d %d %4.1f&quot;,  st[i].num, st[i].name, st[i].kor, st[i].eng, st[i].mat, st[i].total, st[i].ave); fclose (in); fclose (out); } fscanf fprintf File I/O: practice (4)
#define COUNT 3 struct student { int  num; char name[10]; int kor, eng, mat, total; float ave; int rank; } st[COUNT]; main( ) { FILE *in, *out; int i; in = fopen (&quot;score_input.txt&quot;, &quot;r&quot;); out = fopen (&quot;score_output.txt&quot;, &quot;w&quot;); for (i=0; i<COUNT; i++) { fscanf  (in, &quot;%d %s %d %d %d&quot;, &st[i].num, st[i].name, &st[i].kor, &st[i].eng, &st[i].mat); st[i].total = st[i].kor + st[i].eng + st[i].mat; st[i].ave = st[i].total / 3.0; } for (i=0; i<COUNT; i++)  fprintf  (out, &quot;%d %s %d %d %d %d %4.1f  %d &quot;,  st[i].num, st[i].name, st[i].kor, st[i].eng, st[i].mat, st[i].total, st[i].ave,  st[i].rank ); fclose (in); fclose (out); } ,[object Object],for (i=0; i<COUNT; i++) st[i].rank = 1; for (i=0; i<COUNT-1; i++) { j = 0; for (j=j+1; j<COUNT; j++) { if (st[i].ave > st[j].ave)  st[j].rank += 1; else if (st[i].ave < st[j].ave) st[i].rank += 1; } } ascending sort
#define COUNT 2 struct student { int  num; char name[8]; int kor, eng, mat, total; float ave; } st[COUNT]; int  num, kor, eng, mat, total; char  name[8]; float ave; main( ) { FILE *in, *out; int i; unsigned rsize; long rec; in  = fopen (&quot;score_input.txt&quot;, &quot;r&quot;); out = fopen (&quot;score_output.txt&quot;, &quot;w+&quot;); rsize = sizeof(struct student); for (i=0; i<COUNT; i++) { fscanf (in, &quot;%d %s %d %d %d&quot;,&st[i].num, st[i].name, &st[i].kor, &st[i].eng, &st[i].mat); st[i].total = st[i].kor + st[i].eng + st[i].mat; st[i].ave = st[i].total / 3.0; fprintf(out, &quot;%d %s %d %d %d %d %f&quot;,st[i].num, st[i].name, st[i].kor, st[i].eng, st[i].mat, st[i].total, st[i].ave); } printf(&quot; 검색 레코드 번호 . [ 종료는  ctrl+x]: &quot;); while (scanf(&quot;%ld&quot;, &rec) != 0) { fseek(out, rsize*(rec-1L), SEEK_SET); fscanf (out, &quot;%d %s %d %d %d %d %f&quot;,&num, name, &kor, &eng, &mat, &total, &ave); printf(&quot; 검색 레코드 : %d %s %d %d %d %d %f&quot;, num, name, kor, eng, mat, total, ave);  } fcloseall(); } ,[object Object],search: “fseek”

More Related Content

What's hot

2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1Chris Ohk
 
파이썬 스터디 2주차
파이썬 스터디 2주차파이썬 스터디 2주차
파이썬 스터디 2주차Han Sung Kim
 
배열과 포인터
배열과 포인터배열과 포인터
배열과 포인터영기 김
 
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3Chris Ohk
 
(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3guestc0587d1
 
Haskell study 10
Haskell study 10Haskell study 10
Haskell study 10Nam Hyeonuk
 
Template at c++
Template at c++Template at c++
Template at c++Lusain Kim
 
C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2Chris Ohk
 
C++’s move semantics
C++’s move semanticsC++’s move semantics
C++’s move semanticsLusain Kim
 
Lua 문법 -함수
Lua 문법 -함수Lua 문법 -함수
Lua 문법 -함수Jaehoon Lee
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++KWANGIL KIM
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉HyunJoon Park
 
자바 스터디(6기) 1
자바 스터디(6기) 1자바 스터디(6기) 1
자바 스터디(6기) 1Jina Lee
 
[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)NAVER D2
 

What's hot (20)

2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1
 
파이썬 스터디 2주차
파이썬 스터디 2주차파이썬 스터디 2주차
파이썬 스터디 2주차
 
배열과 포인터
배열과 포인터배열과 포인터
배열과 포인터
 
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
 
What’s new in c++11
What’s new in c++11What’s new in c++11
What’s new in c++11
 
6 function
6 function6 function
6 function
 
(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3
 
Haskell study 10
Haskell study 10Haskell study 10
Haskell study 10
 
Template at c++
Template at c++Template at c++
Template at c++
 
C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2
 
C++’s move semantics
C++’s move semanticsC++’s move semantics
C++’s move semantics
 
Lua 문법 -함수
Lua 문법 -함수Lua 문법 -함수
Lua 문법 -함수
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++
 
C++11
C++11C++11
C++11
 
Lua 문법
Lua 문법Lua 문법
Lua 문법
 
강의자료 2
강의자료 2강의자료 2
강의자료 2
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉
 
자바 스터디(6기) 1
자바 스터디(6기) 1자바 스터디(6기) 1
자바 스터디(6기) 1
 
[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)
 

Similar to C수업자료

게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차Yeonah Ki
 
프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2dktm
 
파이썬 기본 문법
파이썬 기본 문법파이썬 기본 문법
파이썬 기본 문법SeongHyun Ahn
 
2012 Ds B1 01
2012 Ds B1 012012 Ds B1 01
2012 Ds B1 01seonhyung
 
Python3 brief summary
Python3 brief summaryPython3 brief summary
Python3 brief summaryHoChul Shin
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
2015 Kitel C 언어 강좌3
2015 Kitel C 언어 강좌32015 Kitel C 언어 강좌3
2015 Kitel C 언어 강좌3ssuseraf62e91
 
자바스크립트 기초문법~함수기초
자바스크립트 기초문법~함수기초자바스크립트 기초문법~함수기초
자바스크립트 기초문법~함수기초진수 정
 
게임프로그래밍입문 5주차
게임프로그래밍입문 5주차게임프로그래밍입문 5주차
게임프로그래밍입문 5주차Yeonah Ki
 
게임프로그래밍입문 4주차
게임프로그래밍입문 4주차게임프로그래밍입문 4주차
게임프로그래밍입문 4주차Yeonah Ki
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdfjinwookhong
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdfkd19h
 
You can read go code
You can read go codeYou can read go code
You can read go codeHomin Lee
 
Code로 이해하는 RNN
Code로 이해하는 RNNCode로 이해하는 RNN
Code로 이해하는 RNNSANG WON PARK
 

Similar to C수업자료 (20)

게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차
 
06장 함수
06장 함수06장 함수
06장 함수
 
프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2
 
튜터링 #9 20120409
튜터링 #9 20120409튜터링 #9 20120409
튜터링 #9 20120409
 
파이썬 기본 문법
파이썬 기본 문법파이썬 기본 문법
파이썬 기본 문법
 
2012 Ds B1 01
2012 Ds B1 012012 Ds B1 01
2012 Ds B1 01
 
Python3 brief summary
Python3 brief summaryPython3 brief summary
Python3 brief summary
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
3주차 스터디
3주차 스터디3주차 스터디
3주차 스터디
 
2015 Kitel C 언어 강좌3
2015 Kitel C 언어 강좌32015 Kitel C 언어 강좌3
2015 Kitel C 언어 강좌3
 
자바스크립트 기초문법~함수기초
자바스크립트 기초문법~함수기초자바스크립트 기초문법~함수기초
자바스크립트 기초문법~함수기초
 
java_2장.pptx
java_2장.pptxjava_2장.pptx
java_2장.pptx
 
게임프로그래밍입문 5주차
게임프로그래밍입문 5주차게임프로그래밍입문 5주차
게임프로그래밍입문 5주차
 
게임프로그래밍입문 4주차
게임프로그래밍입문 4주차게임프로그래밍입문 4주차
게임프로그래밍입문 4주차
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf
 
이산치2번
이산치2번이산치2번
이산치2번
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf
 
You can read go code
You can read go codeYou can read go code
You can read go code
 
Code로 이해하는 RNN
Code로 이해하는 RNNCode로 이해하는 RNN
Code로 이해하는 RNN
 
2012 Ds 01
2012 Ds 012012 Ds 01
2012 Ds 01
 

C수업자료

  • 1. C Programming [email_address]
  • 2.
  • 3.
  • 4.
  • 5.
  • 6. Constant 1 Byte * n 1 Byte 4 Byte 4 Byte ASCII * n 문자열 string ASCII 문자 char acter 문자상수 1s + 지수 + 가수 = -3.4E- 38 ~ 3.4E 38 실수 float ing number 1s + 2 31 = -21 억 ~ +21 억 정수 int eger 숫자상수
  • 7.
  • 8. Address int a; char b; a = 10; b = ‘a’; Memory(RAM) … 100 101 102 103 104 105 106 107 … a: 4 Byte b: 1 Byte 10 10 (1010 2 ) ‘ a’ (97 10 )
  • 9. Standard I/O cf. gets vs scanf(“%s” …) All printf String puts One char putchar Output All scanf String gets One char getchar Input functions
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19. 1 2 3 4
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41. main( ) { int row, col; for (row = 1; row < 10; row ++) { printf (&quot;&quot;); for (col = 1; col < 10; col ++) printf (&quot;%2dx%2d=%2d&quot;, row, col, row*col); } } 제어문 ( 반복문 ): 이중 Loop
  • 42. main( ) { int count, i, j; printf (&quot;Repeat count ?&quot;); scanf (&quot;%d&quot;, &count); for (i = 1; i <= count; i ++) { for (j = i ; j <= count ; j ++) printf (&quot;*&quot;); printf (&quot;&quot;); } } 이중 Loop: practice (1)
  • 43. main( ) { int count, i, j; printf (&quot;Repeat count ?&quot;); scanf (&quot;%d&quot;, &count); for (i = 1; i <= count; i ++) { for (j = 1 ; j <= i ; j ++) printf (&quot;*&quot;); printf (&quot;&quot;); } } 이중 Loop: practice (2)
  • 44. main( ) { int n, i, j, cnt=0, cnt2=0; printf (“Repeat count = &quot;); scanf (&quot;%d&quot;, &n); for (i = 0; i < n; i += 2) { for (j = 0; j <= n/2-cnt-1; j ++) printf (&quot; &quot;); for (j = 0; j < i+1; j ++) printf (&quot;*&quot;); cnt ++; printf (&quot;&quot;); } cnt2 = 1; for (i = n-1; i > 1; i -= 2) { for (j = 0; j < cnt2; j ++) printf (&quot; &quot;); for (j = 0; j < i-1; j ++) printf (&quot;*&quot;); cnt2 ++; printf (&quot;&quot;); } } Advanced 이중 Loop: practice (3)
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57. Array: practice (4) void main( ) { int buf[10][10] = {0}; int i, j, n, ncnt, xcnt, ycnt, x, y; ncnt = 1; n= 5; x = y = xcnt = ycnt = 0; for (i=0; i<=n*n; i++) { buf[x--][y++] = ncnt++; if (x<0 && y<n) { xcnt ++; x = xcnt; y = ycnt; } if (y>=n) { ycnt ++; x = xcnt; y = ycnt; } } for (i=0; i<n; i++) { for (j=0; j<n; j++) printf(&quot;%3d&quot;, buf[i][j]); printf(&quot;&quot;); } } Advanced
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74. main( ) { int count, i, j; printf(&quot; 반복 횟수 ?&quot;); scanf(&quot;%d&quot;,&count); for (i = 1; i <= count; i ++) { for (j = 1; j <= i; j ++) printf(&quot;*&quot;); printf(&quot;&quot;); } } int count; int input_count( ), print_asterisk( ); main( ) { input_count(); print_asterisk(); } input_count( ) { printf(&quot; 반복 횟수 ?&quot;); scanf(&quot;%d&quot;,&count); return(0); } print_asterisk( ) { int i, j; for (i = 1; i <= count; i ++) { for (j = 1; j <= i; j ++) printf(&quot;*&quot;); printf(&quot;&quot;); } return(0); } 사용자 함수 반복 횟수 입력 “ * ” 출력
  • 75. int print_asterisk( int cnt ); main( ) { int count ; printf(&quot; 반복 횟수 ?&quot;); scanf(&quot;%d&quot;,&count); print_asterisk( count ); } print_asterisk( int count ) { int i, j; for (i = 1; i <= count; i ++) { for (j = 1; j <= i; j ++) printf(&quot;*&quot;); printf(&quot;&quot;); } return(0); } int count; int print_asterisk( ); main( ) { printf(&quot; 반복 횟수 ?&quot;); scanf(&quot;%d&quot;,&count); print_asterisk( ); } print_asterisk( ) { int i, j; for (i = 1; i <= count; i ++) { for (j = 1; j <= i; j ++) printf(&quot;*&quot;); printf(&quot;&quot;); } return(0); } Call By Value 사용자 함수
  • 76.
  • 77.
  • 78. 1 에서 10 까지의 합 : Call by Value 와 Address 혼용 main( ) { int count = 10, result; hap ( count , &result ); printf(“ %d&quot;, result); } 1 부터 count 까지의 합을 구하여 result 에 저장 사용자 함수 : practice (1)
  • 79. 3 개의 수 가운데 최대값과 최소값을 찾는 함수 : Call by Value 와 Address 혼용 main( ) { int num1 = 10, num2 = 20, num3 = 30; int mx, mn; max_min (num1, num2, num3, &mx, &mn ); printf(“max = %d, min = %d&quot;, mx, mn); } 사용자 함수 : practice (2)
  • 80.
  • 81.
  • 82. int a[ ] = { 1, 2, 3 }; int *pa = a; if (pa == &a[0]) printf (“same”); else printf (“different”); printf (“%d”, *a); Pointer: Pointer & Array … .. 1000 … .. 1 2 3 pa a[0] a[1] a[2] 1000 1004 1008 variable value address sizeof (int) = + 4
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98. int process (struct student); typedef struct student { int num; char name[10]; int kor, eng, mat, sum; float ave; }who; int count; void main( ) { struct student who; printf(&quot; 학생수를 입력하시오 : &quot;); scanf(&quot;%d&quot;,&count); printf(&quot; 번호 성 명 국어 영어 수학 총점 평균 &quot;); process(who); } process (struct student who2) { int no; for (no = 1; no <= count; no ++) { scanf(&quot;%d%s%d%d%d&quot;,&who2.num,who2.name,&who2.kor,&who2.eng,&who2.mat); who2.sum = who2.kor + who2.eng + who2.mat; who2.ave = who2.sum / 3.0; printf(&quot;%2d %s %3d %3d %3d %3d %4.1f&quot;,who2.num,who2.name,who2.kor,who2.eng,who2.mat,who2.sum,who2.ave); } return 0; }
  • 99.
  • 100. int process (struct student*); typedef struct student { int num; char name[10]; int kor, eng, mat, sum; float ave; }who; int count; void main( ) { struct student who; printf(&quot; 학생수를 입력하시오 : &quot;); scanf(&quot;%d&quot;,&count); printf(&quot; 번호 성 명 국어 영어 수학 총점 평균 &quot;); process(&who); } process (struct student *who) { int no; for (no = 1; no <= count; no ++) { scanf(&quot;%d%s%d%d%d&quot;,&who->num,who->name,&who->kor,&who->eng,&who->mat); who->sum = who->kor + who->eng + who->mat; who->ave = who->sum / 3.0; printf(&quot;%2d %s %3d %3d %3d %3d %4.1f&quot;,who->num,who->name,who->kor,who->eng,who->mat,who->sum,who->ave); } return 0; }
  • 101.
  • 102. struct student { int num; char name[10]; int kor, eng, mat, sum; float ave; }; void input_score( struct student[ ] ), print_score( struct student[ ] ); int class_sum, n; main() { struct student who[2] ; input_score(who); print_score(who); } void input_score( struct student who[ ] ) { printf(&quot; 번호 성 명 국어 영어 수학 총점 평균 &quot;); for (n=0; n<2; n++) { scanf(&quot;%3d%s%4d%4d%4d&quot;,&who[n].num, who[n].name, &who[n].kor, &who[n].eng, &who[n].mat); who[n].sum = who[n].kor + who[n].eng + who[n].mat; who[n].ave = who[n].sum / 3.0; class_sum = class_sum + who[n].sum; } } void print_score( struct student who[ ] ) { printf(&quot; 번호 성 명 국어 영어 수학 총점 평균 &quot;); for (n=0; n<2; n++) printf(&quot;%3d %s%4d %4d %4d %4d %5.1f&quot;,who[n].num,who[n].name, who[n].kor,who[n].eng,who[n].mat,who[n].sum,who[n].ave); }
  • 103.
  • 104. struct student { int num; char name[10]; int kor, eng, mat, sum; float ave; }; void input_score( struct student * ), print_score( struct student * ); int class_sum, n; main() { struct student who[2] ; input_score(who); print_score(who); } void input_score( struct student *who ) { printf(&quot; 번호 성 명 국어 영어 수학 총점 평균 &quot;); for (n=0; n<2; n++) { scanf(&quot;%3d%s%4d%4d%4d&quot;, &(who+n)-> num, (who+n)->name, &(who+n)->kor, &(who+n)->eng, &(who+n)->mat); (who+n)->sum = (who+n)->kor + (who+n)->eng + (who+n)->mat; (who+n)->ave = (who+n)->sum / 3.0; class_sum = class_sum + (who+n)->sum; } } void print_score( struct student *who ) { printf(&quot; 번호 성 명 국어 영어 수학 총점 평균 &quot;); for (n=0; n<2; n++) printf(&quot;%3d %s%4d %4d %4d %4d %5.1f&quot;,(who+n)->num,(who+n)->name, (who+n)->kor,(who+n)->eng,(who+n)->mat,(who+n)->sum,(who+n)->ave); }
  • 105.
  • 106.
  • 107.
  • 108. fopen fclose File I/O Memory variable array structure Hard disk data.txt fseek ftell printf putchar fscanf fread getc fgets fwrite fprintf putc fputs scanf getchar gets
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114. #define COUNT 3 struct student { int num; char name[10]; int kor, eng, mat; } st[COUNT]; int main() { FILE *fp; int n; for (n=0; n<COUNT; n++) { printf(&quot;%d 번째 번호 = &quot;,n+1); scanf(&quot;%d&quot;,&st[n].num); printf(&quot;%d 번째 이름 = &quot;,n+1); scanf(&quot;%s&quot;,st[n].name); printf(&quot;%d 번째 국어점수 = &quot;,n+1); scanf(&quot;%d&quot;,&st[n].kor); printf(&quot;%d 번째 영어점수 = &quot;,n+1); scanf(&quot;%d&quot;,&st[n].eng); printf(&quot;%d 번째 수학점수 = &quot;,n+1); scanf(&quot;%d&quot;,&st[n].mat); } fp = fopen(&quot;score.txt&quot;,&quot;w&quot;); fwrite(st, sizeof(struct student), COUNT, fp); fclose(fp); fp = fopen(&quot;score.txt&quot;,&quot;r&quot;); fread(st, sizeof(struct student), COUNT, fp); for (n=0; n<COUNT; n++) printf(&quot; 번호 : %d 이름 : %s %3d %3d %3d&quot;, st[n].num, st[n].name, st[n].kor, st[n].eng, st[n].mat); fclose(fp); } Q1. Size of student ? Q2. n 개의 struct 의 CBV ? File I/O: practice (2)
  • 115. struct student { int num; char name[10]; int kor, eng, mat; } st; input_data (struct student[ ]); int n; int main( ) { FILE *fp; struct student buf[COUNT]; input_data (buf); fp = fopen (&quot;score.txt&quot;,&quot;w&quot;); fwrite (buf, sizeof(struct student), COUNT, fp); fclose (fp); fp = fopen (&quot;score.txt&quot;,&quot;r&quot;); fread (buf, sizeof(struct student), COUNT, fp); for (n=0; n<COUNT; n++) printf (&quot; 번호 : %d 이름 : %s %3d %3d %3d&quot;, buf[n].num, buf[n].name, buf[n].kor, buf[n].eng, buf[n].mat); fclose (fp); } input_data (struct student buf[ ]) { for (n=0; n<COUNT; n++) { printf (&quot;%d 번째 번호 = &quot;,n+1); scanf (&quot;%d&quot;,&buf[n].num); printf (&quot;%d 번째 이름 = &quot;,n+1); scanf (&quot;%s&quot;,buf[n].name); printf (&quot;%d 번째 국어점수 = &quot;,n+1); scanf (&quot;%d&quot;,&buf[n].kor); printf (&quot;%d 번째 영어점수 = &quot;,n+1); scanf (&quot;%d&quot;,&buf[n].eng); printf (&quot;%d 번째 수학점수 = &quot;,n+1); scanf (&quot;%d&quot;,&buf[n].mat); } }
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122. #define COUNT 3 struct student { int num; char name[10]; int kor, eng, mat, total; float ave; } st[COUNT]; main( ) { FILE *in, *out; int i; in = fopen (&quot;score_input. txt&quot;, &quot;r&quot;); out = fopen (&quot;score_output.txt&quot;, &quot;w&quot;); for (i=0; i<COUNT; i++) { fscanf (in, &quot;%d %s %d %d %d&quot;, &st[i].num, st[i].name, &st[i].kor, &st[i].eng, &st[i].mat); st[i].total = st[i].kor + st[i].eng + st[i].mat; st[i].ave = st[i].total / 3.0; } for (i=0; i<COUNT; i++) fprintf (out, &quot;%d %s %d %d %d %d %4.1f&quot;, st[i].num, st[i].name, st[i].kor, st[i].eng, st[i].mat, st[i].total, st[i].ave); fclose (in); fclose (out); } fscanf fprintf File I/O: practice (4)
  • 123.
  • 124.