473,763 Members | 9,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the 'standard' is so strange

This behavior seems very strange to me, but I imagine that someone will
be able to 'explain' it in terms of the famous C standard.

-------------------- code -----------------------------------
#include <stdio.h>

int main (void)
{
char xx[]="abcd";
char * p1 = xx;
char * p2 = xx;
int i;
for(i = 0; i < 4; i++)
printf("%d %c %c\n", i, *p1++, *p1 );
putchar('\n');
for(i = 0; i < 4; i++)
printf("%d %c %c\n", i, *p2, *p2++);
}

-------------------- output -----------------------------------

0 a a
1 b b
2 c c
3 d d

0 b a
1 c b
2 d c
3 d

-------------------------------------------------------------

--
I understand you come from England,
but I promise not to hold it against
you.
Nov 17 '08
20 2238
On Mon, 17 Nov 2008 13:38:40 -0800 (PST), jameskuyper
<ja*********@ve rizon.netwrote:
>Pilcrow wrote:
...
>Before posting my example I had already realised that I was not allowed
(at least in *this* case) to use a shortcut to modify an argument to a
function *within* the parens. I just assumed that the much-cited k&r
had fully defined the language. All these 'undefines' have me feeling
like I'm trying to herd cats. (snakes?)

K&R did not fully define the language. The C89 standard did define it,
but that definition included specifying, with considerable precision,
situations in which the standard does not specify the behavior.
>Now I realise that I will have to read the 'standard', which seems to
have been written by a team composed of Philidelphia lawyers and
abstract mathematicians, neither of whom heard of Henry W Fowler. Truly
gelatinous prose. Technical writing need not be so murky. Oh, well, we
live to learn.

A key thing that you need to understand is that the standard is,
conceptually , a contract between implementors of C and developers of C
programs. If a developer writes strictly conforming code, a conforming
implementati on has to produce exactly the behavior specified by the
standard. The "undefined behavior" that bothers you so much is simply
one of the several methods which the standard uses to identify code
which doesn't meet the contract requirements.

These ways are:
1. The standard requires that a conforming implementation must produce
at least one diagnostic message whenever given a program that contains
any syntax errors or constraint violations. An implementation is also
free to produce diagnostic messages for any other reason that it
wishes. Diagnostics are not required to provide you with any useful
information; they are not even required to be in any language that
anyone knows how to read. However, an implementation is required to
document how to identify diagnostic messages. If you don't find the
messages helpful, that's a legitimate issue to complain about to the
implementor - but it doesn't make the implementation non-conforming.

Having generated that diagnostic, an implementation is free to process
your code a produce a program that you might or might not be willing
to actually execute (I wouldn't).

2. Unspecified behavior: in some cases, the standard allows an
implementati on a range of choices. This is usually not done just for
the fun of it - it's done because existing implementations handle the
situation in different ways, often because the best way to handle the
situation is different on different machines. By making the behavior
unspecified, the standard makes it easier to implement C in an
efficient fashion on a much wider variety of platforms than just about
any other language. The price we pay for this is that we have to be
careful to avoid writing code that depends upon unspecified behavior,
unless we have a very good reason to tie a program to a particular
implementati on of C.

A program whose behavior is unspecified must still behave in one of
permitted ways, an implementation is not free to make it behave in a
completely arbitrary fashion. In many cases, unspecified behavior is
also implementation-defined behavior; in that case, the implementation
is required to document which choice it made. See Annex J.3 for
implementati on-defined behavior; see Annex J.1 for other unspecified
behavior.

3. Undefined behavior: the standard imposes no requirements, of any
kind, on the behavior. An implementation is free to provide it's own
definition of the behavior. If you're deliberately relying upon a
particular implementation' s definition of the behavior, that's fine.
However, if you intend your code to be portable, you must avoid
undefined behavior completely. See Annex J.2 for undefined behavior.
>Maybe I'll try to make a catalog of *all* the 'undefines' and similar
gotchas in C. Written in English.

Virtually every sentence of the standard contains a "gotcha", many of
them contain several "gotchas". To write them out in clear English,
avoiding the technical jargon that makes the standard so difficult to
read, a complete list of the "gotchas" will have to be several times
longer than the standard itself.

...
>BTW, what's a 'sequence point', and how could I recognize one in a dark
alley? Never mind, maybe the *standard* will tell me.

Here's the complete list of sequence points from Annex C:
>— The call to a function, after the arguments have been evaluated (6.5.2.2).
— The end of the first operand of the following operators: logical AND && (6.5.13);
logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17).
— The end of a full declarator: declarators (6.7.5);
— The end of a full expression: an initializer (6.7.8); the expression in an expression
statement (6.8.3); the controlling expression of a selection statement (if or switch)
(6.8.4); the controlling expression of a while or do statement (6.8.5); each of the
expressions of a for statement (6.8.5.3); the expression in a return statement
(6.8.6.4).
— Immediately before a library function returns (7.1.4).
— After the actions associated with each formatted input/output function conversion
specifier (7.19.6, 7.24.2).
— Immediately before and immediately after each call to a comparison function, and
also between any call to a comparison function and any movement of the objects
passed as arguments to that call (7.20.5).
Thank you, sir. That will help.

