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]
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·ÎºÎÅÍÀÇ ÀÔÃâ·ÂÀ» file ¶Ç´Â ¾Õ ÇÁ·Î¼¼½ºÀÇ Ãâ·ÂÀ¸·Î Àüȯ
ÀÔ·ÂÀÇ ³¡ °Ë»ç´Â EOF (End Of File) ¹®ÀÚ·Î ÇÔ. (stdio.h¿¡ Á¤ÀÇ)
while ((ch = getchar()) != EOF) putchar(ch);
EOF ¹®ÀÚ ÀÔ·ÂÀº control-'Z' ('Ctrl' key¿Í 'z' key¸¦ µ¿½Ã¿¡ ´©¸§)
À§ ÇÁ·Î±×·¥À» ÀÛ¼ºÇÏ°í ½ÇÇà ÈÀÏ·Î ÀÔÃâ·Â º¯È¯À» °Ë»çÇ϶ó.
75 36 10Áø¼ö 75ÀÇ 36Áø¼ö °ªÀº 23 71 36 10Áø¼ö 71ÀÇ 36Áø¼ö °ªÀº 1z 0 0
abc def ghi abc def ghi kkk mmm ggg kkk mmm ggg Ctrl-Z ÃÑ 6 ´Ü¾î