473,406 Members | 2,377 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,406 software developers and data experts.

Signed & unsigned types

Hi,
from what i've read (http://tigcc.ticalc.org/doc/
keywords.html#short) and unsigned int should normally be in the range
0 to 65535. However, compiling and running this program:

#include <stdio.h>

int main(){
unsigned int y=1;
y-=2;
printf("y=%i\n",y);
}

produces "y=-1". shouldn't it be either 65535 or some error?

-ido.
Dec 5 '07 #1
9 2295
Ido Yehieli wrote:
Hi,
from what i've read (http://tigcc.ticalc.org/doc/
keywords.html#short) and unsigned int should normally be in the range
0 to 65535.
No, it should normally be in the range that the implementation specifies.
Not all implementations have 16-bit [[un]signed] ints.
However, compiling and running this program:

#include <stdio.h>

int main(){
unsigned int y=1;
y-=2;
printf("y=%i\n",y);
}

produces "y=-1". shouldn't it be either 65535 or some error?
Shouldn't you use an unsigned format to print an unsigned integer?

--
Chris "rhetorical" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Dec 5 '07 #2
Ido Yehieli wrote:
Hi,
from what i've read (http://tigcc.ticalc.org/doc/
keywords.html#short) and unsigned int should normally be in the range
0 to 65535. However, compiling and running this program:

#include <stdio.h>

int main(){
unsigned int y=1;
y-=2;
printf("y=%i\n",y);
What does the %i mask in printf() expect?

What mask should you specify for unsigned data?
}

produces "y=-1". shouldn't it be either 65535 or some error?

You lied to printf(), by saying you were providing an "int" argument,
but actually providing an "unsigned int". You get undefined behaviour.

Some compilers may give you a warning, but you can't rely on that.
Dec 5 '07 #3
On Dec 5, 11:25 am, Chris Dollin <chris.dol...@hp.comwrote:
Shouldn't you use an unsigned format to print an unsigned integer?
Oh right... thanks.
Dec 5 '07 #4
On Dec 5, 11:31 am, Mark Bluemel <mark_blue...@pobox.comwrote:
Some compilers may give you a warning, but you can't rely on that.
Do you know of a compiler that does strict type checking?
So that the code above would produce 2 errors or warnings, one for the
%i and one for making an unsigned int negative?

-ido.
Dec 5 '07 #5
Ido Yehieli wrote:
On Dec 5, 11:31 am, Mark Bluemel <mark_blue...@pobox.comwrote:
>Some compilers may give you a warning, but you can't rely on that.

Do you know of a compiler that does strict type checking?
So that the code above would produce 2 errors or warnings, one for the
%i and one for making an unsigned int negative?
No I don't. GCC is, to some extent, capable of checking printf usage
but it didn't complain about your source.
Dec 5 '07 #6
Ido Yehieli wrote:
Mark Bluemel <mark_blue...@pobox.comwrote:
>Some compilers may give you a warning, but you can't rely on that.

Do you know of a compiler that does strict type checking?
So that the code above would produce 2 errors or warnings, one for
the %i and one for making an unsigned int negative?
What 'code above'? See my sig.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>

--
Posted via a free Usenet account from http://www.teranews.com

Dec 5 '07 #7
Ido Yehieli <Ido.Yehi...@gmail.comwrote:
I guess what i should do is to use a signed int and
add something like:

assert(int_that_should_be_positive>-1);
Not if int_that_should_be_positive is an unsigned int or
higher. In a comparison of unsigned int and int values,
the int (in this case -1) will be converted to an unsigned
int.

Some compilers will actually warn you that the condition
may always be false in that particular case!

If you want to test for non-negative integers, use...

int_that_should_be_positive >= 0

Again, you may get a warning that this condition is always
true for unsigned integer types, but you can ignore that
warning.

--
Peter
Dec 5 '07 #8
On Wed, 05 Dec 2007 10:31:46 +0000, Mark Bluemel
<ma**********@pobox.comwrote in comp.lang.c:
Ido Yehieli wrote:
Hi,
from what i've read (http://tigcc.ticalc.org/doc/
keywords.html#short) and unsigned int should normally be in the range
0 to 65535. However, compiling and running this program:

#include <stdio.h>

int main(){
unsigned int y=1;
y-=2;
printf("y=%i\n",y);

What does the %i mask in printf() expect?
I don't know, what do you mean by mask?
What mask should you specify for unsigned data?
Again, what mask? Most people associate the term "mask" with a
pattern, hopefully of an unsigned integer type, used in bitwise
operations.

The C standard uses the term "conversion specifier". It's ever so
much better than mask, don't you think?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Dec 6 '07 #9
Jack Klein wrote:
On Wed, 05 Dec 2007 10:31:46 +0000, Mark Bluemel
<ma**********@pobox.comwrote in comp.lang.c:
>Ido Yehieli wrote:
>> printf("y=%i\n",y);
What does the %i mask in printf() expect?

I don't know, what do you mean by mask?
[snip]
The C standard uses the term "conversion specifier". It's ever so
much better than mask, don't you think?
OK point taken...
Dec 6 '07 #10

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

Similar topics

8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
27
by: Marcus Kwok | last post by:
I am getting warnings when comparing a (regular) int to the value returned from std::vector.size() in code similar to the following: int i = //stuff if (i >= vec.size()) The compiler gives...
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...
22
by: juanitofoo | last post by:
Hello, I've just switched to gcc 4 and I came across a bunch of warnings that I can't fix. Example: #include <stdio.h> int main() { signed char *p = "Hola";
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...
11
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
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...

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.