--
You are a gentleman and a scholar
and a judge of fine whiskey.
Nov 18 '08 #11
On Mon, 17 Nov 2008 15:59:18 -0800, Pilcrow <Pi******@gmail .comwrote:
>On Mon, 17 Nov 2008 13:01:28 -0800, Keith Thompson <ks***@mib.or g>
wrote:
>>Again, why put the word "standard" in scare quotes? It really is the
standard.

because it scares me??
>>
The standard is a technical document. The authors valued precision
over pleasant prose. If you have specific cases where you think the
In my experience, precision *is* pleasant.
>>same ideas could be expressed more clearly, by all means bring them
up; comp.std.c is probably the best place to do so. If you have
specific suggestions for better wording, that's great.
>>Maybe I'll try to make a catalog of *all* the 'undefines' and similar
gotchas in C. Written in English.

Take a look at Annex J of the C99 standard (or n1256 if you don't have
the official standard).

Turns out I have n1256. I thought that was it. Where can I get the
C99, and the other, previous ones? URL, please?
nevermind... found it.
Nov 18 '08 #12
On 17 Nov, 20:26, Pilcrow <Pilcr...@gmail .comwrote:
On Mon, 17 Nov 2008 09:18:44 -0800, Keith Thompson <ks...@mib.or g>
Others have done a very good job of answering your question. But I'm
curious: why do you have such a dismissive attitude regarding the C
standard (putting the words "standard" and "explain" in quotation
marks and so forth)?

Before posting my example I had already realised that I was not allowed
(at least in *this* case) to use a shortcut to modify an argument to a
function *within* the parens. I just assumed that the much-cited k&r
had fully defined the language. All these 'undefines' have me feeling
like I'm trying to herd cats. (snakes?)

To mix metaphores: just when I think I'm beginning to get a grip on C,
it turns into sea, and runs through my fingers.

Now I realise that I will have to read the 'standard', which seems to
have been written by a team composed of Philidelphia lawyers and
abstract mathematicians, neither of whom heard of Henry W Fowler.
the C standard is a model of clarity. And Fowler is over rated.

Truly
gelatinous prose. Technical writing need not be so murky. Oh, well, we
live to learn.
if you're going to be precise in english then a certain murkiness
is inevitable. As Churchill almost said:

"writing a technical standard in english is the worst possible
solution, apart from all the other solutions that have been tried"

Maybe I'll try to make a catalog of *all* the 'undefines' and similar
gotchas in C. Written in English.

Thanks to all for a most illuminating experience.

