By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

limits.h question

P: n/a
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64 value */

Why won't it print the limit?
Nov 14 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
By the way...this might be off topic, but if i code the same thing in c++
with cout << it works ok...weird
"Mark Bruno" <ya*************@yahoo.com> wrote in message
news:3M********************@comcast.com...
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64 value */
Why won't it print the limit?

Nov 14 '05 #2

P: n/a
Mark Bruno wrote:
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64 value */

Why won't it print the limit?


It would seem that you are using a <limits.h> not appropriate for your
compiler. Make sure that you don't have a mismatch. For comparison, I get
for my implementation:

#include <limits.h>
#include <stdio.h>
int main(void)
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
printf("Smallest signed long: %ld\n", LONG_MIN);
printf("Smallest signed int: %d\n", INT_MIN);
printf("Smallest signed short: %hd\n", SHRT_MIN);
printf("Smallest signed char: %d\n", SCHAR_MIN);
printf("Largest signed long long: %lld\n", LLONG_MAX);
printf("Largest unsigned long long: %llu\n", ULLONG_MAX);
printf("Largest signed long: %ld\n", LONG_MAX);
printf("Largest unsigned long: %lu\n", ULONG_MAX);
printf("Largest signed int: %d\n", INT_MAX);
printf("Largest unsigned int: %u\n", UINT_MAX);
printf("Largest signed short: %hd\n", SHRT_MAX);
printf("Largest unsigned short: %hu\n", USHRT_MAX);
printf("Largest signed char: %d\n", SCHAR_MAX);
printf("Largest unsigned char: %u\n", UCHAR_MAX);
return 0;
}

Smallest signed long long: -9223372036854775808
Smallest signed long: -2147483648
Smallest signed int: -2147483648
Smallest signed short: -32768
Smallest signed char: -128
Largest signed long long: 9223372036854775807
Largest unsigned long long: 18446744073709551615
Largest signed long: 2147483647
Largest unsigned long: 4294967295
Largest signed int: 2147483647
Largest unsigned int: 4294967295
Largest signed short: 32767
Largest unsigned short: 65535
Largest signed char: 127
Largest unsigned char: 255


--
Martin Ambuhl

Nov 14 '05 #3

P: n/a
I'm using MS Visual C++ 7.1 (.net 2003), with all the appropriate headers.
Anyone with MSVC++7.1 out there who can vouch for this bug?
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:xF******************@newsread3.news.atl.earth link.net...
Mark Bruno wrote:
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64 value */
Why won't it print the limit?


It would seem that you are using a <limits.h> not appropriate for your
compiler. Make sure that you don't have a mismatch. For comparison, I get
for my implementation:

#include <limits.h>
#include <stdio.h>
int main(void)
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
printf("Smallest signed long: %ld\n", LONG_MIN);
printf("Smallest signed int: %d\n", INT_MIN);
printf("Smallest signed short: %hd\n", SHRT_MIN);
printf("Smallest signed char: %d\n", SCHAR_MIN);
printf("Largest signed long long: %lld\n", LLONG_MAX);
printf("Largest unsigned long long: %llu\n", ULLONG_MAX);
printf("Largest signed long: %ld\n", LONG_MAX);
printf("Largest unsigned long: %lu\n", ULONG_MAX);
printf("Largest signed int: %d\n", INT_MAX);
printf("Largest unsigned int: %u\n", UINT_MAX);
printf("Largest signed short: %hd\n", SHRT_MAX);
printf("Largest unsigned short: %hu\n", USHRT_MAX);
printf("Largest signed char: %d\n", SCHAR_MAX);
printf("Largest unsigned char: %u\n", UCHAR_MAX);
return 0;
}

