how to do
printf( "%c", 50 ); and also printf( "%d", 'a' );
via
ostream cout ??
thx
Printable View
how to do
printf( "%c", 50 ); and also printf( "%d", 'a' );
via
ostream cout ??
thx
You should really consider using complete sentences. See how easy that was to read? See how easy it was to figure out what I was suggesting? Now let's see if we can get a question out of that mess you've posted above:
"You can use a number, 50, for example, with the %c format specifier, or a letter, and both will print a character. Is there a way to do that with the ostream / cout?"
My goodness that was so much better! I'm almost blown away by my own linguistic ability!
Well, there's one way...Code:cout << static_cast<char>(50);
Quzah.
I' m sorry. I am a Chinese.Quote:
Originally Posted by quzah
thanks for helping
Quzah, don't blast him about his language -- he obviously doesn't speak English natively. Anyway, you (the OP) want to learn about type casting. Quzah showed you the standard C++ way for regular type casts -- static_cast<>(). It accepts a data type as its template parameter and the variable you want to cast as a parameter. So, to convert "int a" into a float, you could say,
Also note that C++ accepts C-style casting, as well. Thus, the following will work (even if it is now outdated):Code:int a;
//...
float f=static_cast<float>(a);
Again, this is outdated and isn't as clear as using a C++-style cast.Code:int a;
//...
float f = (float)a;
The other C++ casts are dynamic_cast<>(), const_cast<>(), and reinterpret_cast<>().
Now, to get to your question:
You need to type cast. For this cast, you would use static_cast (just as Quzah showed). You can do either
orCode:int a = 50;
//...
std::cout << static_cast<char>(a) << std::flush;
//or
std::cout << static_cast<char>(50) << std::flush;
Code:int a = 50;
//...
std::cout << (char)a << std::flush;
//or
std::cout << (char)50 << std::flush;
I hope that answers your question in more depth.
And the other way round, the second example:
std::cout << static_cast<int>('a');
>printf( "%c", 50 );
If you want to avoid casting (always a good idea if you can manage it):
>printf( "%d", 'a' );Code:cout.put(50);
Sadly, the other way around isn't nearly as clean:
Code:cout<< static_cast<int>('a');
Thx all , I will study about these things more deeply,.
Obvious huh? You must be new here. We* get* complete* idiots* here * all the time* who can't or simply won't "speak" coherently. So, how again was it obvious? Oh, that's right, you're one of those idiots who thinks it'll make you bigger in our eyes to blast me. Well go on, give it your best shot.Quote:
Originally Posted by Kybo_Ren
Quzah.
*Go on, it's a link. Knock yourself out.
No, I don't think it will make me "bigger in your eyes".
I have not heard any native speaker say, "how to do.."
when asking how to do anything.
Also, the question was pretty short.
For me, that's a big clue that either
1.) The OP doesn't know English very well
2.) The OP doesn't speak English natively.
In either case, responding with a highly sarcastic, "You should really consider using complete sentences. See how easy that was to read? See how easy it was to figure out what I was suggesting? Now let's see if we can get a question out of that mess you've posted above:" isn't very nice.
Who said anything about "nice"? Who are you, Ms. Manners?
Quzah.