In <9a**************************@posting.google.com >
ro*****@globalpinoy.com (Roopa) writes:
Is there anything wrong with this code ?
#include <stdio.h>
int main()
{
int i=32999;
printf ("\n%d\n", i);
return 0;
}
It depends on what you expect from it. It is not a strictly conforming
program, because its output is implementation-defined.
According to <limits.h>
* maximum value for an object of type int
INT_MAX +32767.
Now that i have crossed in my code does it invoke any UB.
Nope, i is initialised with an implementation-defined value if INT_MAX
is defined as 32767.
Also since on my m/c int is 4 bytes i believe max value i can store
is 0xffffffff, So essentially an unsigned int can hold (4294967295)
Correct me if i am wrong. So whats is the use of INT_MAX..(or similar
for other objects) defined in the standard.
Are you sure you're quoting from the <limits.h> of a compiler with
4-byte int's? Most likely, you're looking at the <limits.h> of a compiler
with 16-bit int's.
The main purpose of INT_MAX and friends is to allow you to detect integer
overflow before it actually happens (signed integer overflow invokes
undefined behaviour, so, detecting it after it happens is already too
late).
Another purpose is to allow selecting the right type for your needs
at compile time, because such macros can be used in preprocessor
directives. Imagine that you need a 32-bit unsigned integer type.
You could simply use unsigned long, but this may be wasteful on
implementations with 32-bit int and 64-bit long (if you need to allocate
large arrays of this type). You could solve the problem like this:
#include <limits.h>
#if UINT_MAX >= 4294967295
typedef unsigned uint32t;
#else
typedef unsigned long uint32t;
#endif
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
Da*****@ifh.de