Type Conversion

Rules

  • assignment: right type converts to left type, with or without sign expression
  • compare & binary operator: converts low to high (unsigned higher than signed)

Char to Int

char c = 0xFF;
int i = c;
cout << i;

It's machine dependent. There are two situations. (without sign extension)
1) It's always converted to a positive number.

255

2) It can be both positive or negative number, and depend on MSB of c. (with sign extension)

-1

c = 0xFF = -1 —-> i = -1 = 0xFFFF

Int to Char

directly truncate

Pointer Conversion

int i = 10;
char *c;
c = &i;
-> convert pointer of int to pointer of char
-> i + 1 = i + sizeof(int)
-> c + 1 = c + sizeof(char)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License