[완료]fputs의 출력형식에 대하여... NTBS 방식에 대한 문제

superdma의 이미지

fputs의 사용에 대해서 질문드립니다.

리눅스 에서 각각 2가지 배열.

void main()
{
arr[3]="12";
arr2[3]={1,2};
arr[2]=0;
 
fputs(arr,stdout);
fputs(arr2,stdout);
}

으로 코드를 작성후 출력시

왜 arr2는 출력이 제대로 이루어 지지 않나요?
배열안에 들어있는 값을 봐도 둘 배열 모두 같은 값을 가지고 있는데요,.,

c언어에서는 string을 NTBS로 다루는데 왜 저런 현상이 일어날까요?
감사합니다.

cinsk의 이미지

void main()
{
  arr[3]="12";
  arr2[3]={1,2};
  arr[2]=0;
 
  fputs(arr,stdout);
  fputs(arr2,stdout);
}

물론, 줄여서 쓴 코드이겠지만, 정확한 답변을 바란다면 아래와 같이 쓰길 바랍니다.

int main(void)
{
  char arr[3]="12";
  char arr2[3]={1,2};
  arr[2]=0;
 
  fputs(arr,stdout);
  fputs(arr2,stdout);
 
  return 0;
}

일단, arr와 arr2의 내용은 서로 다릅니다. arr을 초기화한 코드는, 문자 '1'과 '2'를 넣은 것이지만,
arr2를 초기화한 코드는 문자 코드 1과 2에 해당하는 코드를 넣은 것입니다. 따라서 arr을 arr2 형태로 쓴다면 (ASCII character set이라고 가정):

  char arr[3] = { 49, 50, };

이 됩니다.

--
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://www.cinsk.org/cfaqs/

superdma의 이미지

좋은 지적 감사합니다 ^^

두 배열 모두 마지막 배열요소에는 NULL을 가리키는 0값이 들어있는데,
arr 은 fputs에서 정상 출력되지만
arr2는 그렇지 않습니다.

왜 그럴까요?

#include <stdio.h>
 
int main(void)
{
  char arr[3]="12";
  char arr2[3]={1,2};
  int i=0;
 
  arr[2]=0;
 
  printf("value of arr\n");
  for(i=0; i != 3; i++)
  {
 
	printf("%d\n",arr[i]);
  }
 
  printf("value of arr2\n");
  for(i=0; i != 3; i++)
  {
	printf("%d\n",arr2[i]);
  }
 
  printf("fputs of arr\n");
  fputs(arr,stdout);
 
  puts("\n");
 
  printf("fputs of arr2\n");
  fputs(arr2,stdout);
 
  return 0;
}

출력결과

value of arr
49
50
0
value of arr2
1
2
0
fputs of arr
12
 
fputs of arr2
Press any key to continue
phonon의 이미지

입력하신 ASCII code값은 각각 아래와 같습니다.
1: Start of Header
2: Start of Text

제어 문자가 눈에 보일리가 없지요.
원하시는 것이 아래의 것이 아닌가요?
char arr2[3]={'1','2'};

superdma의 이미지

저는 c의 string이 NTBS 방식인데
왜 arr2가 제대로 출력안되는지 의아해 했거든요.
이제야 그 의문이 풀리네요.

답변 해주신 두분 정말 감사합니다.

앞으로 좋은 주제 가지고 자주 들릴께요 ^^

댓글 달기

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