# Thread: Some C questions

1. ## Some C questions

Hello there. I was reading this book (C premire) and came up with this

Code:
`The ranking of types, from highest to lowest, is long double, double, float, unsigned long long, long long, unsigned long, long, unsigned int, and int. One possible exception is when long and int are the same size, in which case unsigned int outranks long. The short and char types don't appear in this list because they would have been already promoted to int or perhaps unsigned int.`
Remembering in one of the threads where user "cas" mentioned this :

Code:
```255 / -1 as an unsigned char:
1111 1111
Converted to int:
0000 0000 0000 0000 0000 0000 1111 1111

The above is only -1 if the least significant 8 bits are used.  As an int (signed or unsigned) it's clearly 255.

4294967295 / -1 as an unsigned int:
1111 1111 1111 1111 1111 1111 1111 1111
Converted to int:
1111 1111 1111 1111 1111 1111 1111 1111

This is ambiguous as an int: unsigned, it's 4294967295.  Signed, it's -1.```
Therefore having this code :

Code:
```signed char x = -5;
int y=5;
printf("%d",x+y);```
x should be converted to int

5 in the binary form can be written as a char by 0000 0101
=> x=-5 is equal to 1111 1011 (2's compliment)

after converting to an int y will be represented by

0000 0000 0000 0000 0000 0000 1111 1011
+ 0000 0000 0000 0000 0000 0000 0000 0101 (adding 5)

0000 0000 0000 0000 0000 0001 0000 0000

which is 256.

But how come it is printing 0, which is the logical answer that i should get ?

2. Because x is converted to an int too, and that gives you 1111 1111 1111 1111 1111 1111 1111 1011.

3. Because x is converted to an int too, and that gives you 1111 1111 1111 1111 1111 1111 1111 1011.
x is being converted to an int after having 2's complement

0000 0000 0000 0000 0000 0000 1111 1011

or i made a mistake .. is it changed to an int before taking it's 2's compliment ?

And another Question :

Can anyone advice me some tutorials or papers about API and windows programming in C and about how should i start socket programming in C and under windows ?

4. "Convert to an int" does not mean "have zeroes put in front of it". "Convert to an int" means "build an int that has the same value as". The int that has the same value as -5 is 1111 1111 1111 1111 1111 1111 1111 1011.

5. Simply put, a signed integer can hold the range of a signed char, so its value is preserved once promoted. And +5 + (-5) = 0.
And please don't put any text in code tags. If anything, use quotes.

6. As for ...

Can anyone advice me some tutorials or papers about API and windows programming in C and about how should i start socket programming in C and under windows ?

7. Originally Posted by BlaX
Can anyone advice me some tutorials or papers about API and windows programming in C and about how should i start socket programming in C and under windows ?
Not a windows user myself but pretty sure the word you want for google here is "winsock", maybe it is even called "The Winsock API".

A lot of people recommend this:

Beej's Guide to Network Programming

which the keyword here is "network" and not "socket", but since they refer to the same level of the OSI model it's the same deal.

Popular pages Recent additions