INT_MAX/MIN

R

Roopa

Hi,

Is there anything wrong with this code ?

#include <stdio.h>

int main()
{
int i=32999;

printf ("\n%d\n", i);
return 0;
}

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.

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.

Thanks
 
J

Jack Klein

Hi,

Is there anything wrong with this code ?

#include <stdio.h>

int main()
{
int i=32999;

printf ("\n%d\n", i);
return 0;
}

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.

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.

Thanks

If an int on your implementation holds 32 bits (which may be less than
4 bytes), but your <limits.h> file defines INT_MAX as 32767, then you
have the wrong <limits.h> file.

See:

http://jk-technology.com/c/inttypes.html
 
K

Keith Thompson

Hi,

Is there anything wrong with this code ?

#include <stdio.h>

int main()
{
int i=32999;

printf ("\n%d\n", i);
return 0;
}

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.

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.

The standard says that INT_MAX has to be *at least* 32767.

If your system has 32-bit ints, your INT_MAX is probably 2147483647.

Your declaration

int i = 32999;

is non-portable; it will work only on systems with INT_MAX >= 32999.
(That probably includes your system.)

Try this:

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

int main(void)
{
printf("INT_MIN == %d\n", INT_MIN);
printf("INT_MAX == %d\n", INT_MAX);
printf("UINT_MAX == %u\n", UINT_MAX);
printf("sizeof(int) == %d (%d bits)\n",
(int)sizeof(int),
(int)sizeof(int) * CHAR_BIT);
return 0;
}
 
P

pete

Roopa said:
So whats is the use of INT_MAX..(or similar
for other objects) defined in the standard.

You can use the likes of INT_MAX to wite correct code
that will work properly on any conforming implementation,
regardless of the value of INT_MAX.

This version of itoa (exercise 3-4 from K&R2)
uses INT_MAX and will work properly on any computer.

#include <limits.h>
void itoa_2(int i, char *s)
{
char c, *p;
int tenth, min_flag;

min_flag = 0;
if (0 > i) {
*s++ = '-';
i = -INT_MAX > i ? min_flag = INT_MAX : -i;
}
p = s;
do {
tenth = i / 10;
*p++ = (char)(i - 10 * tenth + '0');
i = tenth;
} while (i != 0);
*p = '\0';
if (min_flag != 0) {
++*s;
}
while (--p > s) {
c = *p;
*p = *s;
*s++ = c;
}
}
 
D

Dan Pop

In said:
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
 

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
474,266
Messages
2,571,081
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top