Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ struct data member

Tags:

c++

linux

I am working in C++, Linux and I encounter the problem as following:

struct testing{
uint8_t a;
uint16_t b;
char c;
int8_t d;

};

testing t;

t.a = 1;
t.b = 6;
t.c = 'c';
t.d = 4;
cout << "Value of t.a >>" << t.a << endl;
cout << "Value of t.b >>" << t.b << endl;
cout << "Value of t.c >>" << t.c << endl;
cout << "Value of t.d >>" << t.d << endl;

The output on my console is:

Value of t.a >>
Value of t.b >>6
Value of t.c >>c
Value of t.d >>

It seems like the t.a and t.d is missing for the int8_t and uint8_t type. Why is it so?

Thanks.

like image 687
Leslieg Avatar asked Nov 18 '10 11:11

Leslieg


1 Answers

The int8_t and uint8_t types are probably defined as char and unsigned char. The stream << operator is going to output them as characters. Since they're set to 1 and 4 respectively, which are control characters and not printing characters, nothing will be visible on the console. Try setting them to 65 and 66 ('A' and 'B') and see what happens.

EDIT: to print out a numeric value instead of a character, you will need to cast them to an appropriate type:

cout << static_cast<unsigned int>(t.a) << endl;
like image 83
Ferruccio Avatar answered Sep 27 '22 23:09

Ferruccio