CHAPTER 5
CHARACTERS

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
This chapter discusses characters, the one basic data type we've so far ignored. As with integers and reals, we study how to create character constants and define character variables, and we examine the range of values they can hold and present several different ways to read and print them. As part of this discussion, we introduce C's library functions for efficient character-at-a-time input and output and for testing whether a character falls into a particular class, such as uppercase or lowercase. Along the way, we'll also study what happens when we convert back and forth between characters and integers and discover that characters are little more than a special type of integer. The chapter concludes by extending the previous chapter's base conversion program to handle conversions from any base to any other base.

Jump to: [Previous Chapter | Next Chapter]


  1. REPRESENTING AND STORING CHARACTERS
  2. CHARACTERS VERSUS INTEGERS
  3. CHARACTER INPUT AND OUTPUT
  4. CHARACTER-TESTING FUNCTIONS
  5. CONVERTING CHARACTERS INTO NUMBERS
  6. CASE STUDY: A MORE GENERAL BASE CONVERTER

Objectives

Characters

  • ´ÙÀ½À» print ÇÏ·Á¸é? [My name is "Kwang-Soo Hahn."]



    Ç¥ÁØ ÀÔÃâ·Â (Standard Input/Output: stdio)

    C ÇÁ·Î±×·¥Àº ±âº»ÀûÀ¸·Î ´ÙÀ½ Ç¥ÁØ ÀÔÃâ·ÂÀ» Á¦°ø¹Þ´Â´Ù.

    ÀÌ·¯ÇÑ Ç¥ÁØ ÀÔÃâ·ÂÀº file·Î ¹æÇâ ÀüȯÀÌ °¡´ÉÇÏ´Ù.


    ¹®ÀÚ ÀÔ·Â : int getchar(void) - stdin¿¡¼­ ÇÑ ¹®ÀÚ ÀÔ·Â
    ¹®ÀÚ Ãâ·Â : int putchar(int) - stdoutÀ¸·Î ÇÑ ¹®ÀÚ Ãâ·Â

    char ch;
    
    ch = getchar();
    
    i = ch - '0';
    
    if (ch >= '0' && ch <= '9') . . . // ch°¡ ¼ýÀÚ '0'¿¡¼­ '9' »çÀÌ
    
    if (ch >= 'A' && ch <= 'Z') . . . // ch°¡ ¿µ´ë¹®ÀÚ 'A'¿¡¼­ 'Z' »çÀÌ
    
    if (ch >= 'a' && ch <= 'z') . . . // // ch°¡ ¿µ¼Ò¹®ÀÚ 'a'¿¡¼­ 'z' »çÀÌ
    
    for (ch = 'A'; ch <= 'Z'; ch++)
       putchar(ch);
    
    for (i = 0, ch = 'a'; i < 26; i = i + 1)
       putchar(ch + i);
    
    


    ¹®ÀÚÇü ÀÚ·á¿¡ ÀÚÁÖ »ç¿ëµÇ´Â operationµéÀÌ <ctype.h>¿¡ Á¤ÀǵǾî ÀÖ´Ù.

    islower(ch)ch°¡ ¼Ò¹®ÀÚ¸é TRUE
    isupper(ch)ch°¡ ´ë¹®ÀÚ¸é TRUE
    isalpha(ch)ch°¡ alphabetÀ̸é TRUE
    isdigit(ch)ch°¡ ¼ýÀÚ¸é TRUE
    isalnum(ch)ch°¡ alphabet ¶Ç´Â digitÀ̸é TRUE
    ispunct(ch)ch°¡ punctuation symbolÀ̸é TRUE
    isspace(ch)ch°¡ white space(' ', '\t', '\n', '\f', '\v')¸é TRUE
    tolower(ch)ch¸¦ ¼Ò¹®ÀÚ·Î º¯È¯
    toupper(ch)ch¸¦ ´ë¹®ÀÚ·Î º¯È¯



    Standard I/O redirection°ú pipe (MS-DOS¿Í UNIX OS¿¡¼­ À¯¿ë)

    Standard I/O·ÎºÎÅÍÀÇ ÀÔÃâ·ÂÀ» file ¶Ç´Â ¾Õ ÇÁ·Î¼¼½ºÀÇ Ãâ·ÂÀ¸·Î Àüȯ




    ½Ç½À ¿¹) ÀÔ·ÂÀÇ ³¡ÀÌ ¿Ã ¶§±îÁö Çѹ®ÀÚ Çѹ®ÀÚ¾¿ Àоî È­¸é¿¡ Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.

    ÀÔ·ÂÀÇ ³¡ °Ë»ç´Â EOF (End Of File) ¹®ÀÚ·Î ÇÔ. (stdio.h¿¡ Á¤ÀÇ)

    
    while ((ch = getchar()) != EOF)
       putchar(ch);
    
    
    

    EOF ¹®ÀÚ ÀÔ·ÂÀº control-'Z' ('Ctrl' key¿Í 'z' key¸¦ µ¿½Ã¿¡ ´©¸§)

    À§ ÇÁ·Î±×·¥À» ÀÛ¼ºÇÏ°í ½ÇÇà È­ÀÏ·Î ÀÔÃâ·Â º¯È¯À» °Ë»çÇ϶ó.




    Assignment #5 (±â°£: 1ÁÖÀÏ)

    ÇÁ·Î±×·¥ 1) Áø¹ý º¯È¯ ÇÁ·Î±×·¥ÀÇ È®Àå

  • 16Áø¹ý¿¡¼­´Â 10¿¡¼­ 15±îÁöÀÇ ¼ö¸¦ Ç¥ÇöÇϱâ À§ÇÏ¿© ¿µ¹®ÀÚ a¿¡¼­ f¸¦ »ç¿ëÇÑ´Ù. ±×·¯³ª ¿µ¹®ÀÚ a¿¡¼­ z±îÁö ÀüºÎ »ç¿ëÇϸé 36Áø¹ýÀÇ ¼ö±îÁö Ç¥Çö °¡´ÉÇÏ´Ù. ÀÓÀÇÀÇ 10Áø¼ö¸¦ ÀÔ·Â¹Þ¾Æ ÀÓÀÇÀÇ Áø¹ý ¼ö·Î º¯È¯ Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó. (0À» ÀÔ·ÂÇÒ ¶§±îÁö ¹Ýº¹ÇÒ °Í)
    75 36
    10Áø¼ö 75ÀÇ 36Áø¼ö °ªÀº 23
    71 36
    10Áø¼ö 71ÀÇ 36Áø¼ö °ªÀº 1z
    0 0
    
    


    ÇÁ·Î±×·¥ 2) ÀÓÀÇÀÇ ¹®ÀåÀ» ÀÔ·ÂÇÏ¿© ´Ü¾îº°·Î Ãâ·ÂÇÏ°í ÀÔ·ÂÀÌ ³¡³ª¸é ÃÑ ´Ü¾î ¼ö¸¦ Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.

  • ´Ü¾î »çÀÌÀÇ ±¸ºÐÀº white space ¹®ÀÚ·Î ÇÑ´Ù.
    
    abc def ghi
    abc
    def
    ghi
    kkk mmm ggg
    kkk
    mmm
    ggg
    Ctrl-Z
    ÃÑ 6 ´Ü¾î
    
    



    [ Table Of Contents | Previous Chapter | Next Chapter]