ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
From what I understand, if I attempt to increment an unsigned int which is already at its max value, it will flip back to zero.
Question: is this guaranteed to happen by the C language? That is, can I count on this happening on all platforms?
(Of course, I recognize that the max value of the int will change from platform to platform...)
when you exceeds the limit the register overflows this will happen on any operating system try factorial 444444
you must take care when you make a program
try using float number because it has 8 or 10 bytes as i remembered and this is a greet resolution
if you want more : there are libraries for encryption that has 1000 bit as i think that will make all things large
I think it is splitting hairs as to whether it is C or the hardware. I believe this is called overflow---Imagine an 8-bit register containing 11111111 (hex FF, dec 255). Add 1, and it tries to go to 100000000, but that "1" is the ninth bit, which--as they say--falls on the floor. With an 8 bit register, you don't know if your count is at 0 or 256 (or 512,768, etc.)
The header files define exactly what different data types are on various HW platforms.
I don't know. A surprisingly large number of similar basic behaviors in boundary conditions are NOT guaranteed by the language standard (there also may be a question of which version of the language standard).
Quote:
That is, can I count on this happening on all platforms?
But you can count on this happening on all platforms even if it is not specified by the language standard.
There is no platform you might port your C code to in which that isn't the standard hardware behavior and no one implementing a compiler would go to the extra effort required to make that not work, even if they also wouldn't go to any extra effort to make it work.
Yes the standard guarantees that the addition of two unsigned integers will silently wrap (not overflow).
Quote:
6.5.2.9
The range of nonnegative values of a signed integer type is a subrange of the
corresponding unsigned integer type, and the representation of the same value in each
type is the same.31) A computation involving unsigned operands can never overflow,
because a result that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value that can be
represented by the resulting type.
This guarantee is used in code for example in a hashing function.
Quote:
there also may be a question of which version of the language standard)
The above quote is from c99 9899 1999
Last edited by Biddle; 03-24-2009 at 08:01 PM.
Reason: added the standard version
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.