1 / 59

총정리 : 다양한 예제 분석

총정리 : 다양한 예제 분석. 출력형식. # include &lt;stdio.h&gt; // printf() #include &lt;conio.h&gt; // getch() void main() { int i=65; // i 를 int 형으로 선언, 초기값-&gt;65 printf(&quot;i =&gt; %d<br><br>&quot;, i); printf(&quot;i(%%d) : %d<br>&quot;, i); // 10 진수로 출력

glynn
Download Presentation

총정리 : 다양한 예제 분석

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 총정리 : 다양한 예제 분석

  2. 출력형식 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i=65; // i를 int형으로 선언, 초기값->65 printf("i => %d\n\n", i); printf("i(%%d) : %d\n", i); // 10진수로 출력 printf("i(%%x) : %x\n", i); // 16진수로 출력 printf("i(%%c) : %c\n", i); // 애스키 문자로 출력 printf("\ni : %d \ni^2 : %d", i, i*i); getch(); // 키입력을 대기 }

  3. 출력형식 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i=123; // i를 int형 변수로 선언 float f=12.34567; printf("|%5d|\n", i); // 폭 : 5, 오른쪽 맞춤 printf("|%-5d|\n", i); // 왼쪽 맞춤 printf("|%5ld|\n\n",123456789); // 폭 : 9 printf("|%10f|\n", f); // 폭 : 10, 오른쪽 맞춤 printf("|%010.3f|\n", f); // 소숫점 이하 3자리 getch(); // 키입력을 대기 }

  4. If-else #include <stdio.h> // printf(), scanf() #include <conio.h> // getch() void main() { int i, suja; printf("suja ...? "); scanf("%d", &suja); if (suja >= 100) // 100 이상이면 printf("100 YeeSang\n"); else if (suja >= 0) // 100 미만, 0 이상이면 printf("0 ~ 100 SaYee\n"); else if (suja >= -100) // 0 미만, -100 이상이면 printf("-100 ~ 0 SaYee\n"); else // -100 미만이면 printf("-100 MiMan\n"); getch(); }

  5. For문 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i, hab=0; // 1 ~ 10의 hab을 계산 for (i=1 ; i <= 10 ; i++) { printf("%-3d", i); hab += i; } printf("\n1 ~ 10 hab : %d\n", hab); getch(); }

  6. While #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i=0; long hab=0; // hab을 구하려면 0으로 // 초기화되어 있어야 한다 while ( i<=100 ) // i<=100인한 반복 { hab += i; // hab을 i만큼 증가시킴 i++; // i를 1만큼 증가시킴 } printf("1 ~ 100 hab : %ld", hab); getch(); }

  7. Char형 범위 #include <stdio.h> void main() { char i=0; // char형 범위 : -128 ~ 127 while ( 1 ) // 무한 while 루프 { printf("%5d", i); i++; } }

  8. Do-while #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i=0, n; long hab=0; printf("n....? "); scanf("%d", &n); do { hab += i; i++; } while ( i<=n ); // i<=n인한 반복 printf("i : %d\n", i); printf("1 ~ n hab : %ld\n", hab); getch(); }

  9. switch #include <stdio.h> // printf(), scanf() #include <conio.h> // getch() void main() { int n; printf("n....? "); scanf("%d", &n); printf("\n n %% 5 : %d\n", n % 5); switch (n % 5) { case 0: printf("\nNaMerJi 0"); break; case 1: printf("\nNaMerJi 1"); break; case 2: printf("\nNaMerJi 2"); break; default : printf("\nNaMerJi 3 or 4"); break; } getch(); }

  10. Break #include <stdio.h> // printf(), scanf() #include <conio.h> // getch() void main() { int suja,hab=0; while ( 1 ) // 무한 while 루프 { printf(" suja(IbRyuk End :0)...? "); scanf("%d", &suja); if (suja == 0) // suja==0이면 while 루프 종료 break; hab += suja; // hab에 suja를 더한다 } printf("\n hab : %d", hab); getch(); }

  11. #include <stdio.h> // printf(), scanf() #include <conio.h> // getch() #include <math.h> // sqrt() void main() { int suja=1; while ( suja != 0 ) // suja가 0이 아닌한 반복 { printf("\n suja...? "); scanf("%d", &suja); if (suja < 0) // 음수이면 { printf(" 0 YeeSang JungSu !\n"); continue; } printf(" %d JeGobGun : %g\n", suja, sqrt(suja)); } printf("\n\n End"); getch(); } Continue

  12. #include <stdio.h> // printf(), scanf() #include <conio.h> // getch() void main() { int i, n, c='A'; while ( 1 ) // 무한 while 루프 { printf("\n\n n ....? "); scanf("%d", &n); for (i=1 ; i<=n ; i++) // n회 반복 실행된다 { printf(" %c", c); if (c == 'Q') // c가 Q이면 goto END; c++; // c값을 1 증가 } } END: printf("\n\n End"); getch(); } goto

  13. 초기화 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i, hab; for (i=1 ; i<=10 ; i++) // 1 ~ 10의 hab을 계산 hab = hab+i; // hab += i;와 동일 printf("1 ~ 10 hab : %d\n", hab); getch(); }

  14. ++연산자 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i, a=1; printf("\na : %d\n", a); for (i=1 ; i<=3 ; i++ ) printf("a : %d\t", a++); // a++ : 사용후 1증가 printf("\na : %d\n\n", a); // a : 4 a = a*10; // a : 40 printf("\na : %d\n", a); for (i=1 ; i<=3 ; i++ ) printf("a : %d\t", ++a); // ++a : 1증가후 사용 printf("\na : %d\n", a); // a : 43 getch(); }

  15. #include <stdio.h> // printf() #include <conio.h> // getch() void main() { short s; // short 형 범위 : -32768 ~ 32767 long l; // long 형 범위 : 약 -21억 ~ 21 억 s = 1000000; // short 형 범위를 넘어서는 값 l = 1000000; printf("s : %d\n", s); printf("l : %d\n", l); getch(); } 형범위

  16. #include <stdio.h> // printf() #include <conio.h> // getch() void main() { long n; while ( 1 ) // 무한 while 루프 { do { printf(" n(Keut : 0 or 100)...? "); scanf("%d", &n); } while (! (n>=0 && n<=100) ); // 0~100 사이가 아닌한 반복 if (n==0 || n==100) // 0 또는 100이면 break; printf(" n^2 : %ld\n\n", n*n); } printf("\n Keut"); getch(); } 무한 while

  17. #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int x=20, y=10, z; char op; printf(" x : %d y : %d\n\n", x, y); //------------------------------------ printf(" +/-...? "); op = getche(); if (op == '+') z=x+y; else z=x-y; printf("\n %d\n", z); //-------------------------------------- printf("\n +/-...? "); op = getche(); z = (op == '+') ? x+y : x-y ; printf("\n %d", z); getch(); } 연산자

  18. 형변환 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i=2, j=3; printf("i : %d j : %d\n\n",i,j); printf("i/j : %d\n", i/j); printf("(float)i/j : %f\n", (float)i/j); printf("(float)(i/j) : %f\n", (float)(i/j)); printf("(int)3.789 : %d\n", (int)3.789); printf("(char)65.987 : %c\n", (char)65.987); getch(); }

  19. 논리연산자 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { // unsigned char형 범위 : 0 ~ 0xFF unsigned char a=0xA7; printf("a : %X\n\n", a); printf("a & F0 : %X\n", a & 0xF0); printf("a | F0 : %X\n", a | 0xF0); printf("a ^ F0 : %X\n", a ^ 0xF0); getch(); }

  20. #include <stdio.h> // printf() #include <conio.h> // getch() #define MSG "suja...? " #define MAX 100 void main() { int i, suja; printf("The largest input number : %d\n\n", MAX); // MAX(==100)보다 큰 숫자가 입력되는한 반복 do { printf(MSG); scanf("%d", &suja); } while (suja > MAX); printf("\nThe largest input number : MAX\n"); printf( "Input number : %d\n", suja); getch(); } 매크로

  21. #include <stdio.h> // printf() #include <conio.h> // getch() void main() { short a[3][2] = { {1,2}, {3,4}, {5,6} }; int i; printf("\n a : %p", a); printf("\n a[0] : %p", a[0]); printf("\n a[1] : %p", a[1]); printf("\n a[2] : %p\n", a[2]); for (i=0 ; i<3 ; i++) { printf("\n a[%d][0] : %d", i, a[i][0]); printf("\n a[%d][1] : %d", i, a[i][1]); } printf("\n\n sizeof(a) : %2d", sizeof(a)); printf( "\n sizeof(a[0]) : %2d", sizeof(a[0])); getch(); } 2차원배열

  22. 문자열배열 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { char *cp[4] = {"YeJeRo BaeUNun", "Visual C++", "ShinDongJun", "GiJunYunGuSa" }; int i; // 메모리 주소와 수록된 자료를 출력 for (i=0 ; i<4 ; i++) printf( "\ncp[%d] : %d => %s", i, cp[i], cp[i]); getch(); }

  23. #include <stdio.h> // puts() #include <conio.h> // getch() #include <malloc.h> // malloc() #include <ctype.h> // toupper() char *DaeMunJa(char *str) // 함수값 : str의 각 문자를 대문자로 바꾼 문자열 { int i, j; char *p; // 문자열 str의 문자수를 i에 구한다 i = 0; while (str[i] != 0) i++; // str의 문자열을 수록할 메모리 할당 p = (char *)malloc(i+1); // str의 각 문자를 대문자로 변환, 할당된 메모리에 수록 for (j=0 ; j<i ; j++) p[j] = toupper(str[j]); p[j] = 0; // 공문자열로 만든다 return p; } 문자열 함수값 void main() { puts( DaeMunJa("Visual VC++. Exciting !!") ); puts( DaeMunJa(”Written by ShinDJ ???") ); getch(); }

  24. #include <stdio.h> // printf() #include <conio.h> // getch() long Hab(int *, int *, int *); // 함수의 원형을 선언 void main() { int a=10, b=20, c=30; printf("\nHab(&a, &b, &c) : %d", Hab(&a, &b, &c) ); printf("\n a : %d", a); printf("\n b : %d", b); printf("\n c : %d", c); getch(); } long Hab(int *x, int *y, int *z) // 함수값 : *x + *y + *z { int hab = *x + *y + *z; *x = 9; *y = 8; *z = 7; return hab; } 함수인자

  25. #include <stdio.h> // printf() #include <conio.h> // getch() int gob(int *x, int *y) { int h, c, g; h = *x + *y; c = *x - *y; g = *x * *y; *x = h; // 합. 실인자 a에 인도된다 *y = c; // 차. 실인자 b에 인도된다 return g; // 곱 : 함수값 } 함수인자 - 주소 void main() { int a=10, b=20; printf("\n a : %d b : %d\n\n", a, b); printf("\n gob(&a, &b) : %d", gob(&a, &b)); printf("\n a(Hab) : %d", a); printf("\n b(Cha) : %d", b); getch(); }

  26. #include <stdio.h> // printf() #include <conio.h> // getch() #include <stdlib.h> // rand(), srand(), RAND_MAX #include <time.h> // time(), time_t #include <malloc.h> // malloc(), free() void main() { int i; long *lp[5]; time_t t; // 실행시마다 다른 계열의 난수 발생되게 한다 srand( time(&t) ); // 메모리 할당, 할당된 메모리에 난수 수록 for (i=0 ; i<5 ; i++) { lp[i] = (long *)malloc( sizeof(long) ); *lp[i] = ( rand()*9/RAND_MAX+1 )*100000; } 포인터배열 // 할당된 메모리의 주소와 수록된 자료를 출력 for (i=0 ; i<5 ; i++) { printf( "\nlp[%d] : %d BunJi", i, lp[i]); printf("\t*lp[%d] : %ld", i, *lp[i]); free( lp[i] ); // 사용이 끝나면 메모리 해제 } getch(); }

  27. #include <stdio.h> // printf() #include <conio.h> // getch() void main() { long *p; int i; p = (long *)1000; printf("p : %d\n", p); // --p는 p의 주소를 4바이트씩 감소 for (i=0 ; i<5 ; i++) printf("\n--p : %d", --p); getch(); } 포인터주소

  28. #include <stdio.h> // putchar() #include <conio.h> // getch() void main() { int ch; for (ch=65 ; ch<=70 ; ch++) { putchar(ch); // 애스키 코드가 ch인 문자를 출력 printf(" Ascii Code : %d", ch); putchar('\n'); } getch(); } Putchar()

  29. #include <stdio.h> // puts() #include <conio.h> // getch() void main() { char *mjyul1 = "YeJeRo BaeUNun"; // char형 포인터 char *mjyul2 = "VC++"; puts(mjyul1); puts(mjyul2); puts("\n\aKeut"); getch(); // \a : 삑 소리 } Puts()

  30. #include <stdio.h> // printf(), scanf() #include <conio.h> // getch() long hab(int) ; // 함수 원형 선언 long hab(int n) // 함수값 : 1 ~ n 사이의 정수합 { if (n == 1) return 1; else return n + hab(n-1); } void main() { int n; long h; printf("\n n... ? "); scanf("%d", &n); h = hab(n); printf("\n 1 ~ %d hab : %d\n", n, h); getch(); } 재귀호출

  31. #include <stdio.h> // printf(), NULL #include <conio.h> // getch() 자기참조구조체 void main() { struct _gujoche x = { 'X', 1, NULL }, y = { 'Y', 2, NULL }, z = { 'Z', 3, NULL }, w = { '#', 77, NULL }; struct _gujoche *p; // x->y->z로 연결(연결 리스트) x.next = &y, y.next = &z, z.next = NULL; p = &x; // 연결 리스트 시작 주소를 p에 대입 ListPyoSi(p); // 리스트 각 노드의 멤버를 출력 printf("\n"); // y 와 z 노드 사이에 w 노드를 삽입(x->y->w->z) y.next = &w, w.next = &z; p = &x; ListPyoSi(p); getch(); } void ListPyoSi(struct _gujoche *p); struct _gujoche { char c; int i; struct _gujoche *next; };

  32. void ListPyoSi(struct _gujoche *p) // p가 가리키는 리스트의 각 노드 멤버를 출력 { // 마지막 노드에 도달할 때까지 반복 while ( p != NULL ) { printf("\n %c %2d", p->c, p->i); p = p->next; // p를 다음 노드로 이동 } }

  33. #include <stdio.h> // printf() #include <conio.h> // getch() #include <math.h> // sqrt(), log(), exp()... #define PI 3.14159265 void main() { printf("\n sqrt(123.45) : %g\n", sqrt(123.45)); printf( " log(10) : %g\n", log(10)); printf( " exp(1) : %g\n", exp(1)); printf( " sin(PI/6) : %g\n", sin(PI/6)); printf( " tan(PI/4) : %g\n", tan(PI/4)); printf( " pow(12.34, 2) : %g\n", pow(12.34,2)); getch(); } 수학함수

  34. Strchr() #include <stdio.h> // printf() #include <conio.h> // getch() #include <string.h> // stchr() void main() { char *str1 = "C & Pointer. Powerful !"; char *str2; str2 = strchr(str1, 'o'); // 'o' 이후 문자열만 구한다 printf("\n str1 : %s", str1); printf("\n str2 : %s", str2); getch(); }

  35. #include <stdio.h> // printf() #include <conio.h> // getch() #include <string.h> // strlen(), strcmp() void main() { char *str1 = "C & POINTER"; char *str2 = "C & Pointer"; printf( "\n %u", strlen(str1)); printf("\n\n %d", strcmp( str1, str2)); printf("\n\n %d", strncmp(str1, str2, 5)); getch(); } Strcmp()

  36. #include <stdio.h> // printf() #include <conio.h> // getch() #include <malloc.h> // malloc() #include <string.h> // strcpy(), strncpy() void main() { char *str1, str2[10]; str1 = (char *)malloc(10); strcpy( str1, "ABCDEFGHI"); strncpy(str2, "ABCDEFGHI", 3); printf("str1 : %s\n", str1); printf("str2 : %s\n", str2); // 쓰레기가 따라 붙기도 한다 getch(); } Strcpy()

  37. #include <stdio.h> // printf(), scanf() #include <conio.h> // getch() long hab(int m, int n); // hab()의 원형을 선언 void main() { int i,j; long h; //----------------- 숫자 입력 (0을 입력하면 종료) do { printf("\n i.... ? " ); scanf("%d", &i); printf( " j.... ? " ); scanf("%d", &j); h = hab(i,j); printf("\n hab : %ld\n", h); } while ( i != 0 ); getch(); } 사용자함수 long hab(int m, int n) // 함수값 : m ~ n 사이의 정수합 { int i; long sum=0; for (i=m ; i<=n ; i++) sum += i; return sum; }

  38. #include <stdio.h> // printf(), puts() #include <conio.h> // getche(), putch(), cputs() #include <malloc.h> // malloc() void main() { char *cp; int n=0, i; cp = (char *)malloc(200); // 엔터 키가 입력될 때까지 문자를 입력받는다 do { *cp = getche(); // 문자 입력, *cp에 수록 n++; } while (*cp++ != '\r'); *cp = 0; // 공문자열로 만든다 cp--; // 마지막 문자 위치로 이동 printf("\n\n"); // 입력된 문자열의 문자들을 역순으로 출력 for (i=0 ; i != n ; i++) putch( *cp-- ); printf("\n\n"); cp++; puts(cp); getch(); } 문자열

  39. Main인자 #include <stdio.h> /* printf() */ void main(int argc, char **argv) { int i; printf("\n ==== : %d\n", argc); for (i=0 ; i<argc ; i++) printf("\n *(argv+%d) : %s", i, *(argv+i) ); }

  40. #include <stdio.h> // printf() #include <conio.h> // getch() void main() { int i, j; int a[4][5] = { 'A', 80, 70, 0, 1, 'B', 65, 60, 0, 1, 'C', 50, 85, 0, 1, 'D', 90, 90, 0, 1 }; // i번째 학생의 합을 a[i][3]에 구한다 for (i=0 ; i<4 ; i++) a[i][3] = a[i][1] + a[i][2]; // i번째 학생의 석차를 a[i][4]에 구한다 for (i=0 ; i<4 ; i++) for (j=0 ; j<4 ; j++) if (a[j][3] > a[i][3]) a[i][4]++; printf("Name Kor Math Sum Rank\n"); 배열 // i번째 학생의 성적을 출력 for (i=0 ; i<4 ; i++) { printf("%c %d %d %d %d", a[i][0], a[i][1], a[i][2], a[i][3], a[i][4]); printf("\n"); } getch(); }

  41. #include <stdio.h> // printf() #include <conio.h> // getch() // a[i]의 값을 모두 3씩 증가시킨다. n : 배열의 요소수 void SamJeungGa(int *a, int n) { int i; for (i=0 ; i<n ; i++) a[i] += 3; } void main() { int i, byul[] = { 10,20,30,40,50 }; SamJeungGa(byul, sizeof(byul)/sizeof(int)); for (i=0 ; i<5 ; i++) printf("\n %d", byul[i]); getch(); } 배열인자

  42. 배열주소 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { short byul[5]; int i; printf("\n byul : %p\n", byul); for (i=0 ; i<5 ; i++) printf("\n &byul[%d] : %p", i, &byul[i]); printf("\n\n byul ByteSu : %d\n", sizeof(byul)); getch(); }

  43. #include <stdio.h> // printf() #include <conio.h> // getch() void main() { char byul[] = "BEYUL"; char *ptr = "POINTER"; int i; // 배열(byul)을 포인터 형식으로 참조 for (i=0 ; i<5 ; i++) printf("*(byul+%d) : %c\n", i, *(byul+i)); // 포인터(ptr)를 배열 형식으로 참조 for (i=0 ; i<7 ; i++) printf("\nptr[%d] : %c", i, ptr[i] ); getch(); } 배열포인터

  44. 문자배열 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { char irum[] = "SHINDJ"; char juso[] = {'S', 'E', 'O', 'U', 'L', 0}; char chwimi[] = {'S', 'O', 'O', 'L'}; printf("\nirum : %s", irum); printf("\njuso : %s", juso); printf("\nchwimi : %s", chwimi); getch(); }

  45. 문자열 #include <stdio.h> // printf() #include <conio.h> // getch() void main() { char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char n; printf(" alpha : %s", alpha); printf("\n\n MyutGulJa..."); scanf("%d", &n); alpha[n] = 0; printf("\n alpha : %s", alpha); getch(); }

  46. 문자포인터 #include <stdio.h> // printf() #include <conio.h> // printf() void main() { char *p = "SHINDJ"; int i=0; // 문자열 p의 끝에 도달할 때까지 반복 do { printf("*(p+%d) : %c(%d)\n", i, *(p+i), *(p+i)); } while ( *(p + i++) != 0 ); getch(); }

  47. #include <stdio.h> // printf() #include <conio.h> // getch() //1 ~ n 사이의 정수를 출력. 함수값:1 ~ n 사이의 정수합 long PyoSiHab(int n) { int i; long h=0; // 1 ~ n 사이의 정수를 출력 for (i=1 ; i<=n ; i++) printf("%8d", i), h += i; return h; // 1 ~ n 사이의 정수합 } void main() { long hab; hab = PyoSiHab(50); printf("\n hab : %ld", hab); getch(); } 함수값, 함수인자

  48. #include <stdio.h> // printf() #include <conio.h> // getche() #include <ctype.h> // isupper(), islower().... void main() { int c; do { printf("\nMunJa ... ? " ); c = getche(); if (isupper(c)) printf("\n\t DaeMunJa\n"); else if (islower(c)) printf("\n\t SoMunJa\n"); else if (isdigit(c)) printf("\n\t SuJa\n"); else printf("\n\t GiTaMunJa\n"); } while ( ! isspace(c) ); // 공란,탭,엔터가 아닌한 반복 printf("\nKeut"); getch(); } Ctype.h

  49. #include <stdio.h> // printf() #include <conio.h> // getch() #include <malloc.h> // malloc() void main() { char *cp ; int danersu = 0; // 500바이트를 할당, 시작 주소를 cp에 대입 cp = (char *)malloc(500); printf(" MunJaYul ... ? "); gets(cp); // 문자열의 끝 문자에 도달할 때까지 반복 while (*cp != 0) { // 공란이 아니고 문자열 끝이 아니면(단어 중간이면) while (*cp != ' ' && *cp != 0) cp++; // cp를 다음 문자로 이동 danersu++; while ( *cp == ' ' ) // 공란이면 cp++; // cp를 다음 문자로 이동 } printf("\n DaNerSu : %d", danersu); getch(); } Malloc()

  50. #include <stdio.h > // printf() #include <conio.h> // getch() int x; // 전역변수. HamSu(), main()에서 모두 유효 void HamSu() { int m; // 지역변수. HamSu()에서만 유효 m = 1234; x = 5678; printf("\n\n\t\t m(HamSu): %d ", m); printf(" \n\t\t x(HamSu): %d ", x); } 전역,지역변수 void main() { int m; // 지역변수. main()에서만 유효 m=12, x=34; printf( "\n HamSu() SilHaengJun"); printf("\n\t m(main) : %d", m); printf("\n\t x(main) : %d", x); HamSu(); printf("\n\n HamSu() SilHaengHoo"); printf("\n\t m(main) : %d", m); printf("\n\t x(main) : %d", x); getch(); }

More Related