Start New Discussion Reply to this Discussion enum type IO operations with printf,scanf
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.
Related Article: printf : weird++
is a C++ discussion thread by tubby123 that has 11 replies and was last updated 1 year ago.
IndianaRonaldo
Light Poster
45 posts since Jan 2011
Reputation Points: 7
Solved Threads: 3
Skill Endorsements: 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.
Tumlee
Junior Poster
172 posts since Oct 2011
Reputation Points: 84
Solved Threads: 33
Skill Endorsements: 3
© 2013 DaniWeb® LLC
Page rendered in 0.0724 seconds
using 2.94MB