0

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.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Tumlee
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.