Smallest signed long long: -9223372036854775808
Smallest signed long: -2147483648
Smallest signed int: -2147483648
Smallest signed short: -32768
Smallest signed char: -128
Largest signed long long: 9223372036854775807
Largest unsigned long long: 18446744073709551615
Largest signed long: 2147483647
Largest unsigned long: 4294967295
Largest signed int: 2147483647
Largest unsigned int: 4294967295
Largest signed short: 32767
Largest unsigned short: 65535
Largest signed char: 127
Largest unsigned char: 255


--
Martin Ambuhl

Nov 14 '05 #4

P: n/a
I think this stems from VC++'s non C99 compliance, so some long long
features are implemented, but some (such as %lld in printf and scanf)
aren't.
"Mark Bruno" <ya*************@yahoo.com> wrote in message
news:yM********************@comcast.com...
I'm using MS Visual C++ 7.1 (.net 2003), with all the appropriate headers.
Anyone with MSVC++7.1 out there who can vouch for this bug?
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:xF******************@newsread3.news.atl.earth link.net...
Mark Bruno wrote:
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64
value
*/
Why won't it print the limit?


It would seem that you are using a <limits.h> not appropriate for your
compiler. Make sure that you don't have a mismatch. For comparison, I get for my implementation:

#include <limits.h>
#include <stdio.h>
int main(void)
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
printf("Smallest signed long: %ld\n", LONG_MIN);
printf("Smallest signed int: %d\n", INT_MIN);
printf("Smallest signed short: %hd\n", SHRT_MIN);
printf("Smallest signed char: %d\n", SCHAR_MIN);
printf("Largest signed long long: %lld\n", LLONG_MAX);
printf("Largest unsigned long long: %llu\n", ULLONG_MAX);
printf("Largest signed long: %ld\n", LONG_MAX);
printf("Largest unsigned long: %lu\n", ULONG_MAX);
printf("Largest signed int: %d\n", INT_MAX);
printf("Largest unsigned int: %u\n", UINT_MAX);
printf("Largest signed short: %hd\n", SHRT_MAX);
printf("Largest unsigned short: %hu\n", USHRT_MAX);
printf("Largest signed char: %d\n", SCHAR_MAX);
printf("Largest unsigned char: %u\n", UCHAR_MAX);
return 0;
}

Smallest signed long long: -9223372036854775808
Smallest signed long: -2147483648
Smallest signed int: -2147483648
Smallest signed short: -32768
Smallest signed char: -128
Largest signed long long: 9223372036854775807
Largest unsigned long long: 18446744073709551615
Largest signed long: 2147483647
Largest unsigned long: 4294967295
Largest signed int: 2147483647
Largest unsigned int: 4294967295
Largest signed short: 32767
Largest unsigned short: 65535
Largest signed char: 127
Largest unsigned char: 255


--
Martin Ambuhl


Nov 14 '05 #5

P: n/a
Mark Bruno wrote:
I think this stems from VC++'s non C99 compliance, so some long long
features are implemented, but some (such as %lld in printf and scanf)
aren't.


Please stop top-posting. It's rude.

My understanding is that Microsoft has not made, and has no interest in
making their compiler C99-compliant.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #6

P: n/a
> Mark Bruno wrote:
I think this stems from VC++'s non C99 compliance, so some long long
features are implemented, but some (such as %lld in printf and scanf)
aren't.


<OT>
I may be wrong, but I think that on Windows systems, printf is
implemented in msvc.dll (along with most if not all of the C standard
library). This is probably where the problem lies. You could check
whether there's an updated version with proper C99 support.

I tried a little test:

$ gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/3.3.1/specs
Configured with: /GCC/gcc-3.3.1-3/configure --with-gcc --with-gnu-ld
--with-gnu-as --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc
--libdir=/usr/lib --libexecdir=/usr/sbin --mandir=/usr/share/man
--infodir=/usr/share/info
--enable-languages=c,ada,c++,f77,pascal,java,objc --enable-libgcj
--enable-threads=posix --with-system-zlib --enable-nls
--without-included-gettext --enable-interpreter --enable-sjlj-exceptions
--disable-version-specific-runtime-libs --enable-shared
--disable-win32-registry --enable-java-gc=boehm
--disable-hash-synchronization --verbose --target=i686-pc-cygwin
--host=i686-pc-cygwin --build=i686-pc-cygwin
Thread model: posix
gcc version 3.3.1 (cygming special)
$ cat fun.c
#include <stdio.h>
#include <limits.h>

