473,785 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 20604
<ra********@yah oo.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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********@yah oo.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.u k> 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_Keit h) 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
8899
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 could tell was d: Matches an optionally signed decimal integer ... i: Matches an optionally signed integer ...
9
4178
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 signed char which is -256. Does it means that we can assign the same ASCII value to both signed and unsigned. That means the ASCII value can be represented with a type of signed char and also unsigned char? For example int main(void) {
10
15664
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
4
1042
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 etc and their implementation.the way compiler treats them and the scenarios where one
35
5477
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 : %d\n",sizeof(unsigned char)); printf("short : %d\n",sizeof(short)); printf("unsigned short : %d\n",sizeof(unsigned short)); printf("int : %d\n",sizeof(int));
20
5365
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 <stdlib.h>
18
11854
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 00, 09 06 03 ... etc. with something like scanf("%i %i %i, ", &var1, &var2, &var3);
38
2523
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
39457
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
10324
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.