With printf, you specify the output type like this

unsigned char a;
printf("%u", a);

but with cout,

cout << a;

there is no type required. Do you have to cast the variable in order to see a reasonable output

cout << (int)a;

or something like that? How do you know which type to convert to? Ie when I do cout << a; I get some crazy ascii character, but what I wanted to see was a value between 0 and 255.



Because you did not initialize it..Example..

// declare n init
unsigned char a='a';
// display

no, it is initialized (i guess I should have written that). I get the value from a function, ie.

unsigned char a[3];
glReadPixels(a); // puts data into a

cout << a;

It outputs a crazy character - I would like to see the value (0-255).


std::cout<<static_cast<int>(a[0]); // n next

a is not a char, is an array of char. you have to print its component one at a time.

bah I mis-typed again, I was trying to do

cout << a[0];


cout << a;

With the correct version, I do infact have to cast it as an int.

what is the difference between


cout << (int)a[0];




The difference between (int) and static_cast<int> is really the limitation
of static_cast<>

I think it is easier to see an example and then discuss it.

char A('x');
double X[50];

int i=static_cast<int>(A);      // OK
int k=(int) A;
long int j = (int) X;
long int k = static_cast<long int>(X);   // FAILS AT COMPILE
long int z=reinterpret_cast<long int>(X); // OK

Basically, static_cast will only allow conversions between related types.
Note, the very subtle error in the code above (int) X will work BUT loses precision so the full address is not converted [ok I know it is machine dependent.. but 64 bit/gcc is does], and that alone is worth the extra typing.