|
Integer types, and integer overflows, in C
C offers you the ability to define a whole variety of integer types:
short int my_number;
int my_number;
long int my_number;
typically occupying 16, 32 and 64 bits of memory (but that is NOT a standard, and you may find different on some systems). And the amount of memory that's declared controls the minimum and maximum numbers that can be held. C, as a language which assumes the programmer knows what (s)he is doing, will not check for overflow for you, and will give results which are plain wrong arithmetically if you exceed the limits.
There's an example [here] from yesterday's C course, where I attempted to fill an array with an exponentially growing series of numbers, and ended up bursting even the capability of a long int. You'll note that (on my machine, in this case), I ended up getting zeros back rather than the largest number the machine can handle - a dramatic failure of my code.
There's a header file - limit.h - which #defines a whole load of constants such as INT_MAX to help you program to check for limits. There's an example of it in use [here] in a training example from our C Programming course.
(written 2012-01-25, updated 2012-01-28)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles C201 - C and C based languages - C Language Fundamentals [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09) [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05) [3234] Your program - you just provide the filling in the sandwich - (2011-04-08) [3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04) [2842] Staring a C course with Hello World - why? - (2010-06-30) [2576] What does const mean? C and C++ - (2010-01-15) [2005] Variables and pointers and references - C and C++ - (2009-01-23) [1671] Compiling C programs with gcc - an overview - (2008-06-10) [888] Turning C from source to a running program - (2006-10-06)
265d
Some other Articles
Looking upBack to UniChars, char arrays and strings in C. Some early cautions and pitfalls.Are we one man, one vote? No, and we never have been. Integer types, and integer overflows, in CDefining, declaring and initialising variables in CPromoting a single one of your domains on the search enginesLearning about how to help people learning - and retaining C++ Courses - do I get official certification at the end of my Well House course?Emergency First Aid Provision at Well House Manor - part of the service
|
4106 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|