473,385 Members | 1,730 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,385 software developers and data experts.

two questions

Hi NG,

I have the next program:

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned int a = UINT_MAX;

printf("%u\n", a);
printf("%u\n", ++a);

return 0;
}

When I compile and run this the result is:
4294967295
0

This doesn't surprise me because I overflowed a. But I was wondering if
the standard defines this behaviour, or if it depends on the cpu used.
All cpu's I know (not that many) will just overflow when you add one to
the maximum value for that particular cpu, but I don't think it is
unreasonable to design a cpu which just leaves the value at UINT_MAX.

Another question I have is about the assert macro. Is it commonly seen
as a good practice to use this or is it better/safer to just always do a
check yourself and return if something is wrong?

Thanks,
Mark

Nov 14 '05 #1
5 1154
Capstar <sp***@eg.homeip.net> scribbled the following:
Hi NG, I have the next program: #include <stdio.h>
#include <limits.h> int main(void)
{
unsigned int a = UINT_MAX; printf("%u\n", a);
printf("%u\n", ++a); return 0;
} When I compile and run this the result is:
4294967295
0 This doesn't surprise me because I overflowed a. But I was wondering if
the standard defines this behaviour, or if it depends on the cpu used.
All cpu's I know (not that many) will just overflow when you add one to
the maximum value for that particular cpu, but I don't think it is
unreasonable to design a cpu which just leaves the value at UINT_MAX.


The standard defines that unsigned types will safely wrap around when
overflowed. Signed types don't have to, they can do anything at all

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Nov 14 '05 #2
Capstar <sp***@eg.homeip.net> wrote:
unsigned int a = UINT_MAX;

printf("%u\n", a);
printf("%u\n", ++a); When I compile and run this the result is:
4294967295
0

This doesn't surprise me because I overflowed a. But I was wondering if
the standard defines this behaviour,
For unsigned types, yes. Not for signed types or non-integers.
Another question I have is about the assert macro. Is it commonly seen
as a good practice to use this or is it better/safer to just always do a
check yourself and return if something is wrong?


If the answer depends on data you get from your users (even if
indirectly, for example to a file), check, and report _nicely_. Don't
bluntly abort just because someone made a typo.
If the answer depends _only_ on data you have generated yourself, or
which might have come from your user but which you have already
validated and should be correct - in other words, if the error in
question is a programming error, not a data error - then you can use
assert(). Even so, it's never nice to simply lose your user half an hour
of data entered because _you_ made a programming error, so be careful.

Richard
Nov 14 '05 #3
Capstar wrote:
Hi NG,

I have the next program:

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned int a = UINT_MAX;

printf("%u\n", a);
printf("%u\n", ++a);

return 0;
}

When I compile and run this the result is:
4294967295
0

This doesn't surprise me because I overflowed a. But I was wondering if
the standard defines this behaviour, or if it depends on the cpu used.
All cpu's I know (not that many) will just overflow when you add one to
the maximum value for that particular cpu, but I don't think it is
unreasonable to design a cpu which just leaves the value at UINT_MAX.


As already pointed out, behavior such as this is actually mandated by
the standard. However, it may be useful to point out why leaving
UINT_MAX at UINT_MAX upon increment wouldn't be a very good choice.

The integers modulo (UINT_MAX+1), with addition and multiplication as
usually defined, form what is mathematically called a "ring". This gives
a number of properties that are very desirable, such as:

a*(b+c) = a*b + a*c
For each (a,b), there exist an element "-b" such that a+b+(-b)==a

[[...and many more...]]

Guarantees such as these would be violated when introducing a barrier
like UINT_MAX. It's perhaps a bit difficult to appreciate why this would
be necessarily be "bad" unless you have a bit of algebra background.
Suffice it to say that many algorithms (e.g., arbitrary precision
arithmetic) would be very difficult to get right, without the ring
properties.
Best regards,

Sidney

Nov 14 '05 #4
Sidney Cadot wrote:
Capstar wrote:
Hi NG,

I have the next program:

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned int a = UINT_MAX;

printf("%u\n", a);
printf("%u\n", ++a);

return 0;
}

When I compile and run this the result is:
4294967295
0

This doesn't surprise me because I overflowed a. But I was wondering
if the standard defines this behaviour, or if it depends on the cpu
used. All cpu's I know (not that many) will just overflow when you add
one to the maximum value for that particular cpu, but I don't think it
is unreasonable to design a cpu which just leaves the value at UINT_MAX.

As already pointed out, behavior such as this is actually mandated by
the standard. However, it may be useful to point out why leaving
UINT_MAX at UINT_MAX upon increment wouldn't be a very good choice.

The integers modulo (UINT_MAX+1), with addition and multiplication as
usually defined, form what is mathematically called a "ring". This gives
a number of properties that are very desirable, such as:

a*(b+c) = a*b + a*c
For each (a,b), there exist an element "-b" such that a+b+(-b)==a

[[...and many more...]]

Guarantees such as these would be violated when introducing a barrier
like UINT_MAX. It's perhaps a bit difficult to appreciate why this would
be necessarily be "bad" unless you have a bit of algebra background.
Suffice it to say that many algorithms (e.g., arbitrary precision
arithmetic) would be very difficult to get right, without the ring
properties.
Best regards,

Sidney


Thanks for all of your quick replies.
This info is very helpfull.

Mark

Nov 14 '05 #5
Capstar wrote:
[...]
This doesn't surprise me because I overflowed a. [declared as unsigned int] But I was wondering if
the standard defines this behaviour,
Yes
or if it depends on the cpu used.
No
Another question I have is about the assert macro. Is it commonly seen
as a good practice to use this or is it better/safer to just always do a
check yourself and return if something is wrong?


This is a religious issue. The angels are on the side that uses assert()
during development and debugging; it should never be used to (mis)handle
run-time errors in production code. But, there are heathen among us in clc.
--
Martin Ambuhl
Nov 14 '05 #6

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

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
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
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
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: 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
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
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
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...

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.