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]