BTW, what's a 'sequence point', and how could I recognize one in a dark
alley? Never mind, maybe the *standard* will tell me.

It's been real
It's been complex
--
Nick Keighley

[begin quote]
5.4.4.2. Semantics
A MOID-NEST-jump J, in an environ E, is elaborated as follows:
- let the scene yielded in E by the label-identfier of J be composed
of a series
S2 and an environ E1;
Case A:
MOID is not any procedure yielding MOID1:
- let S1 be the series of the smallest {1.1.3.2.g} serial-clause
containing
S2;
- the elaboration of S1 in E1, or of any series in E1 elaborated
in its
place, is terminated {2.1.4.3.e};
- S2 in E1 is elaborated \in place of" S1 in E1;
Case B:
MOID is some procedure yielding MOID1:
- J in E {is completed and} yields the routine composed of
(i) a new MOID-NEST-routine-text whose unit is akin {1.1.3.2.k} to
J,
(ii) E1.

[...]

10.3. Transput declarations
{ "So it does!" said Pooh, "It goes in!"
"So it does!" said Piglet, "And it comes out!"
"Doesn't it?" said Eeyore, "It goes in and out like
anything,"
Winnie-the-Pooh, A.A. Milne.}

[end quote]

Both from "Revised Report on the Algorithmic Language ALGOL 68"
Nov 18 '08 #13
In article <7a************ *************** *******@r36g200 0prf.googlegrou ps.com>,
Nick Keighley <ni************ ******@hotmail. comscrewed up again
thusly:
....
>the C standard is a model of clarity. And Fowler is over rated.
In much the same way that Bridgit Bardot (in her prime) was a model of
ugliness. I.e., to get the concept, you conceive of the exact opposite
of the "model".

Nov 18 '08 #14
In article <gf**********@n ews.xmission.co mga*****@shell.x mission.com (Kenny McCormack) writes:
....
Bridgit Bardot (in her prime)
Who is that?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 18 '08 #15
Dik T. Winter wrote:
In article <gf**********@n ews.xmission.co m>
ga*****@shell.x mission.com (Kenny McCormack) writes: ...
> Bridgit Bardot (in her prime)

Who is that?
A typo. Actually three, her firstname is Brigitte
Nov 18 '08 #16
Joachim Schmitz wrote:
Dik T. Winter wrote:
>ga*****@shell.x mission.com (Kenny McCormack) writes:
>>Bridgit Bardot (in her prime)

Who is that?

A typo. Actually three, her firstname is Brigitte
I think we should offer sympathy to Mr Winter.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Nov 19 '08 #17
CBFalconer wrote:
Joachim Schmitz wrote:
>Dik T. Winter wrote:
>>ga*****@shell.x mission.com (Kenny McCormack) writes:

Bridgit Bardot (in her prime)

Who is that?

A typo. Actually three, her firstname is Brigitte

I think we should offer sympathy to Mr Winter.
For being to young to have seen BB in her prime? Nah...

Bye, Jojo
Nov 19 '08 #18
On 17 Nov, 22:55, Stephen Sprunk <step...@sprunk .orgwrote:
Pilcrow wrote:
On Mon, 17 Nov 2008 09:18:44 -0800, Keith Thompson <ks...@mib.or g>
wrote:
C is indeed less well-defined than most other languages.
are you sure about this? Might it be that C is better at
documenting its dark corners?

I've seen it argued[1] that the Ada standard has more open issues than
the C standard (this was some time ago so the holes may have been
plugged now).

