Signed & unsigned types

I

Ido Yehieli

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.
 
C

Chris Dollin

Ido said:
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?
 
M

Mark Bluemel

Ido said:
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.
 
I

Ido Yehieli

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.
 
M

Mark Bluemel

Ido said:
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.
 
B

Ben Bacarisse

Ido Yehieli said:
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?

gcc can warn you about format/argument mismatches (provided the format
string is not computed at run-time) but the other issue is not a type
error. Subtracting 2 form an unsigned quantity is a perfectly normal
and correct operation. If the value wraps round according to C's
rules for unsigned arithmetic that, too, is quite correct so there is
no error to detect.
 
I

Ido Yehieli

Subtracting 2 form an unsigned quantity is a perfectly normal
and correct operation. If the value wraps round according to C's
rules for unsigned arithmetic that, too, is quite correct so there is
no error to detect.

Yes, you are right.
I guess what i should do is to use a signed int and add something
like:

assert(int_that_should_be_positive>-1);

to the lines that change its value.

-ido.
 
C

CBFalconer

Ido said:
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/>
 
P

Peter Nilsson

Ido Yehieli said:
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.
 
J

Jack Klein

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
 
M

Mark Bluemel

Jack said:
Ido Yehieli wrote:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top