473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

what exactly is the difference between signed and unsigned in C?

Hi all,

I am not clear what is the difference between signed and unsigned
in C. Many say, unsigned means only +ve values and 0.

Can I use unsigned int i = -3?
What happens internally ? What conversion happens?
Also on 32 and 64bit machines what happens?

Can anybody explain?

Nov 14 '05 #1
5 20571
<ra********@yahoo.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I am not clear what is the difference between signed and unsigned
in C. Many say, unsigned means only +ve values and 0.

Can I use unsigned int i = -3?
Yes.
What happens internally ? What conversion happens?
Conversion of a value to any unsigned type is conceptually performed by
repeatedly adding or subtracting <maximum value for type>+1 until the result
lies between 0 and <maximum value for type> inclusive.

If UINT_MAX (defined in <limits.h>), the maximum value for an unsigned int,
is 65535, then this:

#include <stdio.h>
int main(void) {
unsigned int i = -3;
printf("%u\n", i);
return 0;
}

will output 65533, since -3 + (65535+1) = 65533.

If UINT_MAX = 4294967295 (common on 32- and 64-bit platforms), the output
will be 4294967293.
Also on 32 and 64bit machines what happens?


The only difference the platform makes to the above conversion is due to the
possible difference in UINT_MAX (assuming the compiler is conforming). Is
that what you were asking?

Alex
Nov 14 '05 #2

<ra********@yahoo.com> wrote
I am not clear what is the difference between signed and unsigned
in C. Many say, unsigned means only +ve values and 0.

Can I use unsigned int i = -3?
What happens internally ? What conversion happens?
Also on 32 and 64bit machines what happens?

Can anybody explain?

Almost all computers use two's complement notation. This means that to
negate an integer you invert the bits, and then add one, losing the overflow
if any. so 1 becomes 11111110 and then 11111111 (all bits set). Zero becomes
11111111 and then overflows to 00000000, which is correct, -0 equals 0.

Internally the processor often has no concept of negative numbers. This is
because adding and throwing away the overflow, in two's complement, is
exactly the same as negating and subtracting.

However the C standard doesn't guarantee that two's complement will be used.
So we have the situation that trying to express -3 as an unsigned integer is
probably going to produce its twos complement value 11111101 (right extend
the ones to sixteen of thirty-two bits instead of eight), which is a
largeish unsigned value. However you cannot be sure of this, and sometimes
you might manage to trigger an internal trap. So from the point of view of
the C stnadard it is actually better to forget all you know about two's
complement values, and just assume that trying to express a negative integer
as an unsigned is illegal.
Nov 14 '05 #3
"Malcolm" <ma*****@55bank.freeserve.co.uk> writes:
[...]
However the C standard doesn't guarantee that two's complement will be used.
So we have the situation that trying to express -3 as an unsigned integer is
probably going to produce its twos complement value 11111101 (right extend
the ones to sixteen of thirty-two bits instead of eight), which is a
largeish unsigned value. However you cannot be sure of this, and sometimes
you might manage to trigger an internal trap. So from the point of view of
the C stnadard it is actually better to forget all you know about two's
complement values, and just assume that trying to express a negative integer
as an unsigned is illegal.


Conversion from signed to unsigned is well-defined in C. It's defined
in terms of values, not in terms of the underlying representation.

Conceptionally, converting a signed value to "unsigned int" is done by
repeatedly or subtracting UINT_MAX+1 until the result is within range.
For example, assuming 16-bit int, (unsigned)-3 is computed by adding
65536 to the mathematical value, yielding 65533.

It happens that this is easy to implement on a 2's-complement system,
but the same requirement applies to other systems (though they're
rare).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
To answer your question a bit more expansively, unsigned and signed
ints are treated differently in terms of what condition flags are used
to evaluate them per C relational operators.

In plain english this means that a>b may have different answers
depending on whether a and b are signed, unsigned, or mixed.

Nov 14 '05 #5
Novitas wrote:
To answer your question a bit more expansively, unsigned and signed
ints are treated differently in terms of what condition flags are used to evaluate them per C relational operators.
What are "condition flags".
In plain english this means that a>b may have different answers
depending on whether a and b are signed, unsigned, or mixed.


That's not very expansive :)
If a and b are both signed, or both unsigned, there is no confusion.
If one is signed and one isn't, then the signed one is promoted to
unsigned (via the process described by Keith Thompson in this thread).

The same goes for the other arithmetic binary operators
(but not the bitwise binary operators).

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ling | last post by:
Hi, I'm trying to do fscanf from a file with integers. I've tried using both %d and %i hoping that I would figure out their difference. When I checked the man pages, the only difference I...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
4
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
35
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : ...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
18
by: Martin Jørgensen | last post by:
Hi, Today I got a really strange problem... I've made myself a data-file and I read in data from that file.... When I read something like this line: 03 04 05, 00 04 01, 05 03 07, 08 03...
38
by: DaVinci | last post by:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define SECONDS_PER_YEAR (60*60*24*365)UL int main() { unsigned long int u_i = SECONDS_PER_YEAR; printf("%lu\n",u_i);
30
by: iskeletor | last post by:
in a program it is passing like that: #define INFINITY 0xFFFFFFF it is a ASCII code of what? or is it a address that a pointer hold? so adress of what?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.