warning message가 왜 뜨는지..

지나가는새의 이미지

다음과 같은 코드가 헤더파일에 있습니다.

#define  A   1
#define  B   2
#define  C   4

#define LVL A+B+C

#define DBG(x, fmt, args...) { \
    if(x & LVL) printf(fmt, ##args)\
}

*.c 에서는 다음과 같이 호출합니다.

DBG(A, "AAAAAAA\n");

이것을 컴파일 하면(물론 -Wall 포함) 다음과 같은 경고 메세지가 나옵니다.

warning : suggest parentheses around + or - in operand of &

물론 실행은 잘 되지만 좀 찜찜 합니다.

이유를 알 수 있을까요? :oops:

appkr의 이미지

실제로 define을 풀어보면

if( x & 1 + 2 + 4 ) 이런식이 되겠죠.

연산자 우선순위에 따라서 오른쪽의 더하기가 먼저 실행되지만

만약의 경우에 대비해서-_- 괄호로 싸주는게 더 좋죠.

왼쪽의 x도 마찬가지.

#define  A   1
#define  B   2
#define  C   4

#define LVL A+B+C

#define DBG(x, fmt, args...) { \
    if((x) & (LVL)) printf(fmt, ##args)\
}

이런식으로 써주시는게 좋습니다.

지나가는새 wrote:
다음과 같은 코드가 헤더파일에 있습니다.

#define  A   1
#define  B   2
#define  C   4

#define LVL A+B+C

#define DBG(x, fmt, args...) { \
    if(x & LVL) printf(fmt, ##args)\
}

*.c 에서는 다음과 같이 호출합니다.

DBG(A, "AAAAAAA\n");

이것을 컴파일 하면(물론 -Wall 포함) 다음과 같은 경고 메세지가 나옵니다.

warning : suggest parentheses around + or - in operand of &

물론 실행은 잘 되지만 좀 찜찜 합니다.

이유를 알 수 있을까요? :oops:

동쪽하늘의 이미지

#define문을 이용하여 매크로 함수(?)를 작성하는 경우에는
인자처리에 주의를 기울이셔야 합니다.

간단히 예를 들면,
#define MULTI(x, y) (x * y)
가 정의되어 있고,

MULTI(1+2, 3+4)로 사용되는 경우에

1+2 * 3+4 => 1 + (2*3) + 4가 되어버리죠. 우선순위...-_-;;

원하는 것은 (1+2) * (3+4)인데 말입니다.

이런 실수를 막기 위해서는

매크로를 정의할 때, 모든 인자들에 괄호를 묶어주는 것이 바람직합니다.

#define MULTI(x, y) ((x) * (y))
이렇게 말입니다. ^^*);;

위의 코드에서 컴파일러 경고도 이와 같은 이유때문일 것입니다.

즉, & 연산자의 operand인 x와 LVL에 괄호(parentheses)를 쳐주세요~

--
앗~ 한발 늦었네요-_-:oops:

익명 사용자의 이미지

#define DBG(x, fmt, args...) { \
    if((x) & (LVL)) printf(fmt, ##args)\
}

위와 같이 매크로를 선언한 경우

DBG(A, "AAAAAAA\n");

를 전개하면

{ if((x) & (LVL)) printf(fmt, ##args) };

가 됩니다. 그렇지만 중괄호 뒤에 ;가 붙어있어서 컴파일 오류나 경고가 발생하는 문제가 있습니다.

다음과 같이 하면 이를 방지할 수 있습니다:

#define DBG(x, fmt, args...) do { \
    if((x) & (LVL)) printf(fmt, ##args)\
} while (0)

요즘 컴파일러들은 인라인 함수를 지원하므로 매크로보다는 그쪽을 쓰실 것을 권장합니다.

댓글 달기

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