서로 결과가 틀린이유를 알고 싶습니다.

Geniys의 이미지

#include <stdio.h>
#include <stdlib.h>

#define WON 1000
#define PRICE(QTY, UNIT_PRICE) (QTY * UNIT_PRICE / WON)

int PRICE1(int qty, int unit)
{
	return  qty * unit / WON;
}

int main()
{
  int qty1 = 10, qty2 = 5;
  int unit_price = 10000;
  int tot;
  tot = PRICE(qty1 + qty2, unit_price);

  printf("tot = %d\n", tot);

  return 0;
}

위 코드를 #define문 함수[PRICE]를 바꿔보면 결과가 "60"이 나오고,
일반 함수[PRICE1]으로 하면 제가 생각하듯이 "150"이 나오는데 왜 이런차이가 생기는지 궁금합니다.
도대체 60이 어떻게 나오는지가 이해가 안돼서요.

그럼 좋은하루 되세요 ^^

doldori의 이미지

매크로를 전개해보면 금방 아실 텐데요.
매크로를 정의할 때는 다음과 같이 괄호를 쓰는 것이 좋습니다.
#define PRICE(QTY, UNIT_PRICE) ((QTY) * (UNIT_PRICE) / WON)

Geniys의 이미지

괄호를 안치면 어떻게 해석되는지도 궁금합니다.

15 * 10000 / 1000

위 식이 어떻게 해석이 되서 60이 나오게 된건지 알수가 없내요..

============================
A watched pot never boils

Fe.head의 이미지

메크로 변환

PRINCE(qty1 + qty2, unit_prince);
--> QTY * UNIT / WON
--> qty1 + qty2 * unit_prince / 1000
--> 10 + 5 * 10000 / 1000
--> 10 + 50000 / 1000
--> 10 + 50
--> 60

일반 코드

int PRICE1(int qty, int unit)
{
   return  qty * unit / WON;
}

PRINCE1(10 + 5, 10000);
--> PRINCE1(15, 10000);
--> 15 * 10000 / 1000
--> 150

고작 블로킹 하나, 고작 25점 중에 1점, 고작 부활동
"만약 그 순간이 온다면 그때가 네가 배구에 빠지는 순간이야"

doldori의 이미지

15가 아니죠. 원래 정의한 매크로로 PRICE(qty1 + qty2, unit_price) 를 전개하면
qty1 + qty2 * unit_price / WON
가 됩니다. 10 + 5 * 10000 / 1000 == 60 맞죠?
컴파일러는 죄 없습니다. ^^;

monpetit의 이미지

괄호가 없으면 10 + 5 * 10000 / 1000 이 되겠네요.
계산의 결과는 당연히... =)

Fe.head의 이미지

괄호 친경우 메크로 변환

#define PRICE(QTY, UNIT_PRICE) ((QTY) * (UNIT_PRICE) /( WON)) 
PRINCE(qty1 + qty2, unit_prince);
--> (QTY) * (UNIT_PRICE) /( WON)
--> (qty1 + qty2) * (unit_prince) / (1000)
--> (10 + 5) * (10000) / (1000)
--> 15 * 10000 / 1000
--> 150

고작 블로킹 하나, 고작 25점 중에 1점, 고작 부활동
"만약 그 순간이 온다면 그때가 네가 배구에 빠지는 순간이야"

Geniys의 이미지

답변 감사합니다 ^^

괄호를 꼭치도록 하겠습니다~

============================
A watched pot never boils

익명 사용자의 이미지

저런경우 '틀린' 이라고 쓰는것은 옳지 않습니다. '다른' 이라고 쓰셔야합니다.

너와 내가 다른거지... 틀린건 아니지요... ㅎㅎ

댓글 달기

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