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

range for int

I wrote a small program to check the range for int(signed) and long
int(signed) on my machine:
#include <stdio.h>
#include <limits.h>

int main(void)
{
printf("INT_MIN:%d INT_MAX: %d", INT_MIN, INT_MAX);
printf("\nLONG_MAX: %d LONG_MIN: %d", LONG_MIN, LONG_MAX);
return 0;
}

the o/p that i get:
INT_MIN:-2147483648 INT_MAX: 2147483647
LONG_MIN:-2147483648 LONG_MAX: 2147483647

basically the same thing. why is this happening ? also these ranges
seem to contradict with the one given in K & R 2. Does it have
something to do with how the numbers are represented on a particular
machine ?
Jun 27 '08 #1
5 2664
pereges wrote:
I wrote a small program to check the range for int(signed) and long
int(signed) on my machine:
#include <stdio.h>
#include <limits.h>

int main(void)
{
printf("INT_MIN:%d INT_MAX: %d", INT_MIN, INT_MAX);
printf("\nLONG_MAX: %d LONG_MIN: %d", LONG_MIN, LONG_MAX);
return 0;
}

the o/p that i get:
INT_MIN:-2147483648 INT_MAX: 2147483647
LONG_MIN:-2147483648 LONG_MAX: 2147483647

basically the same thing. why is this happening ?
Because these are the ranges your implementation can support.
also these ranges
seem to contradict with the one given in K & R 2.
No. The ranges given in K&R2 are the minimum ones an implementation must
provide. An implementation may implement wider ranges. Also,
sizeof(long) is allowed to be equal to sizeof(int).
Does it have
something to do with how the numbers are represented on a particular
machine ?
Um, yes and no.

If you're talking about why INT_MIN is far smaller than the one given in
K&R2 and INT_MAX is far greater than the one given in K&R2, it is more
related to the number of bits used to store an int.

If it's about why INT_MIN for a 32-bit int can be -2147483648 or
-2147483647, then it has something to do with how a signed int is
internally represented in an implementation.

HTH.
Jun 27 '08 #2
On Jun 10, 4:21*pm, pereges <Brol...@gmail.comwrote:
I wrote a small program to check the range for int(signed) and long
int(signed) on my machine:

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

int main(void)
{
* *printf("INT_MIN:%d INT_MAX: %d", INT_MIN, INT_MAX);
* printf("\nLONG_MAX: %d LONG_MIN: %d", LONG_MIN, LONG_MAX);
* return 0;

}

the o/p that i get:
INT_MIN:-2147483648 * * * * * * * * INT_MAX: 2147483647
LONG_MIN:-2147483648 * * * * * * LONG_MAX: 2147483647