[1] I can't remember exactly where something like "secure C"
by Hatton (I couldn't find it by google). Ah! Wikipedia for "Hatton".
"Safer C" by Les Hatton

[beware: link is broken]
http://www.amazon.co.uk/Safer-High-I...7092931&sr=8-1
>*The reason is
that C evolved and branched on its own long before the standards bodies
got their hands on it, and different implementors had wildly different
ideas about what different things meant. *As a result, ANSI (and thus
ISO) was reduced to trying to document the areas where most or all of
them agreed and leaving the areas of disagreement undefined.

Also, C was always intended to be as efficient as possible, and what
behavior is most efficient on different systems varies. *Finally, one of
C's greatest strengths is the ability to write both portable code (such
as cross-platform applications) and unportable code (such as device
drivers) in the same language; the way this is done is by allowing
"undefined" and "implementa tion-defined" behavior, which can be avoided
by anyone trying to write portable code but embraced by those who don't
care about portability.
<snip>

--
Nick Keighley

"The Dinosaurs have come and gone,
we Theriodonts remain"
Nov 19 '08 #19
In article <gg*********@on line.de"Joachim Schmitz" <jo**@schmitz-digital.dewrite s:
CBFalconer wrote:
Joachim Schmitz wrote:
Dik T. Winter wrote:
ga*****@shell.x mission.com (Kenny McCormack) writes:

Bridgit Bardot (in her prime)

Who is that?

A typo. Actually three, her firstname is Brigitte
I think we should offer sympathy to Mr Winter.

For being to young to have seen BB in her prime? Nah...
Tsk. At least she was refreshing after DD. But since her retirement some
35 years ago she has become politically involved, although not exactly correct.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 19 '08 #20

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

Similar topics

16
1950
by: Pjer Holton | last post by:
If I were to build a Windows application that is a true standard Windows application in every conceivable way and that adheres to the MS Windows standards as much as possible (installation, GUI, printing, registry, RTF etc.), and if portability to other platforms is only a minor concern, ... would Python be a good choice? What packages, libraries and modules would you recommend for GUI, installation, data storage etc.? Thank you very...
25
3357
by: BJörn Lindqvist | last post by:
See: http://www.wxpython.org/quotes.php. especially: "wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard Python GUI toolkit is that Tkinter was there first." - Guido van Rossum Guess, that answers my question, but isn't "Tkinter was there first" a very bad answer? :) It is kinda ugly too, so I wonder why it can't be replaced? Or maybe another GUI...
0
2166
by: Hugo Fjelsted Alrøe | last post by:
I am fairly new at MySQL, so please excuse any ignorance. We are using MySQL (version 3.23.48) in connection with an Eprints = archive. We have non-english deposits in the archive, and non-standard = characters are not always shown right in our system. They are shown = wrong in the subscription alerts that the archive generates and sends = out - this is what annoys us. And they are shown wrong in the database = itself. But they are shown...
8
2922
by: Sensei | last post by:
I have a quick question about the math library included in the standard C90 (and 99). The gcc, xlc and possibly other compilers/linkers on some unix platforms cannot use any math functions in math.h if a switch (-lm) is used, so *explicitly* having the linker use a file called libm.so or libm.a/dylib/whatever. Is it true on all the platforms? (answer: probably no)
52
3787
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low level machine. C stands for portability and platform and machine independent. If the C compiler and C standard library are written in C itself, is it possible that one "standard" C compiler plus library is enough? The standard implementation is...
132
4647
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C community. It would seem that C99 is the most up-to-date Standard, but far more people seem to be working off the C89 Standard. Could someone please explain to me why this is so? --
21
2840
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value of x is: %d\n", x);
26
3825
by: Rick | last post by:
I'm told that "#pragma once" has made it into the ISO standard for either C or C++. I can't find any reference to that anywhere. If it's true, do any of you have a reference I can use? Thanks...
130
6794
by: euler70 | last post by:
char and unsigned char have specific purposes: char is useful for representing characters of the basic execution character set and unsigned char is useful for representing the values of individual bytes. The remainder of the standard integer types are general purpose. Their only requirement is to satisfy a minimum range of values, and also int "has the natural size suggested by the architecture of the execution environment". What are the...
0
9564
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
9387
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
10148
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...
1
9938
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
8822
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
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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...
1
3917
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
2794
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.