int main(void)
{
printf("LLONG_MAX: %lld\n", LLONG_MAX);
printf("LLONG_MIN: %lld\n", LLONG_MIN);

return 0;
}
$ gcc -W -Wall -std=c99 -pedantic-errors fun.c -o fun.exe
$ ./fun
LLONG_MAX: 9223372036854775807
LLONG_MIN: -9223372036854775808
$ gcc -W -Wall -std=c99 -pedantic-errors -mno-cygwin fun.c -o fun.exe
$ ./fun
LLONG_MAX: -1
LLONG_MIN: 0
When compiled with -mno-cygwin (which forces gcc to link against the
normal Windows libraries instead of the Cygwin unix-like libraries) I
see the same thing you do, probably because it's using the exact same
library (msvc.dll).
</OT>
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #7

P: n/a
Kevin Goodsell wrote:
Mark Bruno wrote:
I think this stems from VC++'s non C99 compliance, so some long long
features are implemented, but some (such as %lld in printf and scanf)
aren't.

<OT>
I may be wrong, but I think that on Windows systems, printf is
implemented in msvc.dll


I think that should have been msvcrt.dll.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #8

P: n/a
What's top posting? How do I stop it?
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Le******************@newsread1.news.pas.earth link.net...
Kevin Goodsell wrote:
Mark Bruno wrote:

I think this stems from VC++'s non C99 compliance, so some long long
features are implemented, but some (such as %lld in printf and scanf)
aren't.

<OT>
I may be wrong, but I think that on Windows systems, printf is
implemented in msvc.dll


I think that should have been msvcrt.dll.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 14 '05 #9

P: n/a
"Mark Bruno" <ya*************@yahoo.com> writes:
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64 value */

Why won't it print the limit?


