I have just started learning C and a question has bugged me for a while now. If I write
int i = -1;
unsigned int j = 2;
unsigned int k = -2;
What is the type of integer literal -1 and 2 and -2, and how does it get converted to get stored in signed int and unsigned int?
What is meant by signed integer, is that the property of variable or integer literal too? Like -2 is signed integer and 2 is unsigned integer?
In C99 and C11
If you want to specifies the type of your integer you can use an integer constant:
You can write integer with decimal, octal or hexa representation:
Decimal representation:
By default, the type of -1, 0, 1, etc. is
int,long intorlong long int. The compiler must peak the type that can handle your value:That only work for
signedvalue, if you wantunsignedvalue you need to add u or U:If you want
long intorlong long intbut notint, you can use l or L:You can combine u and l:
Finally, if you want only
long long int, you can use ll or LL:And again you can combine with u.
Octal and Hexadecimal representation:
Without suffix, a integer will match with
int,long int,long long int,unsigned int,unsigned long intandunsigned long long int.u doesn't differ from decimal representation. l or L and ll or LL add unsigned value type.
This is similar to string literals.