Hi all,
I am just going through C++ basics now and I read that enum types cannot be input directly with cout and cin and such.And also that type coercion from int to enum is not allowed.But when i do this,

enum SumEnum{ENUM1,ENUM2,ENUM3}
  SumEnum VarEnum;
  scanf("%d",&VarEnum);
  printf("%d",VarEnum);

It happens perfectly.printf is ok because enum to int coercion is allowed,but how come scanf works if int to enum is not allowed?

Thanks in advance.

More than likely, it's just because printf() and scanf() are functions that have been around since before C++ and if you were to disallow that suddenly, it would cause older code to no longer compile correctly.

Plain vanilla C didn't have all the type-safety that C++ had, and IIRC enum-int conversions were allowed back then.

My only other explanation would be that printf() and scanf() are pretty hacky functions that can't even check if the correct number of arguments are being passed to them. It's entirely possible that they can interperet enums and ints just because they don't know any better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.