What happens when a char is set too large to be inserted in bytes?

Say I have a char pointer:

    char * cp;
    int val2 = 2;
    cp = &val2;
    *cp = 1234;

What happens because the value 1234 is too large to fit in 1 byte? Will it just overflow and cause the wrong values, or will it somehow store the correct value for several bytes?

+3
source share
1 answer

B *cp = 1234;, *cprefers to only one byte, the first (lowest address) byte val2.

In assignment, the value of the right-hand side is converted to the type of the left-hand side. Thus, 1234 is converted to char.

. , C, 1234 , char. ( C char , .) , char .

char , : 1234 char ( 255, 256). 1234 210, , *cp.

char 1234 char, . C -46.

, cp cp = &val2;, val2. val2. C , ( ), C , ( ). ( , , .)

C , , , C , , int, , C - . .

, , char - , . , , short char, short *sp = &val2; *sp = 0;, , undefined.

+8

All Articles