c++ - #define confusion with {} and declaration -
i got c++ program school test.
#include<iostream.h> #define convert(p,q) p+2*q void main() { int a,b,result; cin>>a>>b; result=convert(a,b); cout<<result; }
this works correctly.
when put p+2*q in {}, gives me error : "expression syntax in function main()"
now when declare result before output, this:
int result=convert(a,b);
it works. why , why not?
avoid using macros if have trouble understanding compiler complains about. code looks when macro expanded, when put {}
result={a+2*b}
and looks declaration on same line
int result={a+2*b}
before c++11 standard, former syntax error. since c++11, copy-list-initialization of temporary (see syntax labeled (10) ).
the latter aggregate initialization.
Comments
Post a Comment