배열에 특수문자는 어떻게 넣나요?

wltjd666의 이미지

int main()
{
char Board[3];

Board[0]='★';
Board[1]='☆';
Board[2]='+';

printf("%c",Board[0]);
printf("%c",Board[1]);
printf("%c",Board[2]);
}

이렇게 하면 +는 출력되지만 별 모양은 이상한 값으로 출력되는데
별 모양도 배열에 추가할려면 어떻게 해야하나요?

shint의 이미지


문자열 끝을 0x00 으로 채워줘야 하지만. 그냥 되네요.
확인해보셔야 할겁니다.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
	char Board[10];
	Board[0] = '★' >> 8; Board[1] = '★';
	Board[2] = '☆' >> 8; Board[3] = '☆';
	Board[4] = '+'  >> 8; Board[5] = '+';
	printf("%c%c\n", '★' >> 8, '★');
	printf("%c%c\n", 'ㄱ' >> 8, 'ㄱ');
	printf("%c%c\n", '가' >> 8, '가');
	printf("%c%c\n", Board[0], Board[1]);
	printf("%c%c\n", Board[2], Board[3]);
	printf("%c%c\n", Board[4], Board[5]);
	return 0;
}
 
★
ㄱ
가
★
☆
+
Press any key to continue . . .

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

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

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

Prentice의 이미지

★ 검색해보면 U+2605, 즉 유니코드에서 16진수로 2605번째(?), 10진수로 9733번째(?) 문자로 뜹니다.

근데 signed char로 표현할 수 있는 범위는 -128부터 127까지죠.

 의 이미지

C언어에서 ★, ☆와 같은 문자들은 대개 멀티바이트 문자(Multibyte character)에 속합니다.
말인즉슨 분명히 문자 하나이지만 char 하나, 그러니까 1바이트에 담기지 않는다는 얘기지요.

질문자님의 프로그램은 일단 이런 식으로 고쳐볼 수 있습니다.

#include <stdio.h>
 
int main(void) {
	const char *Board[3];
	Board[0] = "★";
	Board[1] = "☆";
	Board[2] = "+";
	printf("%s", Board[0]);
	printf("%s", Board[1]);
	printf("%s", Board[2]);
}

분명히 문자 하나를 다루는데 문자열을 다루듯 해야 하는 불편함을 저한테 따지셔봤자 소용 없습니다. :(
물론 극복할 방법이 있긴 한데... 산 넘어 산입니다.

#include <stdio.h>
#include <stddef.h>
#include <locale.h>
 
int main(void) {
	wchar_t Board[3];
	setlocale(LC_ALL, "");
	Board[0] = L'★';
	Board[1] = L'☆';
	Board[2] = L'+';
	printf("%lc", Board[0]);
	printf("%lc", Board[1]);
	printf("%lc", Board[2]);
}

wchar_t라는 녀석을 쓰면 소위 wide character라는 걸 다룰 수가 있어요.
문제는 그걸 출력할 때인데, C11 표준을 읽어보면 printf가 lc 포맷 지정자를 받으면 wide char를 받아 multibyte char로 변환한답니다. 그 과정에서 로케일(locale)이라는 게 개입되는데 솔직히 저는 여전히 로케일이 뭔지 잘 몰라요. 그냥 적절한 순간에 적절히 마법 주문처럼 setlocale(LC_ALL, "");라고 한 번 외쳐 주면 잘 될 수 있다더라... 정도만 알고 지냅니다.

마음 잡고 공부해도 이해가 안 될 정도로 어려운 개념일 거라고 생각하지는 않는데, 안 그래도 배울 거 많은 세상에 솔직히 귀찮네요. 언제고 제가 진지한 프로그래밍을 하다가 저 마법 주문이 안 먹히는 순간이 오면 그 때 공부하려고요.

아무튼 답변은 이 정도면 충분하겠군요.

jehnpark의 이미지

윗분이 설명을 잘못하긴거같은데
windows에서 console은 보통 multibyte로 합니다만
글쓴이분은 linux환경이신거같네요 linux에서는 utf-8환경에서 하는 경우가 대부분이라 알고있습니다.
별모양은 2byte짜리이기 때문에 1바이트씩 분할해서 출력하면 정상출력 됩니다.

 의 이미지

multibyte character와 wide character는 C언어 표준에서 사용하는 용어입니다.

C11 std wrote:
3. Terms, definitions, and symbols
3.7.2
1 multibyte character
sequence of one or more bytes representing a member of the extended character set of
either the source or the execution environment
2 NOTE The extended character set is a superset of the basic character set.

3.7.3
1 wide character
value representable by an object of type wchar_t, capable of representing any character
in the current locale

따라서 Linux도 예외가 아니며, utf-8로 2바이트 인코딩된 ★, ☆는 multibyte character에 속합니다.

파이썬3의 이미지

사알짝 비틀어봤어요~

/* -*- coding: utf-8 -*- */
 
#include <stdio.h>
 
int main()
{
	char str[3][4] = {
		"★",
		"☆",
		"+",
	};
 
	printf("%s\n", str[0]); /* 검은별 */
	printf("%s\n", str[1]); /* 하얀별 */
        printf("%s\n", str[2]); /* 더하기 부호 */
 
	return 0;
}
 
/* 편집: GNU Emacs 26.3 (Ubuntu 18.04) */

[우분투 18.04 파여폭스 나비에서 적었어요~]

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.