简体   繁体   中英

unexpected output when passing unsigned char to cout

There's probably a silly error in my code. I have defined the following variables:

unsigned char   uEngines;
unsigned short  uActiveEngines
unsigned short  uDed
.... 

and a few others.

Elsewhere in the code, I tried to print the structure in the gdb and got the following .

$6 = {uEngines  = 12 '\f', uActiveEngines = 4095 .....

1) I am trying to output the uEngines value using cout , but it's only outputting a blank space:

cout <<strVariable->uEngines;

2) what does '\\f' mean in the gdb output ?

Am I doing something wrong with data type ?

It's a formfeed, one of the non-printable characters in the ASCII character set below space.

It's meant to do a "page advance" operation on whatever device you send it to, assuming that the device supports it.

12 is the decimal value as shown below:

Char  Dec  Hex  Control Action
----  ---  ---  --------------
NUL     0    0  Null character
SOH     1    1  Start of heading, = console interrupt
STX     2    2  Start of text, maintenance mode on HP console
ETX     3    3  End of text
EOT     4    4  End of transmission, not the same as ETB
ENQ     5    5  Enquiry, goes with ACK; old HP flow control
ACK     6    6  Acknowledge, clears ENQ logon hand
BEL     7    7  Bell, rings the bell...
BS      8    8  Backspace, works on HP terminals/computers
HT      9    9  Horizontal tab, move to next tab stop
LF     10    a  Line Feed
VT     11    b  Vertical tab
FF     12    c  Form Feed, page eject
CR     13    d  Carriage Return
:
:

Because it's a char , std::cout << uEngines; will output it as a character rather than an integral value. If you want it as in integral value, cast it to one:

std::cout << (int)uEngines;

The answer to both 1) and 2) is that it's a char , so it's being interpreted as an ASCII character.

1) Cast it to an int to print the numeric value.

cout << (int) myStruct.uEngines;

2) This is a harmless quirk of how gdb displays the value, and can be ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM