computer programming - c language -contents.kocw.net/kocw/document/2014/hankukforeign/... ·...
TRANSCRIPT
Programming Steps
Source Code
Compiler
Object Code
Linker
Executable Code
#include <stdio.h>
#include <math.h>
int main(void) {
printf(“I love C = %lf\n“, sin(2.19));
return 0;
}
sea.c 또는 sea.cpp
Program Begin
SET A “I love C =”
WRITE A OUT
SET B 2.19
CALL SIN B C
WRITE C RET
Program End
sea.o Startup Code
SIN B
func begin
-------
func end
sea
Programming Steps
Startup Code Starting address
0x300001
0x300001
0x300002
0x300003
0x300004
0x300005
0x300006
0x300007
0x300008
I love C =
2.19
Main Code
WRITE A OUT
SET A 0x300007
SET B 0x300008
CALL SIN B C
(Goto 0x100008)
WRITE C RET
SIN function
0x100008
0x100009
0x100010
Programming Steps
• Compiler
– UNIX, GNU, Borland C (Turbo C), Microsoft Visual C/C++
• 사용할 compiler: Visual Studio 2008/2010 또는 Visual C++ Express Edition
– Visual C++ Express Edition은 Microsoft 홈페이지에서 무료 다운로드하여 사용 가능
– 프로젝트 형식: Win32
– 템플릿: Win32 콘솔 응용 프로그램
– 이름: <적당히 작명할 것>
– 위치: <자신의 디렉토리를 만들어 지정할 것>
#include <stdio.h>
#include <math.h>
int main(void) {
printf(“I love C = %lf\n“, sin(2.19));
return 0;
}
Analysis of “sea.c”
keyword header file (standard input output)
predefined function call
function name type
return control
function call in C library
function
Y = F(X)
printf
• printf(“사과\n”);
printf(“토마토\n”);
printf(“파인애플\n”);
– C 문장들은 차례대로 수행된다.
• printf(“ … “)
– #include <stdio.h>가 필요하다
– 큰 따옴표 안의 문자열이 그대로 출력된다
• \n
– 행을 바꾸는 특별 명령 문자
• \a (경고음), \b(back space), \t(탭), \r(캐리지 리턴), \’ , \”, \?, \\
특별 출력 명령 사용
• printf(“boy \tapple\n”);
printf(“mornimk\b\bng\n”);
printf(“up\r\down\n”);
printf(“sound!\a\a\a\n”);
printf(“\” 큰 따옴표!\n”);
printf(“\\ backspace\\ \n”);
printf와 데이터 출력
• printf(“Apple %d Orange %d\n”, 100, 200.9);
• %d – 정수형식으로 출력함 (소수점 이하가 없음)
• %f, %lf
– 실수형식으로 출력함
– %f : 4 바이트에 저장된 실수 값 출력
– %lf : 8 바이트에 저장된 실수 값 출력
• %c
– 문자형식으로 출력
• %s
– 문자열 형식으로 출력
marathon.c #include <stdio.h>
int main(void)
{
int miles, yards;
float kilometers;
miles = 26;
yards = 385;
kilometers = 1.609 * (miles + yards/1760.0);
printf(“\n A marathon is %f kilometers.\n\n”, kilometers);
return 0;
}
한 문장은 세미콜론으로 끝난다 (예외있음)
상수
변수
수식
블록은 { 로 시작해서 } 로 끝난다.
marathon.c
#include <stdio.h>
Standard Input Output header file
• C는 여러 개의 표준 헤더 파일을 제공한다 (예: stdio.h, math.h, string.h)
• 헤더파일은 .h 로 끝난다.
• 헤더 파일에는 함수 선언문, type 선언, 상수 선언 등이 담겨 있다.
• #include <abc.h> 라고 하면 abc.h 라는 헤더파일이 참조된다.
• 컴파일을 시작하기 전에 참조된 헤더파일을 프로그램에 복사한다.
{
}
marathon.c
int main ( void )
main 함수가 돌려줄 값의 종류
함수의 이름
외부에서 main 함수로 전달되는 값
void란 아무런 값도 전달되지 않는다는 의미
• 함수 선언 (function definition)
함수 정의
{ … }
(함수의 본체)
marathon.c
변수의 종류 (integer, float, char)
integer 변수의 이름
두개 이상의 변수를 선언할 때는 comma로 구별
• 변수 선언 (variable definition)
int miles , yards ; 함수선언을 제외한 모든 문장은
semi-colon으로 끝나야 한다.
marathon.c
• 정수 자료형 (integer type)
– short int 최소 16 bit에 저장
– int 최소한 short 보다 많은 bit에 저장 (일반적으로 4bytes = 32bits)
– long int 최소 32 bit에 저장
float kilometers ;
• 실수 (부동소수점) 자료형 (floating-point type)
– float 최소 32 bit에 저장 (4bytes)
– double 최소 64 bit에 저장 (8bytes)
marathon.c
kilometers = 1.609 * (miles + yards/1760.0);
• 정수 값을 정수형 변수에 배정
• 만약, miles = 26.4 라고 했다면 miles에는 26이 배정된다.
miles = 26;
yards = 385;
• yards/1760.0 의 결과는 실수형이 된다.
• (괄호)의 결과는 (정수 + 실수)이므로 실수형이 된다.
• 1.609 * (괄호)의 결과는 실수 * 정수이므로 실수형이 된다.
• kilometers는 실수형이므로 실수 값이 배정된다.
• 배정 (assignment) A = B
– 자료형 변환 (type conversion)
marathon.c
• \n 은 현재 출력 줄을 마감하고 이후의 출력은 다음 줄에 하라는 의미
• %f 는 그 자리에 실수형 (float 형) 값을 출력하라는 의미
• %d 는 정수형, %lf 는 double 형, %c 는 문자형 값을 출력하라는 의미
• printf ( format , 값 ) 함수
– 화면에 값들을 format에 맞게 출력하는 함수
printf(“\n A marathon is %f kilometers.\n\n”, kilometers);
marathon.c
• 정수 값 0을 함수의 최종 결과 값으로 호출한 곳에 돌려준다.
return 0;
double a, b;
a = 16.0;
b = sqrt(16.0);
printf(“ Square root of %lf is %lf \n”, a, b);
double sqrt ( 16.0 )
{
지지고 볶아서 4.0 계산
return ( 4.0 );
}
#define
• #define PI 3.14159
• #define LIMIT 100
– PI는 3.14159라는 실수 상수(constant)의 별칭이다.
– LIMIT 는 100이라는 정수 상수의 별칭이다.
– #define 행의 끝에는 세미콜론을 붙이지 않는다.
• printf ( “ Limit = %d\n”, LIMIT + 200)
– 300이 출력된다.
• PI = 200;
– 이 문장의 결과는 ?
전처리과정 (preprocessing)
#include <stdio.h>
#define LIMIT 100
#define PENALTY -50
#define MONEY 50
int main (void) {
if ( current_speed > LIMIT )
printf(“ Penalty %d \n”, PENALTY);
else
printf(“ Money %d\n”, MONEY*2);
}
int main (void) {
if ( current_speed > 100 )
printf(“ Penalty %d \n”, -50 );
else
printf(“ Money %d\n”, 50*2 );
}
Copy of <stdio.h>
전처리과정 (preprocessing)
• Macro
#include <stdio.h>
#define PI 3.14159
#define AREA( r ) PI * r * r
int main (void) {
printf(“ Area = %lf \n”, AREA(2) );
printf(“ Area = %lf \n”, AREA(1+1);
}
int main (void) {
printf(“ Area = %lf\n”, 3.14159*2*2 );
printf(“ Area = %lf\n”, 3.14159*1+1*1+1);
}
Copy of <stdio.h>
3.14159*4 = 12.56636
3.14159*4 = 12.56636
3.14159*4 = 12.56636
3.14159*1 + 1 + 1 = 3.14159 + 2 = 5.14159