소스 코드 해석 문제 C

kjw7945의 이미지

         int a = 10;
         printf (&"%d is even\0%d is odd"[11*(a&0x1)], a);
다음 위의 두 줄은 수가 짝수인지 아닌지 구분하는 프로그램인데
동작 원리를 모르겠습니다.

다짜고짜 &와 대괄호와 괄호들... 저런 형태의 printf는 처음이라서
잘 모르겠습니다....

void clrscr (void)
{
   printf ("%c[2J%c[1;1H", 27, 27);
}



void gotoxy (short row, short col)
{
   printf ("%c[%d;%dH", 27, row, col);
}

그래서 인터넷을 뒤지던 중에 당므과 비슷한 코드도 발견했습니
다. 모두 한가지를 알면 세가지 모두 알 수 있지 않을까 싶은데..
한가지만이라도 아시는 분은 설명을 부탁드리겠습니다..
정말 궁금하네요...
소리의 이미지

재미있는 코드로군요.

일단 제시하신 첫번째 코드의 printf()를 이해하는 것과 두, 세번째 코드의 printf()를 이해하는 것은 별 관계가 없습니다. 두, 세번째 코드는 어렵게 생각하실필요 없는 아주 일반적인 printf()의 사용입니다.

첫번째 코드는, 다음 세 예제를 이해하신다면 이해하실 수 있으리라 생각됩니다.

char c = ("abcdefg"[4]);
printf("%c", c);
/* 출력결과: e */

const char * str = "abcdefg";
printf("%s", &(str[4]));
/* 출력결과: efg */

printf("%d %d %d %d", (1 & 0x1), (2 & 0x1), (3 & 0x1), (4 & 0x1));
/* 출력결과: 1 0 1 0 */

그리고 마지막 두 코드의 printf() 사용은 char도 정수를 저장하는 데이타 타입임을 이해하시면 됩니다. :)

fox9의 이미지

kjw7945 wrote:
         int a = 10;
         printf (&"%d is even\0%d is odd"[11*(a&0x1)], a);
다음 위의 두 줄은 수가 짝수인지 아닌지 구분하는 프로그램인데
동작 원리를 모르겠습니다.

다짜고짜 &와 대괄호와 괄호들... 저런 형태의 printf는 처음이라서
잘 모르겠습니다....

하나씩 찬찬히 살펴보시면 될듯
우선
a&0x1 을 보시면 a 값과 0x1을 비트 연산해서 이것이 홀수인지 짝수인지 검사를 하겠죠. 사실 홀짝인지는 이미 이곳에서 결정났습니다.
그리고 나서는
11*(a&0x1)을 통해서 11을 곱해 줍니다.
그리고 나서가 printf 문에 들어갈 포멧을 결정하는 부분인데 이부분은
&"%d is even\0%d is odd"[0] <-- 짝수일 경우
&"%d is even\0%d is odd"[11] <-- 홀수일 경우

처럼 계산이 되어있겠죠?
그럼 짝수일 경우는 해당 문자열 주소의 제일 처음값의 주소이기 때문에 결국
"%d is even\0%d is odd" 과 마찬가지 인데 중간에 보면 \0 으로 null일 들어가 있습니다.
그래서 출력은 "... is even" 까지만 되구요
홀수일 경우는 문자열 주소의 11번째 값의 주소라서
처음부터 11을 세어보면
"%d is odd" 요부분의 주소값이 실제로 printf로 전달이 됩니다.
그래서 "... is odd" 라고 출력이 되죠

kjw7945 wrote:

void clrscr (void)
{
   printf ("%c[2J%c[1;1H", 27, 27);
}


void gotoxy (short row, short col)
{
   printf ("%c[%d;%dH", 27, row, col);
}

위의 코드들은 처음의 내용과는 무관한 화면제어코드(정확한 명칭을 잘 ^^) 들의 출력입니다.

댓글 달기

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