I don't believe 0x8000000000000000 is a legal definition for
LLONG_MIN, even if it happens to have the same bit pattern.
(But I suspect the real problem is that your printf() either
doesn't implement %lld, or doesn't implement it correctly.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #10

P: n/a
On Sat, 27 Dec 2003 05:29:23 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
"Mark Bruno" <ya*************@yahoo.com> writes:
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64 value */

Why won't it print the limit?


I don't believe 0x8000000000000000 is a legal definition for


Why not?

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #11

P: n/a
"Mark Bruno" <ya*************@yahoo.com> wrote in
news:XP********************@comcast.com on Fri 26 Dec 2003 09:28:14p:
What's top posting? How do I stop it?


You just top-posted.

Here's an example of why you shouldn't do it.

A: It's self-explanatory.
Q: Why is it rude?
A: It's posting your reply above the post it's in response to.
Q: What is top-posting?

You don't do it by doing like I'm doing now: Put your reply below what you
are quoting. Snipping what you aren't responding to is also nice.

Nov 14 '05 #12

P: n/a
"Mark Bruno" <ya*************@yahoo.com> wrote in message
news:3M********************@comcast.com...
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64 value */
Why won't it print the limit?


I have VC7.1 too and I'm seeing the same.
I guess you can't rely on VC implementing C99 features.

The following code:
printf("%llu\n", ULLONG_MAX); will print UINT_MAX.

Pretty stupid of MS considering that it does recognize the long long format
strings and they have had the __int64 type for years.

This works with VC for instance.
printf("%I64d\n", LLONG_MIN);

All they had to do was to let %lld do the same as %I64d
Nov 14 '05 #13

P: n/a
Jack Klein wrote:

On Sat, 27 Dec 2003 05:29:23 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
"Mark Bruno" <ya*************@yahoo.com> writes:
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000
/*minimum signed __int64 value */

Why won't it print the limit?


I don't believe 0x8000000000000000 is a legal definition for


Why not?


Because, in C99, with few exceptions, the constants in limits.h,
have to have the same type as the type to which they apply.
LLONG_MIN, even if it happens to have the same bit pattern.


--
pete
Nov 14 '05 #14

P: n/a
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:i0********************************@4ax.com...
On Sat, 27 Dec 2003 05:29:23 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
"Mark Bruno" <ya*************@yahoo.com> writes:
Hey, I'm learning about the limits header, and I don't understand one
snippit of code. If I code:

#include <limits.h>
#include <stdio.h>
int main()
{
printf("Smallest signed long long: %lld\n", LLONG_MIN);
return 0;
}
I get the output:
Smallest signed long long: 0

Why is this? I even check in my limits.h file, where I see:
#define LLONG_MIN 0x8000000000000000 /*minimum signed __int64 value */
Why won't it print the limit?


I don't believe 0x8000000000000000 is a legal definition for [LLONG_MIN]


Why not?


Because, for conformance with C99, such a constant would probably have the
wrong type. Assuming that the given value is too large to actually be a
valid (signed) long long, then it has type unsigned long long according to
6.4.4.1p5, and it would then violate 5.2.4.2.1p1.

As a test, I'd try...

printf("%lld\n", (long long) (LLONG_MIN / 2));

--
Peter
Nov 14 '05 #15

P: n/a
pete wrote:
Jack Klein wrote:
Keith Thompson <ks***@mib.org> wrote
"Mark Bruno" <ya*************@yahoo.com> writes:

> Hey, I'm learning about the limits header, and I don't
> understand one snippit of code. If I code:
>
> #include <limits.h>
> #include <stdio.h>
> int main()
> {
> printf("Smallest signed long long: %lld\n", LLONG_MIN);
> return 0;
> }
> I get the output:
> Smallest signed long long: 0
>
> Why is this? I even check in my limits.h file, where I see:
> #define LLONG_MIN 0x8000000000000000
> /*minimum signed __int64 value */
>
> Why won't it print the limit?

I don't believe 0x8000000000000000 is a legal definition for


Why not?


Because, in C99, with few exceptions, the constants in limits.h,
have to have the same type as the type to which they apply.


What is 0x80.....0? It is an unsigned long long, probably 1
greater than LLONG_MAX. When read as a long long by the printf
code an overflow and undefined behavior results. This is why you
will see such constructs (for 2's complement machines) as:

#define INT_MIN (-INT_MAX - 1)

in limits.h. Great care needs to be taken in the printf (or
equivalent code) to handle these extreme values correctly.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #16

P: n/a
On Sat, 27 Dec 2003 12:02:34 GMT, pete <pf*****@mindspring.com> wrote
in comp.lang.c:
Jack Klein wrote:

On Sat, 27 Dec 2003 05:29:23 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
"Mark Bruno" <ya*************@yahoo.com> writes:
> Hey, I'm learning about the limits header, and I don't understand one
> snippit of code. If I code:
>
> #include <limits.h>
> #include <stdio.h>
> int main()
> {
> printf("Smallest signed long long: %lld\n", LLONG_MIN);
> return 0;
> }
> I get the output:
> Smallest signed long long: 0
>
> Why is this? I even check in my limits.h file, where I see:
> #define LLONG_MIN 0x8000000000000000
> /*minimum signed __int64 value */
>
> Why won't it print the limit?

I don't believe 0x8000000000000000 is a legal definition for


Why not?


Because, in C99, with few exceptions, the constants in limits.h,
have to have the same type as the type to which they apply.
LLONG_MIN, even if it happens to have the same bit pattern.


You're absolutely right, I just missed this completely. Thanks for
the correction.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.