basically the same thing. why is this happening ? also these ranges
seem to contradict with the one given in K & R 2. Does it have
something to do with how the numbers are represented on a particular
machine ?
hi
i dont know very much about the macros you have used
yes , the range is depeneds on machine
but here is the solution to find range
#include<stdio.h>
#include<math.h>
int main()
{
printf("sizeof signed int %d, sizeof long %l\n",(power(2,
(8*sizeof(int))-1),(power(2,8*sizeof(int))-1) );
retrun 0;

}

probably it will work , do modification for compilation errors
Jun 27 '08 #3
rams <ra******@gmail.comwrote:
On Jun 10, 4:21Â*pm, pereges <Brol...@gmail.comwrote:
I wrote a small program to check the range for int(signed) and long
int(signed) on my machine:

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

int main(void)
{
Â* Â*printf("INT_MIN:%d INT_MAX: %d", INT_MIN, INT_MAX);
Â* printf("\nLONG_MAX: %d LONG_MIN: %d", LONG_MIN, LONG_MAX);
Â* return 0;

}
i dont know very much about the macros you have used
yes , the range is depeneds on machine
but here is the solution to find range
No, the solution to find the ranges is using those macros.
Everything else only works by making assumptions that may
or may a well not be correct on a certain machine.
#include<stdio.h>
#include<math.h>
int main()
{
printf("sizeof signed int %d, sizeof long %l\n",(power(2,
(8*sizeof(int))-1),(power(2,8*sizeof(int))-1) );
You make at least two assumptions that may be wrong. First
you assume that a char always has 8 bits. That' not the case
on quite a number of systems and that's why there is the
CHAR_BIT macro which tells you how many bits a char has.
And, second you assume that all available bits are used
in the representation of a number which, while probably
correct in most cases, is nothing you can 100% rely on.

I'm not going to comment on the several bugs you managed
to squeeze into a single line of code, just one really
important thing the compiler probably won't find for you:
if you use "%d" as the format specifier you must have an
int argument at the corresponding position. And the pow()
function (I guess that's what you meant when you wrote
power()) returns a double and not an int.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #4
Iman S. H. Suyoto wrote:
No. The ranges given in K&R2 are the minimum ones an implementation must
provide. An implementation may implement wider ranges. Also,
sizeof(long) is allowed to be equal to sizeof(int).
Also, sizeof(long) is allowed to be equal to sizeof(char),
which is one byte.

--
pete
Jun 27 '08 #5
pereges wrote:
I wrote a small program to check the range for int(signed) and long
int(signed) on my machine:
#include <stdio.h>
#include <limits.h>

int main(void)
{
printf("INT_MIN:%d INT_MAX: %d", INT_MIN, INT_MAX);
printf("\nLONG_MAX: %d LONG_MIN: %d", LONG_MIN, LONG_MAX);
return 0;
}

the o/p that i get:
INT_MIN:-2147483648 INT_MAX: 2147483647
LONG_MIN:-2147483648 LONG_MAX: 2147483647

basically the same thing. why is this happening ? also these ranges
seem to contradict with the one given in K & R 2. Does it have
something to do with how the numbers are represented on a particular
machine ?
Also here is a program I wrote some time ago for printing various limits
of the C translation and execution environment.

/* Program to print various implementation defined numerical limits.
*
* Set C99_CONFORMANCE to one if your compiler supports the features
* needed by this program, but fails to define __STDC_VERSION to
199901L.
* TODO: Include complex and imaginary types and features of the runtime
* floating point environment through <fenv.h>
*/
#define C99_CONFORMANCE 1
#if __STDC_VERSION__ == 199901L || C99_CONFORMANCE == 1
#define HAVE_C99 1
#endif

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <signal.h>
#include <wchar.h>

#ifdef HAVE_C99
#include <inttypes.h>
#include <stdbool.h>
#endif

int main(void)
{
int dummy;
printf(
"\nCHAR_BIT == %d\n\n"
"Type\t\t\tSize\tMin.\t\t\tMax.\n"
"================================================= ===========\n\n"
#if HAVE_C99 && __bool_true_false_are_defined
"bool\t\t\t%u\n"
#elif HAVE_C99
"_Bool\t\t\t%u\n"
#endif
"char\t\t\t1\t%d\t\t\t%u\n"
"signed char\t\t1\t%d\t\t\t%d\n"
"unsigned char\t\t1\t0\t\t\t%u\n"
"short\t\t\t%u\t%d\t\t\t%d\n"
"unsigned short\t\t%u\t0\t\t\t%u\n"
"int\t\t\t%u\t%d\t\t%d\n"
"unsigned int\t\t%u\t0\t\t\t%u\n"
"long\t\t\t%u\t%ld\t\t%ld\n"
"unsigned long\t\t%u\t0\t\t\t%lu\n",
CHAR_BIT,
#if HAVE_C99 && __bool_true_false_are_defined
(unsigned)sizeof(bool),
#elif HAVE_C99
(unsigned)sizeof(_Bool),
#endif
CHAR_MIN, CHAR_MAX,
SCHAR_MIN, SCHAR_MAX,
UCHAR_MAX,
(unsigned)sizeof(short), SHRT_MIN, SHRT_MAX,
(unsigned)sizeof(unsigned short), USHRT_MAX,
(unsigned)sizeof(int), INT_MIN, INT_MAX,
(unsigned)sizeof(unsigned int), UINT_MAX,
(unsigned)sizeof(long), LONG_MIN, LONG_MAX,
(unsigned)sizeof(unsigned long), ULONG_MAX);
#ifdef HAVE_C99
printf(
"long long\t\t%u\t%lld\t%lld\n"
"unsigned long long\t%u\t0\t\t\t%llu\n",
(unsigned)sizeof(long long), LLONG_MIN, LLONG_MAX,
(unsigned)sizeof(unsigned long long), ULLONG_MAX);
#endif
printf(
"float\t\t\t%u\t%g\t\t%g\n"
"double\t\t\t%u\t%g\t\t%g\n"
"long double\t\t%u\t%Lg\t\t%Lg\n",
(unsigned)sizeof(float), FLT_MIN, FLT_MAX,
(unsigned)sizeof(double), DBL_MIN, DBL_MAX,
(unsigned)sizeof(long double), LDBL_MIN, LDBL_MAX);
printf(
"\nAdditional properties of floating types:\n"
"FLT_RADIX\t\t%d\n"
"FLT_MANT_DIG\t\t%d\n"
"DBL_MANT_DIG\t\t%d\n"
"LDBL_MANT_DIG\t\t%d\n"
#ifdef HAVE_C99
"DECIMAL_DIG\t\t%d\n"
#endif
"FLT_DIG\t\t\t%d\n"
"DBL_DIG\t\t\t%d\n"
"LDBL_DIG\t\t%d\n"
"FLT_MIN_EXP\t\t%d\n"
"FLT_MAX_EXP\t\t%d\n"
"DBL_MIN_EXP\t\t%d\n"
"DBL_MAX_EXP\t\t%d\n"
"LDBL_MIN_EXP\t\t%d\n"
"LDBL_MAX_EXP\t\t%d\n"
"FLT_MIN_10_EXP\t\t%d\n"
"FLT_MAX_10_EXP\t\t%d\n"
"DBL_MIN_10_EXP\t\t%d\n"
"DBL_MAX_10_EXP\t\t%d\n"
"LDBL_MIN_10_EXP\t\t%d\n"
"LDBL_MAX_10_EXP\t\t%d\n"
"FLT_EPSILON\t\t%g\n"
"DBL_EPSILON\t\t%g\n"
"LDBL_EPSILON\t\t%Lg\n",
FLT_RADIX, FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG,
#ifdef HAVE_C99
DECIMAL_DIG,
#endif
FLT_DIG, DBL_DIG, LDBL_DIG, FLT_MIN_EXP, FLT_MAX_EXP,
DBL_MIN_EXP,
DBL_MAX_EXP, LDBL_MIN_EXP, LDBL_MAX_EXP, FLT_MIN_10_EXP,
FLT_MAX_10_EXP, DBL_MIN_10_EXP, DBL_MAX_10_EXP, LDBL_MIN_10_EXP,
LDBL_MAX_10_EXP, FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON);

#ifdef HAVE_C99
printf(
"\nProperties of types defined in stdint.h\n"
#ifdef INT8_MIN
"int8_t\t\t\t%u\t%" PRId8 "\t\t\t%" PRId8 "\n"
"uint8_t\t\t\t%u\t0\t\t\t%" PRIu8 "\n"
#endif
#ifdef INT16_MIN
"int16_t\t\t\t%u\t%" PRId16 "\t\t\t%" PRId16 "\n"
"uint16_t\t\t%u\t0\t\t\t%" PRIu16 "\n"
#endif
#ifdef INT32_MIN
"int32_t\t\t\t%u\t%" PRId32 "\t\t%" PRId32 "\n"
"uint32_t\t\t%u\t0\t\t\t%" PRIu32 "\n"
#endif
#ifdef INT64_MIN
"int64_t\t\t\t%u\t%" PRId64 "\t%" PRId64 "\n"
"uint64_t\t\t%u\t0\t\t\t%" PRIu64 "\n"
#endif
"int_least8_t\t\t%u\t%" PRIdLEAST8 "\t\t\t%" PRIdLEAST8 "\n"
"uint_least8_t\t\t%u\t0\t\t\t%" PRIuLEAST8 "\n"
"int_least16_t\t\t%u\t%" PRIdLEAST16 "\t\t\t%" PRIdLEAST16 "\n"
"uint_least16_t\t\t%u\t0\t\t\t%" PRIuLEAST16 "\n"
"int_least32_t\t\t%u\t%" PRIdLEAST32 "\t\t%" PRIdLEAST32 "\n"
"uint_least32_t\t\t%u\t0\t\t\t%" PRIuLEAST32 "\n"
"int_least64_t\t\t%u\t%" PRIdLEAST64 "\t%" PRIdLEAST64 "\n"
"uint_least64_t\t\t%u\t0\t\t\t%" PRIuLEAST64 "\n"
"int_fast8_t\t\t%u\t%" PRIdFAST8 "\t\t\t%" PRIdFAST8 "\n"
"uint_fast8_t\t\t%u\t0\t\t\t%" PRIuFAST8 "\n"
"int_fast16_t\t\t%u\t%" PRIdFAST16 "\t\t%" PRIdFAST16 "\n"
"uint_fast16_t\t\t%u\t0\t\t\t%" PRIuFAST16 "\n"
"int_fast32_t\t\t%u\t%" PRIdFAST32 "\t\t%" PRIdFAST32 "\n"
"uint_fast32_t\t\t%u\t0\t\t\t%" PRIuFAST32 "\n"
"int_fast64_t\t\t%u\t%" PRIdFAST64 "\t%" PRIdFAST64 "\n"
"uint_fast64_t\t\t%u\t0\t\t\t%" PRIuFAST64 "\n"
"intmax_t\t\t%u\t%" PRIdMAX "\t%" PRIdMAX "\n"
"uintmax_t\t\t%u\t0\t\t\t%" PRIuMAX "\n"
#ifdef INTPTR_MIN
"intptr_t\t\t%u\t%" PRIdPTR "\t\t%" PRIdPTR "\n"
#endif
#ifdef UINTPTR_MAX
"uintptr_t\t\t%u\t0\t\t\t%" PRIuPTR "\n"
#endif
"%n"
,
#ifdef INT8_MIN
(unsigned)sizeof(int8_t), INT8_MIN, INT8_MAX,
(unsigned)sizeof(uint8_t), UINT8_MAX,
#endif
#ifdef INT16_MIN
(unsigned)sizeof(int16_t), INT16_MIN, INT16_MAX,
(unsigned)sizeof(uint16_t), UINT16_MAX,
#endif
#ifdef INT32_MIN
(unsigned)sizeof(int32_t), INT32_MIN, INT32_MAX,
(unsigned)sizeof(uint32_t), UINT32_MAX,
#endif
#ifdef INT64_MIN
(unsigned)sizeof(int64_t), INT64_MIN, INT64_MAX,
(unsigned)sizeof(uint64_t), UINT64_MAX,
#endif
(unsigned)sizeof(int_least8_t), INT_LEAST8_MIN, INT_LEAST8_MAX,
(unsigned)sizeof(uint_least8_t), UINT_LEAST8_MAX,
(unsigned)sizeof(int_least16_t), INT_LEAST16_MIN,
INT_LEAST16_MAX,
(unsigned)sizeof(uint_least16_t), UINT_LEAST16_MAX,
(unsigned)sizeof(int_least32_t), INT_LEAST32_MIN,
INT_LEAST32_MAX,
(unsigned)sizeof(uint_least32_t), UINT_LEAST32_MAX,
(unsigned)sizeof(int_least64_t), INT_LEAST64_MIN,
INT_LEAST64_MAX,
(unsigned)sizeof(uint_least64_t), UINT_LEAST64_MAX,
(unsigned)sizeof(int_fast8_t), INT_FAST8_MIN, INT_FAST8_MAX,
(unsigned)sizeof(uint_fast8_t), UINT_FAST8_MAX,
(unsigned)sizeof(int_fast16_t), INT_FAST16_MIN, INT_FAST16_MAX,
(unsigned)sizeof(uint_fast16_t), UINT_FAST16_MAX,
(unsigned)sizeof(int_fast32_t), INT_FAST32_MIN, INT_FAST32_MAX,
(unsigned)sizeof(uint_fast32_t), UINT_FAST32_MAX,
(unsigned)sizeof(int_fast64_t), INT_FAST64_MIN, INT_FAST64_MAX,
(unsigned)sizeof(uint_fast64_t), UINT_FAST64_MAX,
(unsigned)sizeof(intmax_t), INTMAX_MIN, INTMAX_MAX,
(unsigned)sizeof(uintmax_t), UINTMAX_MAX,
#ifdef INTPTR_MIN
(unsigned)sizeof(intptr_t), INTPTR_MIN, INTPTR_MAX,
#endif
#ifdef UINTPTR_MAX
(unsigned)sizeof(uintptr_t), UINTPTR_MAX,
#endif
&dummy);
#endif

printf(
"\nOther types:\n"
"ptrdiff_t\t\t%u"
#ifdef PTRDIFF_MIN
"\t%td\t\t%td"
#endif
"\n"
"size_t\t\t\t%u\t0\t\t\t%lu\n"
#ifdef WCHAR_MIN
"wchar_t\t\t\t%u\t%ld\t\t%lu\n"
#endif
#ifdef WINT_MIN
"wint_t\t\t\t%u\t%d\t\t\t%u\n"
#endif
"sig_atomic_t\t\t%u"
#ifdef SIG_ATOMIC_MIN
"\t%d\t\t%u"
#endif
"\n"

"BUFSIZ\t\t\t%lu\n"
"FOPEN_MAX\t\t%u\n"
"FILENAME_MAX\t\t%u\n"
"L_tmpnam\t\t%u\n"
"TMP_MAX\t\t\t%lu\n"
"RAND_MAX\t\t%lu\n"
"MB_LEN_MAX\t\t%lu\n"
"MB_CUR_MAX\t\t%lu\n"
"%n",
(unsigned)sizeof(ptrdiff_t),
#ifdef PTRDIFF_MIN
PTRDIFF_MIN, PTRDIFF_MAX,
#endif
(unsigned)sizeof(size_t), (unsigned long)((size_t)-1),
#ifdef WCHAR_MIN
(unsigned)sizeof(wchar_t), WCHAR_MIN, WCHAR_MAX,
#endif
#ifdef WINT_MIN
(unsigned)sizeof(wint_t), WINT_MIN, WINT_MAX,
#endif
(unsigned)sizeof(sig_atomic_t),
#ifdef SIG_ATOMIC_MIN
SIG_ATOMIC_MIN, SIG_ATOMIC_MAX,
#endif
(unsigned long)BUFSIZ, FOPEN_MAX, FILENAME_MAX, L_tmpnam,
(unsigned long)TMP_MAX, (unsigned long)RAND_MAX,
(unsigned long)MB_LEN_MAX, (unsigned long)MB_CUR_MAX, &dummy);

return 0;
}

Jun 27 '08 #6

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

Similar topics

7
by: Xah Lee | last post by:
Today we'll be writing a function called Range. The Perl documentation is as follows. Perl & Python & Java Solutions will be posted in 48 hours. This is Perl-Python a-day. See...
1
by: Thomas | last post by:
Hi dudes, I have a range object for text (not the one in the IE, the one for Mozilla). Now I have a function to reduce/move the range in the text to the LEFT, e.g.:...
4
by: Bill R via AccessMonster.com | last post by:
I get this Run-Time Error 1004 whenevery the following code runs: On Error GoTo XLSheet2 Set objXL = CreateObject("Excel.Application") With objXL Set objWkb = .Workbooks.Open(strPath) With...
4
by: IMS.Rushikesh | last post by:
Hi All, I am trying to execute below code but it gives me an COMException ///// Code Start //// public string GetName(Excel.Range range) { try { if (range.Name != null)
5
by: Chris | last post by:
Hey all. Anyone who is familiar with Python programming knows that you can have code like this: list = This code puts all the items processed by the for loop in a list plus 1. Is there a way...
29
by: Steve R. Hastings | last post by:
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list and...
3
by: toton | last post by:
Hi, I want ro iterate over a few container class within a range specified, instead of begin & end. How to construct a range class, which takes start & end, and iteration is available within that...
0
by: toton | last post by:
Hi, I have a view class for a vector, which stores different range for a few vectors, and provides a way to iterate over the range. The range class takes a pair of iterator, as given in post...
85
by: Russ | last post by:
Every Python programmer gets this message occasionally: IndexError: list index out of range The message tells you where the error occurred, but it doesn't tell you what the range and the...
0
by: iain654 | last post by:
I have finally worked out how to automatically send a range of cells in the body of an email using Outlook but it is very clumsy and I have to build up the email using the limit of line...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.