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

INT_MAX/MIN

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
Nov 14 '05 #1
4 40267
On 10 Aug 2004 21:06:40 -0700, ro*****@globalpinoy.com (Roopa) wrote
in comp.lang.c:
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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
ro*****@globalpinoy.com (Roopa) writes:
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;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
Roopa wrote:
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;
}
}
Nov 14 '05 #4
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
Nov 14 '05 #5

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

Similar topics

1
by: Dave Huang | last post by:
Hi, I don't actually know Python; I'm just trying to debug a problem I encounted in another program, so apologies if this has been covered before. I did do some Google searches though, and didn't...
7
by: vikky | last post by:
hi all, Out of sheer curosity, I decided to initialize an integer with a number bigger than INT_MAX, however I still am not able to justify its output. Here is the program : #include<stdio.h> ...
2
by: Marlene Stebbins | last post by:
I am entering numbers into my program from the command line. I want to check whether they are > INT_MAX. Sounds simple, but I've discovered that if(x <= INT_MAX) { /* use x in some calculation...
2
by: Al-Burak | last post by:
I have a program that retrieves the cursor position at the point where the data began to be stored, this value is assigned to a 'std::streampos' data type called 'pos'. Later on in the program,...
8
by: A. Farber | last post by:
Hello, I have this simple program: #include <stdio.h> #include <stdlib.h> int main() { char *args = "2162508224"; printf("args=%s, atoi=%lu, atol=%lu\n",
24
by: Yevgen Muntyan | last post by:
Hey, Is it correct that number of value bits in int and unsigned int representation may be the same? If it is so, then INT_MIN may be -(INT_MAX+1) (in mathematical sense), i.e. abs(INT_MIN) is...
26
by: Yevgen Muntyan | last post by:
Hey, It was mentioned elsewhere that printf("%d", INT_MAX); is implementation-defined. Why? Is it because INT_MAX value is implementation-defined so output depends on implementation, or is it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.