CHAPTER 2
GETTING COMFORTABLE WITH C

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]

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]


  1. A PROGRAM TO COMPUTE SIMPLE INTEREST
  2. DEALING WITH COMPILE ERRORS
  3. A MORE COMPACT INTEREST COMPUTING PROGRAM
  4. EXTENDING OUR INTEREST PROGRAM TO READ VALUES
  5. A PROGRAM TO PROCESS SCORES
  6. HANDLING INPUT ERRORS
  7. THE ASSIGNMENT OPERATOR


Objectives

/*
 *     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) ��

������ (Operators)

���� ������ (Binary operators) : +, -, *, /, %

     a + b
     3.14 * 2
     5 / 2
     5 % 2

��� ���� ������ (Unary operator): - (minus: ��ȣ�� �ٲٴ� ������ ��)

���� �켱 ���� (Operator precedence): - (unary) > (*, /, %) > (+, -)