Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 8 years ago

c語言問題求詳觧

下列以C程式語言撰寫之程式執行後產生之輸出為何?

#define PLUS(a, b) a-b

#define MINUS(a, b) a+b

#include <stdio.h>

main()

{ int ml=5, m2=3;

ml = PLUS(m1+m2, m1-m2)*MINUS(m2-m1, m2+m1);

printf(“%d\n”, ml);

}

A-3 B1 C36

我自己的想法

(錯誤算式)

Plus(8,2) minus(-2,8)

a-b=6 a+b=6

6*6=36

請高手說明正確算式謝謝~

3 Answers

Rating
  • 卸貨
    Lv 5
    8 years ago
    Favorite Answer

    ml = PLUS(m1+m2, m1-m2)*MINUS(m2-m1, m2+m1);

    = PLUS(a,b)*MINUS(c,d); // a --> m1+m2, b --> m1-m2, c --> m2-m1, d --> m2+m1

    = a-b*c+d;

    = m1+m2-b*c+d;

    = m1+m2-m1-m2*c+d;

    = m1+m2-m1-m2*m2-m1+d;

    = m1+m2-m1-m2*m2-m1+m2+m1; // m1=5, m2=3

    = 5+3-5-3*3-5+3+5

    = -3

    Macros 只做單純的文字代換動作,不負責其它。

  • Anonymous
    8 years ago

    要用這東西,切記要照優先權把括號全部加上去,不然很容易發生預料以外的情況

  • 8 years ago

    #define PLUS(a, b) a-b // bad

    #define PLUS(a,b) ((a)-(b)) // good

    #define MINUS(a, b) a+b // bad

    #define MINUS(a,b) ((a)+(b)) // good

Still have questions? Get your answers by asking now.