We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,337 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
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.

2
Contributors
1
Reply
10 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
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

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0724 seconds using 2.94MB