Ãâ·Â ¾ç½Ä ÁöÁ¤ (Formatted output)
printf("control string", expr1, expr2, ... );
Control string¿¡ Æ÷ÇÔµÈ \¿Í % ±âÈ£´Â ¾ç½ÄÀ» °áÁ¤Çϴ Ư¼ö ±âÈ£·Î
Ãë±Þ
%d, %i | Decimal integer |
%ld, %li | Long integer |
%O | Octal |
%x, %X | Hexadecimal |
%f | Floating-point |
%e, %E | Exponential |
%g, %G | General, %f ¶Ç´Â %e Áß ÂªÀº Ç¥Çö »ç¿ë
|
%c | Character |
%s | String |
%% | Percent sign |
Ãâ·ÂÀÇ Å©±â¿Í Á¤¹Ðµµ ÁöÁ¤
- %[-][size][.precision]format-character
- - (minus sign): Ãâ·ÂÀ» ¿ÞÂÊ
Á¤·Ä (±âº»Àº
¿À¸¥ÂÊ Á¤·Ä)
- size : Ãâ·ÂÀÌ Â÷ÁöÇÏ´Â
Çà ¼ö
- precision : À¯È¿ ¼ýÀÚ
ÀÚ¸´¼ö
Format | Output
|
%d | 456 |
%5d | __456 |
%-5d | 456__ |
%2d | 456 |
%f | 123.456000 |
%12f | __123.456000 |
%9.2f | ___123.45 |
%e | 1.234560e+02 |
%9.2E | _1.23E+02 |
%s | abcdefg |
%10s | ___abcdefg |
%-10s | abcdefg___ |
%5s | abcdefg |
%5.5s | abcde |
Format string¿¡ Æ÷Ç﵃ ¼ö ÀÖ´Â special character
\a | Bell ¶Ç´Â alert character
|
\b | backspace |
\f | formfeed (start a new page)
|
\n | newline |
\r | carriage return |
\t | tab |
\v | vertical tab |
\\ | backslash (\) |
ÀÔ·Â ¾ç½Ä ÁöÁ¤ (Formatted input)
scanf("control string", ÁÖ¼Ò1, ÁÖ¼Ò2, ... );
scanf functionÀ» ÀÌ¿ëÇØ data¸¦ ÀÔ·ÂÇϱâ À§Çؼ´Â ÀÔ·ÂÇÒ data¸¦
ÀúÀåÇÒ º¯¼öÀÇ ¸Þ¸ð¸® ÁÖ¼Ò¸¦ argument·Î ÁÖ¾î¾ß ÇÑ´Ù.
scanf functionÀÇ formatÀº printf functionÀÇ format°ú À¯»çÇϸç
´ÙÀ½°ú °°ÀÌ Ã³¸®µÈ´Ù.
- Format string³»ÀÇ blank space´Â ÀÓÀÇÀÇ white-space¿Í matchµÈ´Ù.
- % ±âÈ£ µÚ¿¡ ´ÙÀ½°ú °°ÀÌ ÀÔ·Â ÀÚ·áÇüÀ» ÁöÁ¤ÇÑ´Ù.
- %d, %i : a decimal integer
- %f, %e, %g : float
- %lf : double
- %c : a character
- %s : a string
- %[...] : [...]¿¡ Æ÷ÇÔµÇÁö ¾ÊÀº ¹®ÀÚ°¡ ³ª¿Ã ¶§ ±îÁö ¹®ÀÚ¿
ÀÔ·Â.
- %[^...] : ^ ´ÙÀ½¿¡ ÁöÁ¤µÈ ¹®ÀÚ°¡ ³ª¿Ã ¶§ ±îÁö ¹®ÀÚ¿ ÀÔ·Â
- %% : % ±âÈ£°¡ ÀÔ·ÂÀÇ ´ÙÀ½ ¹®ÀÚ°¡ µÇ¾î¾ß ÇÑ´Ù.
- Format string ³»ÀÇ ´Ù¸¥ ¸ðµç ¹®ÀÚµéÀº ÀÔ·ÂÀÇ ¹®ÀÚ¿Í µ¿ÀÏÇÏ¿©¾ß
ÇÑ´Ù.
Standard I/O redirection°ú pipe (MS-DOS¿Í UNIX OS¿¡¼ À¯¿ë)
Standard I/O·ÎºÎÅÍÀÇ ÀÔÃâ·ÂÀ» file ¶Ç´Â ¾Õ ÇÁ·Î¼¼½ºÀÇ Ãâ·ÂÀ¸·Î
Àüȯ
Input redirection : prog <infile
Output redirection : prog >outfile
Pipe : prog1 | prog2