1 / 26

처음으로 배우는 C 프로그래밍

처음으로 배우는 C 프로그래밍. 제1부 기초 제3장 치환, 주소, 대화식 입력. 강의 내용. 치환연산자 주소의 저장과 주소를 저장할 수 있는 변수의 선언 포인터의 활용 scanf() 함수 기호 상수(리터럴). 치환 ( 1 ). 치환 연산자 : = variable = operand; 오른쪽 피연산자는 상수나 변수, 또는 유효한 수식이 될 수 있음 오른쪽에 있는 피연산자의 값을 왼쪽에 있는 변수에 저장 예) year = 1988; total = total + newvalue;

brant
Download Presentation

처음으로 배우는 C 프로그래밍

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. 처음으로 배우는 C 프로그래밍 제1부 기초 제3장 치환, 주소, 대화식 입력

  2. 강의 내용.. • 치환연산자 • 주소의 저장과 주소를 저장할 수 있는 변수의 선언 • 포인터의 활용 • scanf()함수 • 기호 상수(리터럴)

  3. 치환 ( 1 ) • 치환 연산자 : = • variable = operand; • 오른쪽 피연산자는 상수나 변수, 또는 유효한 수식이 될 수 있음 • 오른쪽에 있는 피연산자의 값을 왼쪽에 있는 변수에 저장 예) year = 1988; total = total + newvalue; rate = prime; • 모든 연산자 중에 우선순위가 가장 낮음 ( 부록 A, 표 A-1 ) • 왼쪽에는 반드시 하나의 변수가 있어야 함 • amount + 1892 = 1000 + 10 * 5; /* 에러 */

  4. 치환 ( 2 ) • 동일한 수식에서 여러 개의 치환도 가능 • a=b=c=25 ; /* 이때 계산 순서는 아래와 같음 */ • c = 25; b = c; a = b; • a = ( b = ( c = 25 ) ); • a = 25; b = 25; c = 25;

  5. 치환 ( 3 ) • 프로그램 3-1 # include <stdio.h> void main(void) { float length, width, area ; length = 27.2 ; width = 13.6 ; area = length * width ; printf (“The length of the rectangle is %f ”, length) ; printf (“\n The width of the rectangle is %f “, width) ; printf (“\n The area of the rectangle is %f ”, area) ; }

  6. 치환 연산자 ( 1 ) • 등호 왼쪽의 변수가 오른쪽에서 먼저 사용될 수 있음 • sum = sum + 10 ; • 프로그램 3-2 #include <stdio.h> void main(void) { int sum ; sum = 25 ; printf (“\nThe number stored in sum is %d.. ”, sum) ; sum = sum + 10 ; printf (“\nThe number now stored in sum is %d. ”, sum) ; } 35로 덮어 씌워짐 25 sum = sum + 10 35 sum sum

  7. 치환 연산자 ( 2 ) • 치환 연산자의 양쪽에 같은 변수를 사용하는 치환문의 경우 • 다른 치환 연산자를 사용하여 재 작성 가능 • += -= *= /= %= 예) sum = sum + 25;  sum += 25; sum *= 10;  sum = sum * 10 sum /= 10;  sum = sum / 10; price *= rate;  price = prince * rate ;

  8. Statement Value in sum sum = 0 ; 0 sum = sum + 96 ; 96 sum = sum + 70 ; 166 sum = sum + 85 ; 251 sum = sum + 60 ; 311 누적(accumulating ) ( 1 ) • 값을 계속해서 누적하는 방법

  9. 누적 ( 2 ) • 프로그램 3-3 #include <stdio.h> void main() { int sum; sum = 0; printf ( “\nSum 은 %d 로 초기화 됨”, sum); sum = sum + 10; printf ( “\nSum 은 %d 로 변경됨”, sum); sum = sum + 10; printf ( “\nSum 은 %d 로 변경됨”, sum); sum = sum + 20; printf ( “\nSum 의 최종 값은 %d”, sum); }

  10. 증감 연산자 ( 1 ) • 누적문과 유사 • 변수 = 변수 + 고정 값(1); i = i + 1; n = n - 1; • 변수가 1씩 증/감 되는 경우를 위해 단항 연산자 제공 • ++, -- • ++i, i++, --n, n --

  11. 증감 연산자 ( 2 ) • 전위 증가 연산자 • k = ++n; n = n + 1; k = n; • 후위 증가 연산자 • k = n++; k = n; n = n + 1; Expression Alternative i = i + 1 ++i n = n + 1 ++n count = count + 1 ++count Expression Alternative i = i - 1 --i n = n - 1 --n count = count - 1 --count

  12. 증감 연산자 ( 3 ) • 프로그램 3-4 #include <stdio.h> void main() { int a = 1, b = 1; int aplus, plusb; aplus = a++; plusb = ++b; printf(“a=%d, aplus=%d, b=%d, plusb=%d\n”, a, aplus, b, plusb); } • 출력은 ? • a=2, aplus=1, b=2, plusb=2

  13. 주소 ( 1 ) • 전형적인 변수 • 변수에 저장된 값, 변수의 주소, 변수의 데이터 형으로 이루어짐 • 변수에 저장된 값은 변수의 내용이라고 하고 • 변수를 위해 사용된 메모리의 첫 번째 주소를 주소라 함 하나 또는 여러 개의 바이트 ( 데이터 형에 따라 다름) 변수 값(내용) 변수의 주소

  14. 주소 ( 2 ) • 프로그램 3-5 #include <stdio.h> void main() { int num ; num = 22 ; printf (“The value stored in num is %d. ” , num) ; printf (“\nThe computer uses %d bytes to stored this value”, sizeof(int)); } • 일반적인 프로그래머들은 변수의 이름과 저장된 값, 변수 형에 관심을 두게 됨

  15. 주소 ( 3 ) • 변수의 주소는 어떻게 알아 볼 수 있을까? • & 연산자 사용 • &num : 변수 num의 주소 • 주소의 출력은 • %p 사용 • 프로그램 3-6 #include <stdio.h> void main(void) { int num; num = 22; printf (“num = %d. The address of num is %p.\n”, num, &num); }

  16. 주소 ( 4 ) • 변수 num의 완전한 표현 2byte의 메모리 22 FFF0 변수 num이 사용한 첫 번째 바이트의 주소 변수 num의 내용

  17. 변수명 변수 내용 d Address of m tab_point Address of list chr_point Address of ch 주소의 저장 • 적절히 선언된 변수에 주소를 저장할 수 있음 • num_addr = &num; /* num의 주소를 num_addr에 저장 */ • 주소 저장 • d = &m; • tab_point = &list; • chr_point = &ch; • 포인터 변수 • 포인터를 저장할 수 있는 변수 • d, tab_point, chr_point • 포인터 변수도 사용되기 위해서는 먼저 선언되어져야 함 • int *num_addr; 변수명 변수 내용 num_addr num의 주소

  18. 주소의 사용 • 간접 연산자(*) 사용 • num_addr 와 b는 포인터 변수 : 주소 저장용 int *num_addr, *a, b, num=10; num_addr = &num; /* num의 시작 주소 FFAA를 저장 */ a = num_addr; /* 주소 FFAA를 a에 저장 */ b = *num_addr; /* 주소 FFAA에 저장된 10을 b에 저장 */ 포인터 변수 num_addr FFAA FFAA 정수변수 num 10

  19. 주소 • 프로그램 3-7 #include <stdio.h> void main(void) { int *num_addr; /* declare a pointer to an int */ int miles, dist; /* declare two integer variables */ miles = 22; /* store the number 22 into miles */ dist = 158; /* store the number 158 into dist */ num_addr = &miles; /* store the ‘address of miles’ in num_addr */ printf (“The address stored in num_addr is %p\n”, num_addr); printf (“The value pointed to by num_addr is %d\n\n”, *num_addr); num_addr = &dist; /* now store the address of dist in num_addr */ printf (“The address now stored in num_addr is %p\n”, num_addr); printf (“The value now pointed to by num_addr is %d\n”, *num_addr); }

  20. #include <stdio.h> void main(void) { scanf ( ) printf ( ) } Keyboard Screen scanf() 함수 • 대화식 프로그램 작성을 가능하게 함 • 표준 입력(키보드)로 부터 데이터를 입력 받음 • scanf()와 printf()함수 • scanf(“%d”, &num1) • printf(“%d”, &num2)

  21. scanf() 함수 • 프로그램 3-10 #include <stdio.h> void main(void) { float num1, numm2, product ; printf (“Please type in a number: ”) ; scanf (“%f”, &num1) ; printf (“Please type in another number: ”) ; scanf (“%f”, &num2) ; product = num1 * num2 ; printf (“ %f times %f is %f ”, num1, num2, product); }

  22. 버퍼 입력을 갖는 scanf() • 문자 입력 • 프로그램 3-11 #include <stdio.h> void main(void) { char fkey ; printf (“Please type in a character: ”) ; scanf (“%c”, &fkey) ; printf (“the keystroke just accepted is %d ”, fkey) ; }

  23. 기호 상수 • 리터럴 : 프로그램 내에서 그 값이 명확히 구별되는 데이터 • c = 2 * 3.14 * r; • 수식에 직접 입력된 상수 2와 3.14를 literal 이라 함 • #define • 프로그램 내에서 반복 사용되는 literal 값을 손쉽게 정의하는 방법 • #define PI 3.141592 • c = 2 * PI * r;

  24. 기호 상수 • 프로그램 3-14 #define SALESTAX 0.05 #include <stdio.h> void main(void) { float amount, taxes, total; printf (“\nEnter the amount purchased: ”) ; scanf (“ %f”, &amount) ; /* 금액 */ taxes = SALESTAX * amount ; /* 세금 5% 부과*/ total = amount - taxes ; /* 수령액 = 금액 - 세금 */ printf (“The sales tax is $%4.2f ”, taxes) ; /* 세금 출력 */ printf (\nThe total bill is $%5.2f ”, total) ; /* 수령액 출력 */ }

  25. 일반적인 프로그래밍 오류 • 모든 변수는 식에서 사용되기 전에 초기화 되어져야 함 • 증가 또는 감소 연산자를 식에서 사용할 경우 주의해야 함 • 포인터로 선언되지 않은 변수에 주소를 저장해서는 안됨 • scanf()함수에는 주소(&)를 넘겨 주어야 함 • scanf()함수의 제어 문자열에는 메시지가 포함되지 않음 • scanf()함수 호출시 입력될 데이터 값에 따라 정확한 제어문자(“%d”, “%c”)를 포함해야 함 • scanf()함수에 넘겨지는 제어 문자열을 이중따옴표로 닫아야 하며, 모든 인자는 쉼표로 구분해야 함 • #define문은 세미콜론으로 끝나지 않음

  26. 요약 • 치환연산자 • 주소의 저장과 주소를 저장할 수 있는 변수의 선언 • 포인터의 활용 • scanf()함수 • 기호상수(리터럴)

More Related