putchar와 putch에 관해 질문드립니다.

sweethmlove2930의 이미지

#include <stdio.h>
 
void my_puts(const char *);
 
int main()
{
	my_puts("English 한글 漢文 ★♡");
 
	return 0;
}
 
void my_puts(const char *str)
{
	while(*str) putchar(*str++);
	putchar('\n');
}

이와 같은(putchar을 이용한) 코드를 실행하면, 한글은 물론, 한문과 특수문자 까지도 잘 출력됩니다.

그런데,

#include <conio.h>
 
void my_puts(const char *);
 
int main()
{
	my_puts("English 한글 漢文 ★♡");
 
	return 0;
}
 
void my_puts(const char *str)
{
	while(*str) putch(*str++);
	putch('\n');
}

putch를 사용해서 함수를 호출하면 한글, 한문, 특수문자가 모두 깨져 나옵니다.
putch와 putchar모두 1Byte를 출력하는 것으로 알고 있는데, 왜 putchar에서 한글, 한문에 특수문자까지 출력되는지도 알고 싶습니다.

klyx의 이미지

putchar는 표준라이브러의 함수이며 putch는 표준 라이브러리 함수가 아닙니다.
표준 라이브러리의 입출력 함수들은 모두 내부적으로 버퍼링을 하기 때문에 1글자 찍는다고 1글자가 그대로 찍히는게 아닙니다.
내부에서 버퍼에 글자가 모아지면 한번에 출력하기 때문에 특문을 쪼개서 출력했다고 할지라도 정상적으로 출력될 수 있습니다.
반면에 putch는 버퍼링없이 그대로 한글자씩 출력하기 때문에 쪼개서 찍으면 쪼개진채로 잘못된 코드가 그대로 출력됩니다.

shint의 이미지

윈도우7에서는 putch로 한글이 입력되지 않던데요.

올바른 사용
setlocale (LC_ALL,""); 하면 됩니다.
setlocale (LC_ALL,"korean"); 하면 됩니다.

잘못된 사용
setlocale (LC_ALL, NULL);이 아니네요. ㅇ_ㅇ;;

한글 이라고 입력하면. ÇѱÛ
ㅁㅁㅁㅁ 이라고 입력하면. ¤±¤±¤±

인데. 잘못 사용하는 경우. 저 ¤표시는 문자코드표에 맨 첫번째. 입니다.
http://unicode-table.com/en/
http://unicode-table.com/en/#hangul-syllables
http://www.tamasoft.co.jp/en/general-info/unicode.html
한글은 AC0 번째에 있습니다. 256 * iPage + 위치에 있다고 합니다. 자세한 내용은 WinAPI 책에 유니코드를 보시면. 있습니다.

한마디로. 입력하면 항상 첫 문자 페이지를 표시한다는 말입니다.
그래서. setlocale로 언어설정을 하면. 해당 페이지에 값을 얻어와서 출력 할 수 있는거 같습니다.

자세한 내용은 함수와 책을 참고해 보시기 바랍니다.
¤ ascii code
¤ unicode 검색어

별로 효과 없는 다양한 방법
1. 언어 설정을 영문으로 바꾸기
http://bahndal.egloos.com/465996
2. 일본 게임을 하려면. Microsoft AppLocale을 설치해서 일본어를 보곤 합니다.
3. 워크래프트3 배틀넷에서도 이와 비슷한 현상이 발생합니다.
4. 참고할 만한 링크
http://www.cplusplus.com/reference/clocale/setlocale/
http://blog.naver.com/isentator?Redirect=Log&logNo=10183690935

5. putchar는 #define 되었으니 한번 putch와 함께. 인터넷. 네이버. 구글. C 책을 찾아보세요.

#include <cstdlib>      //devc만 필요
#include <iostream>
 
#include <conio.h>
 
 
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */
#include <locale.h>     /* struct lconv, setlocale, localeconv */
 
using namespace std;
 
int main(int argc, char *argv[])
{
 
//    setlocale (LC_ALL,"");
    setlocale (LC_ALL,"korean");
 
    putch(getch());
    putch(getch());
    putch(getch());
    putch(getch());
    putch(getch());
    putch(getch());
 
 
    system("PAUSE");
    return EXIT_SUCCESS;
}
 

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

sweethmlove2930의 이미지

도움 많이 됐습니다.
답변 주신 두 분 모두 정말 감사드립니다!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.