473,789 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf("%d", INT_MAX);

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 something more subtle so
that output of the following is also implementation-defined:

if (INT_MAX == 65535)
printf("%d", INT_MAX);
else
printf("%d", 65535);

Thanks,
Yevgen
Mar 14 '07 #1
26 6866
Yevgen Muntyan wrote:
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 something more subtle so
that output of the following is also implementation-defined:

if (INT_MAX == 65535)
printf("%d", INT_MAX);
else
printf("%d", 65535);
Um, I again made it overcomplicated . Is this implementation-defined:

printf("%d\n", 65535);

(if \n matters, then we print \n after the first piece of code too,
it's there in main()).

Yevgen
Mar 14 '07 #2
Yevgen Muntyan wrote:
Yevgen Muntyan wrote:
>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 something more subtle so
that output of the following is also implementation-defined:

if (INT_MAX == 65535)
printf("%d", INT_MAX);
else
printf("%d", 65535);

Um, I again made it overcomplicated . Is this implementation-defined:

printf("%d\n", 65535);
After consulting the standard, the above should be

printf("%d\n", 32767);

Oh well.

Yevgen
Mar 14 '07 #3
Yevgen Muntyan wrote On 03/14/07 13:29,:
Yevgen Muntyan wrote:
>>Yevgen Muntyan wrote:
>>>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 something more subtle so
that output of the following is also implementation-defined:

if (INT_MAX == 65535)
printf("%d", INT_MAX);
else
printf("%d", 65535);

Um, I again made it overcomplicated . Is this implementation-defined:

printf("%d\n" , 65535);


After consulting the standard, the above should be

printf("%d\n", 32767);
It seems there are several questions here.

The value of INT_MAX is implementation-defined, so the
output produced by printf("%d\n", INT_MAX) is implementation-
defined. There is nothing "wrong" with the operation, but
a strictly-conforming program must not perform it.

The effect of printf("%d\n", 65535) is implementation-
UNdefined (if I may coin a term). If the implementation-
defined value of INT_MAX is >= 65535, then the output is
well defined. If INT_MAX < 65535, the constant 65535 is
of type unsigned int and the behavior is undefined because
"%d" requires a signed int. Hence, it is implementation-
defined whether the behavior is well defined or undefined.

The effect of printf("%d\n", 32767) is the same on all
hosted implementations (barring I/O errors).

--
Er*********@sun .com
Mar 14 '07 #4
Eric Sosman wrote:
Yevgen Muntyan wrote On 03/14/07 13:29,:
>Yevgen Muntyan wrote:
>>Yevgen Muntyan wrote:

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 something more subtle so
that output of the following is also implementation-defined:

if (INT_MAX == 65535)
printf("%d", INT_MAX);
else
printf("%d", 65535);
Um, I again made it overcomplicated . Is this implementation-defined:

printf("%d\n" , 65535);

After consulting the standard, the above should be

printf("%d\n ", 32767);

It seems there are several questions here.

The value of INT_MAX is implementation-defined, so the
output produced by printf("%d\n", INT_MAX) is implementation-
defined. There is nothing "wrong" with the operation, but
a strictly-conforming program must not perform it.
Sure.
The effect of printf("%d\n", 65535) is implementation-
UNdefined (if I may coin a term). ...
I thought (wrongly) 65535 was minimal value for INT_MAX.

[snip]
The effect of printf("%d\n", 32767) is the same on all
hosted implementations (barring I/O errors).
And this is what I asked about. I wanted to make sure there
is nothing special about passing INT_MAX as int to a variadic
function or something.
This printf thing was mentioned in connection to another instance
of implementation-defined behavior, where any behavior other
than the "right" one would break code in a serious way, so I wanted
to clarify if this INT_MAX is a usual pedant's story or something
serious.

Thanks,
Yevgen
Mar 14 '07 #5
Yevgen Muntyan wrote On 03/14/07 13:59,:
Eric Sosman wrote:
> The effect of printf("%d\n", 32767) is the same on all
hosted implementations (barring I/O errors).


And this is what I asked about. I wanted to make sure there
is nothing special about passing INT_MAX as int to a variadic
function or something.
No, nothing special. INT_MAX must expand to an expression
whose type is `int'; an expansion with the right value but
some other type would be wrong. Since INT_MAX is already an
`int', no promotions occur. Similarly, `32767' is already an
`int' and nothing happens to it.
This printf thing was mentioned in connection to another instance
of implementation-defined behavior, where any behavior other
than the "right" one would break code in a serious way, so I wanted
to clarify if this INT_MAX is a usual pedant's story or something
serious.
Neither, I think, since there's nothing wrong with it.
My point about printf("%d\n", 65535) is purest pedantry, but
printf("%d\n", INT_MAX) is beyond reproach (despite not being
strictly conforming).

--
Er*********@sun .com
Mar 14 '07 #6
Yevgen Muntyan wrote:
>
.... snip ...
>
Um, I again made it overcomplicated . Is this implementation-defined:

printf("%d\n", 65535);
Yes, because there is no guarantee that 65535 is an int. It may be
a long int. It is outside the guaranteed range of an int.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 15 '07 #7
CBFalconer wrote:
Yevgen Muntyan wrote:
... snip ...
>Um, I again made it overcomplicated . Is this implementation-defined:

printf("%d\n ", 65535);

Yes, because there is no guarantee that 65535 is an int. It may be
a long int. It is outside the guaranteed range of an int.
It's not implementation-defined if INT_MAX is less than 65535, is it?
Anyway, as you probably know, it was a wrong question.

Yevgen
Mar 15 '07 #8
On Wed, 14 Mar 2007 17:24:02 GMT, Yevgen Muntyan
<mu************ ****@tamu.eduwr ote in comp.lang.c:
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 something more subtle so
that output of the following is also implementation-defined:

if (INT_MAX == 65535)
printf("%d", INT_MAX);
else
printf("%d", 65535);

Thanks,
Yevgen
Aside from newline issue, that you posted a correction to, the snippet
above, when packaged within an appropriate program with inclusion of
<stdio.hand <limits.his not strictly conforming, as pointed out in
4 p5 of the C99 standard:

"A strictly conforming program shall use only those features of the
language and library specified in this International Standard. It
shall not produce output dependent on any unspecified, undefined, or
implementation-defined behavior, and shall not exceed any minimum
implementation limit."

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 15 '07 #9
Eric Sosman wrote:
[...]
The value of INT_MAX is implementation-defined, so the
output produced by printf("%d\n", INT_MAX) is implementation-
defined. There is nothing "wrong" with the operation, but
a strictly-conforming program must not perform it.
[...]

So, technically speaking, the following is non-conforming?

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

int main(void)
{
printf("INT_MAX is %d\n",INT_MAX);
return(EXIT_SUC CESS);
}

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Mar 15 '07 #10

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

Similar topics

12
3813
by: sugaray | last post by:
does the expression int a=printf("%d\n",a); implementation dependent ? I supposed it would produced the result 2, but i got 4206596 (result may vary on your machine), i wonder if the value produced was a memory address and anything special with this expression ?
188
17439
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
2358
by: baumann | last post by:
hi all, printf("%c",b) doesn't work properly. #include <stdio.h> int a , b; char d, e; char * p; float f; int main(int argc, char* argv) {
9
2794
by: geek | last post by:
Hi all, Why does a printf("%d") statement prints this as output and when I do printf("%c") it doesn't print anything. -13361016 Any help would be appreciated. Thanks,
19
5466
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
12
43470
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: printf("\n%20s\t%7u\t%13i\t%13i","signed int",sizeof(signed int),INT_MIN,INT_MAX);
10
2370
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi, (void *)pj, sizeof *pi);*/ /*size of what?*/ By the way, will the 3rd argument of memcpy follow the size of the 1st or the 2nd argument? If it follows the size of the 1st argument, unwanted data in the memory area pointed by the 2nd parameter...
7
5234
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. http://groups.google.co.in/group/comp.std.c/browse_thread/thread/a656169cb5941cbf/?hl=en# But I want to know what was the conclusion that has been reached. It is unusually long with 146 posts. Therefore it is hard to follow the
29
11119
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
9657
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9502
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10185
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10132
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9006
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2901
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.