This chapter continues our tutorial introduction to C. We focus on two new example C programs: one that computes interest accumulating in a bank account over time, the other that assigns pass/fail grades based on student scores. We describe how each of these programs works and provide several different extensions. These programs introduce C's while and for loops, if statement, and predefined {\headkw scanf} input function. By the chapter's end you'll have been exposed to a wide variety of C's features, and you should feel comfortable writing small but useful C programs.
Jump to: [Previous Chapter | Next Chapter]
/* * add2.c */ #include <stdio.h> // standard I/O header file void main() { int n1, n2, total; // ������ ���� ���� printf("This program adds two numbers.\n"); printf("1st number? "); scanf("%d", &n1); printf("2nd number? "); scanf("%d", &n2); total = n1 + n2; printf("The total is %d.\n", total); }
#include �� #define ���� '#' ��ȣ�� ù��° � �־�� �Ѵ�.
#include <stdio.h> ���� standard I/O library function ���ǵ��� ���α��� ���Խ��� �ڷ� ������� �����ϰ� �Ѵ�. C�� stdio �ܿ� string, time, math ���� ���� standard library���� �����Ѵ�.
main() �� main function�� �����Ѵ�. ��� C program�� �ϳ��� main function�� ���α��� �����Ͽ��� �Ѵ�. (��ġ�� �������)
printf ���� Ư�� ������ ��� �� ����ϸ� ��� ������ format string �̶� �Ҹ��� " �� " ������ ���뿡 ���� �����ȴ�.
��� ������ ����ϱ� ���� �ڷ����� ���� ���ǵǾ�� �Ѵ�.
Integer (������) |
int, unsigned, long, unsigned long, short |
Real (�Ǽ���) |
float, double, long double |
Character (������) |
char, unsigned character |
�ڷ��� ���� ��)
int a, b, c, d; float e, f, g, h; char i, j; a = 1, b = 2, c = 3, d = 4; a = 32768;
�ڷ��� |
Bytes |
��� �� ���� |
---|---|---|
short (int) |
2 �̻� |
-32768 ~ 32767 |
int |
2 or 4 |
-32768 ~ 32767 or -2147483648 ~ 2147483647 |
unsigned (int) |
2 or 4 |
0 ~ 65535 or 0 ~ 4294967295 |
long (int) |
4 �̻� |
-2147483648 ~ 2147483647 |
float |
4 |
10-38 ~ 1038 |
double |
8 |
10-308 ~ 10308 |
char |
1 |
ASCII Value (-128 ~ 127) |
unsigned char |
1 |
0 ~ 255 |
* Byte ���� ��ǻ�� ȯ�濡 ���� �ٸ�. sizeof�� �˻� ����
sizeof(int), sizeof(double) ��
���� ������ (Binary operators) : +, -, *, /, %
a + b 3.14 * 2 5 / 2 5 % 2
��� ���� ������ (Unary operator): - (minus: ��ȣ�� �ٲٴ� ������ ��)
���� �켱 ���� (Operator precedence): - (unary) > (*, /, %) > (+, -)
Interest Rate: 6.00% Starting Balance: $ 5000.00 Period: 7 years Year Balance 1 $ 5300.00 2 $ 5618.00 3 $ 5955.08 4 $ 6312.38 5 $ 6691.13 6 $ 7092.60 7 $ 7518.15
Interest Rate: 6.00% Starting Balance: $ 5000.00 Period: 7 years Year Balance
while-��: Expression�� ��(TRUE)�� ��� Statements �ݺ� ����
while (Expression) { Statements ; }
for-�ݺ���: Expression�� ��(TRUE)�� ��� Statements �ݺ� ����
for ( Start ; Test ; Action ) { Statements ; }
Enter interest rate, principal, and period: .04 1000 7 Interest Rate: 4.00% Starting Balance: $ 1000.00 Period: 7 years Year Balance 1 $ 1040.00 2 $ 1081.60 3 $ 1124.86 4 $ 1169.86 5 $ 1216.65 6 $ 1265.32 7 $ 1315.93
n = scanf("%lf %lf %i", &intrate, &balance, &period);
Score? 91 91 - Passes Score? 70 70 - Passes Score? 60 60 - Fails Score? 69 69 - Fails Score? 4 scores entered, 2 pass, 2 fail. Average: 72
if �� : expression�� TRUE(!= 0)�� statement ����
if (expression) statement;
if-else ��: expression�� ���̸� statement_1 �ƴϸ� statement_2 ����
if (expression) statement_1; else statement_2;