Member Avatar for Nathan_6

Hi everyone.

I was reading some source code and I cam across the line

cout << "\nu converted to upper case is " << static_cast<char> ( toupper('u') ) << endl;

Why would the programmer use static_cast<char> in conjunction with toupper to print 'U' instead of just typing cout << "\nu converted to upper case is " << toupper('u') << endl;

Thanks in advance for your input.

There is no real reason other than "showing off". That said, toupper(int ch) returns an int, so they may have wanted to force it to return a char, which they could have done with a simple (char) cast which is effectively what static_cast<char> does. IE, a waste of space, and it makes the code more difficult to read and parse.

Member Avatar for Nathan_6

are there any other advantages to using static_cast<char> in any other situations?

and is it more proper to use (typeyouwanttocastavariable) [variable to be casted] or the "static_cast" or other types of similar casting functions in c++?

thank you in advance for the input.

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.