3 1 typedef typedef struct char name100 int

  • Slides: 40
Download presentation

3 제 1 절 부가기능 typedef의 확장 typedef struct { char name[100]; int id_num;

3 제 1 절 부가기능 typedef의 확장 typedef struct { char name[100]; int id_num; }EMP_REC; EMP_REC employee[74]; 는 struct { char name[100]; int id_num; } employee[75]; 와 같다.

5 제 1 절 부가기능 프로그램 12 -1 #include <stdio. h> void main(void) {

5 제 1 절 부가기능 프로그램 12 -1 #include <stdio. h> void main(void) { enum color red, green, yellow; enum color crayon = red; /* crayon을 color 형으로 선언하고 red값으로 초기화함 */ printf("f 2 n. The color is %d", crayon); printf("Enter in a value: "); scanf("%d", &crayon); if (crayon == red) printf("The crayon is red. "); else if (crayon == green) printf("The crayon is green. "); else if (crayon == yellow) printf("The crayon is yellow. "); else printf("The crayon is not defined. "); }

9 제 1 절 부가기능 • 조건 수식(conditional expression) – 형태 : expression 1

9 제 1 절 부가기능 • 조건 수식(conditional expression) – 형태 : expression 1 ? expression 2 : expression 3 – 수식 expression 1의 값이 0이 아니면, 즉 참(true)이면 수식 expression 2 의 값이 구해지고, 그렇지 않으면 수식 expression 3의 값이 구해진다. if (hours > 40) rate = 0. 045; else rate = 0. 02; rate = (hours > 40) ? 0. 045 : 0. 02;

10 제 1 절 부가기능 goto 문 프로그램의 실행 제어를 아무 조건 없이 다른

10 제 1 절 부가기능 goto 문 프로그램의 실행 제어를 아무 조건 없이 다른 곳으로 넘길 수 있다. if (denom == 0. 0) goto err; else result = num / denom; err: printf("Error - Attempted Division by Zero");

11 제 1 절 부가기능 goto 문의 부적절한 사용 예 if (a == 100)

11 제 1 절 부가기능 goto 문의 부적절한 사용 예 if (a == 100) goto first; else x = 20; goto sec; first: x= 50; sec: y= 10; if (a == 100) x = 50; else x = 20; y = 10;

14 제 2 절 비트별 연산자(bitwise operator) #include <stdio. h> void main(void) { int

14 제 2 절 비트별 연산자(bitwise operator) #include <stdio. h> void main(void) { int op 1 = 0325, op 2 = 0263; printf("%o ANDed with %o is %o", op 1, op 2, op 1&op 2); } • 실행 결과 => 325 ANDed with 263 is 221

17 제 2 절 비트별 연산자(bitwise operator) #include <stdio. h> void main(void) { int

17 제 2 절 비트별 연산자(bitwise operator) #include <stdio. h> void main(void) { int op 1 = 0325, op 2 = 0263; printf("%o ORed with %o is %o", op 1, op 2, op 1|op 2); } • 실행 결과 => 325 ORed with 263 is 367

30 제 3 절 매크로 • 매크로 사용의 주의 #define SQUARE(x) x * x

30 제 3 절 매크로 • 매크로 사용의 주의 #define SQUARE(x) x * x val = (num 1+num 2)*(num 1+num 2) val = SQUARE(num 1 + num 2); val = num 1 + num 2 * num 1 + num 2; #define SQUARE(x) * (x)

제 4 절 명령 행 인자(command line argument) 프로그램 12 -4 #include <stdio. h>

제 4 절 명령 행 인자(command line argument) 프로그램 12 -4 #include <stdio. h> void main(int argc, char *argv[]) { int I; printf("f 2 n. The number of items on the command line is %d", argc); for (i=0; I < argc; I++) { printf("The address stored in argv[%d] is %p”, I, argv[i]); printf("The character pointed to is %c", *argv[i]); } } 36

제 4 절 명령 행 인자(command line argument) • 프로그램 12 -4의 실행 C>showad

제 4 절 명령 행 인자(command line argument) • 프로그램 12 -4의 실행 C>showad three blind mice 실행 결과는 다음과 같다. The number of items on the command line is 4 The address stored in argv[0] is FFDA The character pointed to is s The address stored in argv[1] is FFEB The character pointed to is t The address stored in argv[2] is FFF 1 The character pointed to is b The address stored in argv[3] is FFF 7 The character pointed to is m 37

제 4 절 명령 행 인자(command line argument) 프로그램 12 -5 /* 명령 행

제 4 절 명령 행 인자(command line argument) 프로그램 12 -5 /* 명령 행 인자를 출력하는 프로그램 */ #include <stdio. h> void main(int argc, char *argv[]) { int I; printf("f 2 n. The following arguments were passed to main(): "); for (i=1; I < argc; I++) printf("%s ", argv[i]); printf(""); } 39

제 4 절 명령 행 인자(command line argument) 프로그램 12 -6 /* 명령 행

제 4 절 명령 행 인자(command line argument) 프로그램 12 -6 /* 명령 행 인자를 수치 자료로 출력하는 프로그램 */ #include <stdio. h> void main(int argc, char *argv[]) { int I; int value; int sum = 0; printf("f 2 n. The following numerical data of the arguments passed to main(): "); for (i=1; I < argc; I++) { value = atoi(argv[i]); sum += value; printf("%d ", value); } printf("f 2 n. The sum of the numerical data is %d", sum); } 40