473,801 Members | 2,582 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
26 6872
Kenneth Brody <ke******@spamc op.netwrites:
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);
}
It's not strictly conforming, but it is conforming:

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

...
A conforming program is one that is acceptable to a
conforming implementation. 4)

...
4) Strictly conforming programs are intended to be maximally
portable among conforming implementations . Conforming
programs may depend upon nonportable features of a
conforming implementation.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Mar 15 '07 #11
Kenneth Brody wrote On 03/15/07 10:46,:
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);
}
It is conforming, but not strictly conforming. From
section 4, paragraphs 5 and 7 and footnote 4:

A /strictly conforming program/ [...] shall not
produce output dependent on [...] implementation-
defined behavior [...]

A /conforming program/ is one that is acceptable to
a conforming implementation. 4)

4) Strictly conforming programs are intended to be
maximally portable among conforming implementations .
Conforming programs may depend upon nonportable
features of a conforming implementation.

The distinction between strictly conforming and conforming
programs is one that the Standard is pretty much forced to make,
but it has always seemed to me that the Standard's definition
of strict conformance is too restrictive to be as useful as it
should be. When a program like Kenneth Brody's turns out not
to be strictly conforming, I think the baby has been thrown out
with the bath water. But I have no better definition to offer,
so I can't complain very convincingly!

When the ANSI Standard was young there used to be a lot of
discussions in c.l.c. about just what "strictly conforming" and
"conforming " were, with reams of writing claiming to explicate
the Real Intent of the writers, sort of like lawyers trying to
find an inherent right to broadband in the U.S. Constitution.
Among these cacophonies of opinion one would occasionally find
"proofs" of the non-existence of strictly conforming programs,
or of "non-silly" strictly conforming programs. For example:

- An S.C. program cannot use the #include directive except
for the headers described in the Standard itself, because
the search algorithm for other headers is implementation-
defined. Since the effect of #include "foo.h" is defined
by the implementation, a program that uses it depends on
implementation-defined behavior.

- An S.C. program cannot terminate, because its termination
status is part of its "output" and has an implementation-
defined form, hence a program that terminates produces
output that depends on implementation-defined behavior.

- An S.C. program cannot produce any output at all, because
the output is rendered in an implementation-defined
character encoding.

- An S.C. program cannot allow its output to be affected by
the success or failure of any particular malloc() call,
because the conditions under which malloc() succeeds or
fails are implementation-defined. An S.C. program can
call malloc(), but even if every single malloc() call
returns NULL its output must be unchanged.

.... and so on. Some arguments of this sort were probably raised
for the purpose of amusement (like the fanciful descriptions of
undefined behavior that used to be popular; "demons will fly from
your nose" was just one of many). Some were probably raised to
mock the newly-minted Standard. Some may have been intended as
serious criticisms. All seem to me to have had tiny grains of
truth (however distorted), enough grains to suggest that this
part of the Standard may have feet of slowly-crumbling clay.

--
Er*********@sun .com
Mar 15 '07 #12
Eric Sosman wrote:
>
Kenneth Brody wrote On 03/15/07 10:46,:
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);
}

It is conforming, but not strictly conforming. From
section 4, paragraphs 5 and 7 and footnote 4:

A /strictly conforming program/ [...] shall not
produce output dependent on [...] implementation-
defined behavior [...]

A /conforming program/ is one that is acceptable to
a conforming implementation. 4)

4) Strictly conforming programs are intended to be
maximally portable among conforming implementations .
Conforming programs may depend upon nonportable
features of a conforming implementation.

The distinction between strictly conforming and conforming
programs is one that the Standard is pretty much forced to make,
but it has always seemed to me that the Standard's definition
of strict conformance is too restrictive to be as useful as it
should be. When a program like Kenneth Brody's turns out not
to be strictly conforming, I think the baby has been thrown out
with the bath water. But I have no better definition to offer,
so I can't complain very convincingly!
Kenneth Brody's program
is what the C standard calls "a correct program".

