![[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]](../Images/QuarterFrontCover.bmp)
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;