Home  >  Q&A  >  body text

C++ macro expansion output problem?

#include <stdio.h>
#include <cmath>
#include <iostream>
using namespace std;
#define pi 3.1415926;
//const double pi=3.1415926;
int main(){
//    printf("%f",(atan2(-1,1)*180/pi));
    cout<< atan2(-1,1)*180/pi;// <<endl;
}

The program is as above. The output is normal under the above circumstances, but after slight modification, the following problems occur.

  1. The program reports an error when adding <<endl. Starting another cout<<endl can output normally.

  2. I tried adding brackets to <<endl, but still got an error.

  3. I tried to use printf to output, but found that it could not be output without adding n.

  4. Everything works fine when using const constants

I don’t understand why macros are not directly replaced? Why is there a problem?

淡淡烟草味淡淡烟草味2736 days ago770

reply all(1)I'll reply

  • 为情所困

    为情所困2017-05-16 13:32:56

    #define pi 3.1415926;

    There is an extra; semicolon

    reply
    0
  • Cancelreply