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

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -