Re: integer and long max/min values

2003-11-25 Thread Sean Farley
On Fri, 21 Nov 2003, Duane H. Hesser wrote:

 On 21-Nov-2003 Richard Coleman wrote:
  Jay Sern Liew wrote:
 
  how do I find out the maximum (and minimum) value a long and int will hold
  in C? (before it overflows or underflows)
 
  if it's compiler-dependent, then does anyone know where I can find the GCC
  documentation for stuff like that?
 
  It will be architecture dependent (32 or 64 bit machines?).  I doubt the
  GCC docs talk about this.  You might check Richard Steven's book on
  Advanced Unix Programming.  It covers lots of information about
  standard machine limits and how to discover them.

 As a point of interest, there is a file

 /usr/src/contrib/gcc/enquire.c

 in the distributed sources which, when compiled and run, will
 report the max and min values of short, long, int, float, etc.
 along with various sizes and alignments.

To get the latest version (5.1a) of enquire:
http://homepages.cwi.nl/~steven/enquire.html

Sean
---
[EMAIL PROTECTED]
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: integer and long max/min values

2003-11-21 Thread Tim Kientzle
Jay Sern Liew wrote:
how do I find out the maximum (and minimum) value a long and int will hold
in C? (before it overflows or underflows)
#include limits.h

INT_MAX and INT_MIN  are the max/min values for an int
LONG_MAX and LONG_MIN are the max/min values for long.
Also, see stdint.h, which is defined in C99.

Also, buy a good C reference book.  ;-)

Tim Kientzle

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: integer and long max/min values

2003-11-21 Thread Richard Coleman
Jay Sern Liew wrote:

how do I find out the maximum (and minimum) value a long and int will hold
in C? (before it overflows or underflows)
if it's compiler-dependent, then does anyone know where I can find the GCC
documentation for stuff like that?
It will be architecture dependent (32 or 64 bit machines?).  I doubt the 
GCC docs talk about this.  You might check Richard Steven's book on 
Advanced Unix Programming.  It covers lots of information about 
standard machine limits and how to discover them.

Richard Coleman
[EMAIL PROTECTED]
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


RE: integer and long max/min values

2003-11-21 Thread Vijay.Singh
Write a simple C program to ++ an int or long variable and see when it overflows.

 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] Behalf Of ext 
 Tim Kientzle
 Sent: Friday, November 21, 2003 12:24 AM
 To: Jay Sern Liew
 Cc: [EMAIL PROTECTED]
 Subject: Re: integer and long max/min values
 
 
 Jay Sern Liew wrote:
  how do I find out the maximum (and minimum) value a long 
 and int will hold
  in C? (before it overflows or underflows)
 
 #include limits.h
 
 INT_MAX and INT_MIN  are the max/min values for an int
 LONG_MAX and LONG_MIN are the max/min values for long.
 
 Also, see stdint.h, which is defined in C99.
 
 Also, buy a good C reference book.  ;-)
 
 Tim Kientzle
 
 ___
 [EMAIL PROTECTED] mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
 To unsubscribe, send any mail to 
 [EMAIL PROTECTED]
 
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: integer and long max/min values

2003-11-21 Thread Peter Pentchev
On Fri, Nov 21, 2003 at 09:53:50AM -0800, [EMAIL PROTECTED] wrote:
[format recovered; Tim Kientzle wrote:]
  Jay Sern Liew wrote:
   how do I find out the maximum (and minimum) value a long 
  and int will hold
   in C? (before it overflows or underflows)
  
  #include limits.h
  
  INT_MAX and INT_MIN  are the max/min values for an int
  LONG_MAX and LONG_MIN are the max/min values for long.
  
  Also, see stdint.h, which is defined in C99.
  
  Also, buy a good C reference book.  ;-)
  
  Tim Kientzle
[..and Vijay Singh wrote:]
 
 Write a simple C program to ++ an int or long variable and see when it
 overflows.

Why, after Tim actually quoted the exact header file to include and the
exact constants to use?

G'luck,
Peter

-- 
Peter Pentchev  [EMAIL PROTECTED][EMAIL PROTECTED][EMAIL PROTECTED]
PGP key:http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint FDBA FD79 C26F 3C51 C95E  DF9E ED18 B68D 1619 4553
You have, of course, just begun reading the sentence that you have just finished 
reading.


pgp0.pgp
Description: PGP signature


Re: integer and long max/min values

2003-11-21 Thread ari
'Tis a bit of a waste of cpu time there.

On a two's complement system, which is probably all you'll come by, you
can find UINT_MAX by setting an unsigned integer to -1:

unsigned int i_max = -1;

A slightly more architecturally independent way of finding UINT_MAX
would be to set an unsigned integer to the complement of its zero:

unsigned int i_max = ~(unsigned int)0;

Either way, you can find the positive signed maximum by dividing your
result by two (discarding the remainder).

Try the following:

#include limits.h
#include assert.h

...
assert ((unsigned int)-1 == UINT_MAX);
assert (~(unsigned int)0 == UINT_MAX);

assert (((unsigned int)-1)/2 == INT_MAX);
assert ((~(unsigned int)0)/2 == INT_MAX);
...

Some may argue against this method, but using an unsigned complement of
zero should hold at least the portability of assuming NULL to be false.

The *_MAX macros, of course, should still be used whenever possible.

ari


[EMAIL PROTECTED] said this stuff:

 Write a simple C program to ++ an int or long variable and see when it overflows.
 
  -Original Message-
  From: [EMAIL PROTECTED]
  [mailto:[EMAIL PROTECTED] Behalf Of ext 
  Tim Kientzle
  Sent: Friday, November 21, 2003 12:24 AM
  To: Jay Sern Liew
  Cc: [EMAIL PROTECTED]
  Subject: Re: integer and long max/min values
  
  
  Jay Sern Liew wrote:
   how do I find out the maximum (and minimum) value a long 
  and int will hold
   in C? (before it overflows or underflows)
  
  #include limits.h
  
  INT_MAX and INT_MIN  are the max/min values for an int
  LONG_MAX and LONG_MIN are the max/min values for long.
  
  Also, see stdint.h, which is defined in C99.
  
  Also, buy a good C reference book.  ;-)
  
  Tim Kientzle
  
  ___
  [EMAIL PROTECTED] mailing list
  http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
  To unsubscribe, send any mail to 
  [EMAIL PROTECTED]
  
 ___
 [EMAIL PROTECTED] mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
 To unsubscribe, send any mail to [EMAIL PROTECTED]
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: integer and long max/min values

2003-11-21 Thread Duane H. Hesser

On 21-Nov-2003 Richard Coleman wrote:
 Jay Sern Liew wrote:
 
 how do I find out the maximum (and minimum) value a long and int will hold
 in C? (before it overflows or underflows)
 
 if it's compiler-dependent, then does anyone know where I can find the GCC
 documentation for stuff like that?
 
 It will be architecture dependent (32 or 64 bit machines?).  I doubt the 
 GCC docs talk about this.  You might check Richard Steven's book on 
 Advanced Unix Programming.  It covers lots of information about 
 standard machine limits and how to discover them.
 
 Richard Coleman
 [EMAIL PROTECTED]
 
 

As a point of interest, there is a file

/usr/src/contrib/gcc/enquire.c

in the distributed sources which, when compiled and run, will
report the max and min values of short, long, int, float, etc.
along with various sizes and alignments.


-- 
--
Just be glad Microsoft doesn't make passenger airplanes.
Duane H. Hesser [EMAIL PROTECTED]
--
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


integer and long max/min values

2003-11-20 Thread Jay Sern Liew
how do I find out the maximum (and minimum) value a long and int will hold
in C? (before it overflows or underflows)

if it's compiler-dependent, then does anyone know where I can find the GCC
documentation for stuff like that?

thanks.


Jay Sern Liew
[EMAIL PROTECTED],ieee}.org
gpg --keyserver pgp.mit.edu --recv-keys 0xA115A33F

pub  1024D/A115A33F 2003-01-02 Jay Sern Liew [EMAIL PROTECTED]
 Key fingerprint = B08E 2481 B4CE 284A C0DE  E359 8646 7B7E A115 A33F
sub  1024g/7504C197 2003-01-02 [expires: 2003-12-31]
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: integer and long max/min values

2003-11-20 Thread Christopher Vance
On Thu, Nov 20, 2003 at 10:39:05PM -0600, Jay Sern Liew wrote:
how do I find out the maximum (and minimum) value a long and int will hold
in C? (before it overflows or underflows)
if it's compiler-dependent, then does anyone know where I can find the GCC
documentation for stuff like that?
Wrong mailing list - if you don't already know really basic C, you
ain't a hacker.
Try limits.h.

--
Christopher Vance
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]