N869
4. Conformance
[#3] A program that is correct in all other aspects,
operating on correct data, containing unspecified behavior
shall be a correct program and act in accordance with
5.1.2.3.

Even though the term isn't used that often on this newsgroup,
"correct programs"
is what I consider this newsgroup to be mostly about.

Ben Pfaff likes the term "correct program".

"When I wrote my earlier article in this thread, I knew that
"strictly conforming" was not a perfect term, but I didn't have a
better one and didn't feel like adding a lot of qualifiers or an
extended explanation."

http://groups.google.com/group/comp....807153d40349c7

--
pete
Mar 16 '07 #13
Eric Sosman wrote:
>
.... snip ...
>
The distinction between strictly conforming and conforming
programs is one that the Standard is pretty much forced to make,
but it has always seemed to me that the Standard's definition
of strict conformance is too restrictive to be as useful as it
should be. When a program like Kenneth Brody's turns out not
to be strictly conforming, I think the baby has been thrown out
with the bath water. But I have no better definition to offer,
so I can't complain very convincingly!
I don't think a strictly conforming *program* exists, but many
strictly conforming *routines* do exist. I.E. those routines that
can be implemented anywhere.

--
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 16 '07 #14
CBFalconer <cb********@yah oo.comwrites:
Eric Sosman wrote:
>>
... snip ...
>>
The distinction between strictly conforming and conforming
programs is one that the Standard is pretty much forced to make,
but it has always seemed to me that the Standard's definition
of strict conformance is too restrictive to be as useful as it
should be. When a program like Kenneth Brody's turns out not
to be strictly conforming, I think the baby has been thrown out
with the bath water. But I have no better definition to offer,
so I can't complain very convincingly!

I don't think a strictly conforming *program* exists, but many
strictly conforming *routines* do exist. I.E. those routines that
can be implemented anywhere.
The standard doesn't talk about strictly conforming routines, and I
think the phrase "strictly conforming" is so narrowly defined that we
should avoid using it other than in the sense specified in the
standard. The idea of "routines that can be implemented anywhere" is
certainly a useful one, though; perhaps that's the same as "routines
(function?) that can potentially be used in a strictly conforming
program".

There's some controversy about what is or is not a "strictly
conforming program", but here's a program that I think meets the
definition unambiguously:

int main(void) { return 0; }

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 16 '07 #15
CBFalconer wrote, On 15/03/07 21:52:
Eric Sosman wrote:
... snip ...
> The distinction between strictly conforming and conforming
programs is one that the Standard is pretty much forced to make,
but it has always seemed to me that the Standard's definition
of strict conformance is too restrictive to be as useful as it
should be. When a program like Kenneth Brody's turns out not
to be strictly conforming, I think the baby has been thrown out
with the bath water. But I have no better definition to offer,
so I can't complain very convincingly!

I don't think a strictly conforming *program* exists,
<pedantic>
What about:
int main(void)
{
return 0;
}
</pedantic>
but many
strictly conforming *routines* do exist. I.E. those routines that
can be implemented anywhere.
I think we can deal with code that is not strictly conforming within
certain limits. Such as, to take a simple example, a program printing
values from limits.h
--
Flash Gordon
Mar 16 '07 #16
Flash Gordon wrote On 03/16/07 14:48,:
CBFalconer wrote, On 15/03/07 21:52:
>>Eric Sosman wrote:
... snip ...
>> The distinction between strictly conforming and conforming
programs is one that the Standard is pretty much forced to make,
but it has always seemed to me that the Standard's definition
of strict conformance is too restrictive to be as useful as it
should be. When a program like Kenneth Brody's turns out not
to be strictly conforming, I think the baby has been thrown out
with the bath water. But I have no better definition to offer,
so I can't complain very convincingly!

I don't think a strictly conforming *program* exists,


<pedantic>
What about:
int main(void)
{
return 0;
}
</pedantic>
Sorry: It returns an implementation-defined form of
"success" to the host environment. Something that goes
from the program to the environment is surely "output,"
hence the program produces implementation-defined output.

;-)
I think we can deal with code that is not strictly conforming within
certain limits. Such as, to take a simple example, a program printing
values from limits.h
Nailing down the "certain limits" is the hard part,
the part for which I at least have nothing constructive
to offer.

"I shall not today attempt further to define the
kinds of material I understand to be embraced
within that shorthand description; and perhaps I
could never succeed in intelligibly doing so. But
I know it when I see it, [...]"
-- Justice Potter Stewart

--
Er*********@sun .com
Mar 16 '07 #17
Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
.... snip ...
>>
I don't think a strictly conforming *program* exists, but many
strictly conforming *routines* do exist. I.E. those routines that
can be implemented anywhere.

The standard doesn't talk about strictly conforming routines, and I
think the phrase "strictly conforming" is so narrowly defined that
we should avoid using it other than in the sense specified in the
standard. The idea of "routines that can be implemented anywhere"
is certainly a useful one, though; perhaps that's the same as
"routines (function?) that can potentially be used in a strictly
conforming program".
For many years I have made a practice of trying to isolate routines
that operate only on memory from something that affects
peripherals, in any language. For example, I may have a routine
that outputs a char, and one that converts a nibble to hex, I
would break this up (in assembly) to:

push acc
shr 4
call hexcvt
call cout
pop acc
call hexcvt
call cout

and use of 'fallthru' can shorted the overall code. This might
give me couthx as:

couthx: call hexcvt
; " " (fall thru)
cout: whatever

reducing the above to:

push acc
shr 4
call couthx
pop acc
; " " (fall thru)
couthx:

You can't apply the fall thru strategies in C, but the rest
applies.

--
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 16 '07 #18
Eric Sosman <Er*********@su n.comwrites:
Flash Gordon wrote On 03/16/07 14:48,:
>CBFalconer wrote, On 15/03/07 21:52:
[...]
>>>I don't think a strictly conforming *program* exists,


<pedantic>
What about:
int main(void)
{
return 0;
}
</pedantic>

Sorry: It returns an implementation-defined form of
"success" to the host environment. Something that goes
from the program to the environment is surely "output,"
hence the program produces implementation-defined output.

;-)
I feel reasonably confident that the comittee did not intend the
concept of "strictly conforming" to be quite *that* useless.

However:

int main(void) { while (1); }

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 16 '07 #19
Keith Thompson wrote, On 16/03/07 21:50:
Eric Sosman <Er*********@su n.comwrites:
>Flash Gordon wrote On 03/16/07 14:48,:
>>CBFalconer wrote, On 15/03/07 21:52:
[...]
>>>I don't think a strictly conforming *program* exists,

<pedantic>
What about:
int main(void)
{
return 0;
}
</pedantic>
Sorry: It returns an implementation-defined form of
"success" to the host environment. Something that goes
from the program to the environment is surely "output,"
hence the program produces implementation-defined output.

;-)

I feel reasonably confident that the comittee did not intend the
concept of "strictly conforming" to be quite *that* useless.

However:

int main(void) { while (1); }
It even still leaves us something to discuss. I prefer
int main(void)
{
for (;;)
continue;
}

:-)
--
Flash Gordon
Mar 17 '07 #20

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

Similar topics

12
3818
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
17463
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
2361
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
2797
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
5469
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
12
43475
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
2374
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
5237
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
11126
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
10524
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10298
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
10278
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
9105
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
5490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4265
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
2
3786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2963
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.