Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the integer constant's default type signed or unsigned?

Tags:

Is the integer constant's default type signed or unsigned? such as 0x80000000, how can I to decide to use it as a signed integer constant or unsigned integer constant without any suffix?

If it is a signed integer constant, how to explain following case?

printf("0x80000000>>3 : %x\n", 0x80000000>>3); 

output:

0x80000000>>3 : 10000000 

The below case can indicate my platform uses arithmetic bitwise shift, not logic bitwise shift:

int n = 0x80000000;  printf("n>>3: %x\n", n>>3); 

output:

n>>3: f0000000 
like image 635
Victor S Avatar asked Jul 03 '12 12:07

Victor S


People also ask

Are integers signed or unsigned by default?

An int is signed by default, meaning it can represent both positive and negative values. An unsigned is an integer that can never be negative.

Does c default to signed or unsigned?

The C and C++ standards allows the character type char to be signed or unsigned, depending on the platform and compiler. Most systems, including x86 GNU/Linux and Microsoft Windows, use signed char , but those based on PowerPC and ARM processors typically use unsigned char .

Is int signed or unsigned by default in Java?

An int is always signed in Java, but nothing prevents you from viewing an int simply as 32 bits and interpret those bits as a value between 0 and 264.

Is int in C unsigned or signed?

The int type in C is a signed integer, which means it can represent both negative and positive numbers. This is in contrast to an unsigned integer (which can be used by declaring a variable unsigned int), which can only represent positive numbers.


1 Answers

C has different rules for decimal, octal and hexadecimal constants.

For decimal, it is the first type the value can fit in: int, long, long long

For hexadecimal, it is the first type the value can fit in: int, unsigned int, long, unsigned long, long long, unsigned long long

For example on a system with 32-bit int and unsigned int: 0x80000000 is unsigned int.

Note that for decimal constants, C90 had different rules (but rules didn't change for hexadecimal constants).

like image 89
ouah Avatar answered Oct 26 '22 00:10

ouah