435,571 Members | 3,168 Online
Need help? Post your question and get tips & solutions from a community of 435,571 IT Pros & Developers. It's quick & easy.

# the 'standard' is so strange

 P: n/a 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 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 #1
20 Replies

 P: n/a Pilcrow wrote: printf("%d %c %c\n", i, *p1++, *p1 ); That line of code makes your program be undefined. You modify p1 and read it twice without an intervening sequence point, to get an address of an object that will have its value printed. printf("%d %c %c\n", i, *p2, *p2++); That one too. N869 6.5 Expressions [#2] Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. -- pete Nov 17 '08 #2

 P: n/a Pilcrow 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 ); It is not specified when the effect "p1++" will happen. Therefore "*p1" is meaningless in this context. The compiler can do what it likes. putchar('\n'); for(i = 0; i < 4; i++) printf("%d %c %c\n", i, *p2, *p2++); Again, it is not specified when the effect "p2++" will happen. Likewise, "*p2" is meaningless in this context. Again, the compiler can do what it likes. } -------------------- output ----------------------------------- 0 a a 1 b b 2 c c 3 d d It appears that the compiler chose to evaluate the arguments to printf from the right to the left, or to not perform the incrementation of p1 until after all arguments were evaluated. You were lucky, much worse things could have happened. 0 b a 1 c b 2 d c 3 d It appears that the compiler chose to evaluate the arguments to printf from the right to the left, and to perform the incrementation of p2 in the rightmost argument before proceeding to evaluate the argument to the left of it. Again, you were lucky, much worse things could have happened. Combining those two data points, I think we can concluded that the compiler choses to evaluate its arguments from right to left and to perform the side-effects associated with each argument as it processes that argument. It is permitted to so do. It it not oblided to so do. The compiler's behaviour does not in any way seem "very strange", it simply appears undefined. The only strange behaviour is that of the author of the code thinking he could get away with writing code with undefined behaviour. Phil -- I tried the Vista speech recognition by running the tutorial. I was amazed, it was awesome, recognised every word I said. Then I said the wrong word ... and it typed the right one. It was actually just detecting a sound and printing the expected word! -- pbhj on /. Nov 17 '08 #3

 P: n/a Pilcrow 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 ------------------------------------------------------------- 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)? -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 17 '08 #4

 P: n/a On Mon, 17 Nov 2008 09:18:44 -0800, Keith Thompson wrote: >Pilcrow This behavior seems very strange to me, but I imagine that someone willbe able to 'explain' it in terms of the famous C standard.-------------------- code -----------------------------------#include 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 a1 b b2 c c3 d d0 b a1 c b2 d c3 d------------------------------------------------------------- Others have done a very good job of answering your question. But I'mcurious: why do you have such a dismissive attitude regarding the Cstandard (putting the words "standard" and "explain" in quotationmarks 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. Truly gelatinous prose. Technical writing need not be so murky. Oh, well, we live to learn. 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 -- I understand you come from England, but I promise not to hold it against you. Nov 17 '08 #5

 P: n/a In article , Keith Thompson Maybe you'' should "read" the 'standard' before you *complain* about/it/. You are such a tool. Nov 17 '08 #6

 P: n/a Pilcrow wrote: > 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 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 ); From this point on your code is undefined. You have raised undefined performance here. The compiler is entitled to do whatever it wishes. Look up sequence points, and restrictions on code between them. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: Try the download section. Nov 17 '08 #8

 P: n/a Pilcrow wrote: Keith Thompson >Take a look at Annex J of the C99 standard (or n1256 if youdon'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? n1256 is more accurate than the printed versions you can buy. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: Try the download section. Nov 18 '08 #10

 P: n/a On Mon, 17 Nov 2008 15:59:18 -0800, Pilcrow On Mon, 17 Nov 2008 13:01:28 -0800, Keith Thompson wrote: >>Again, why put the word "standard" in scare quotes? It really is thestandard. because it scares me?? >>The standard is a technical document. The authors valued precisionover 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 themup; comp.std.c is probably the best place to do so. If you havespecific suggestions for better wording, that's great. >>Maybe I'll try to make a catalog of *all* the 'undefines' and similargotchas in C. Written in English. Take a look at Annex J of the C99 standard (or n1256 if you don't havethe official standard). Turns out I have n1256. I thought that was it. Where can I get theC99, and the other, previous ones? URL, please? nevermind... found it. Nov 18 '08 #12

 P: n/a On 17 Nov, 20:26, Pilcrow 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

 P: n/a In article <7a**********************************@r36g2000prf. googlegroups.com>, Nick Keighley 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

 P: n/a In article

 P: n/a Dik T. Winter wrote: In article ga*****@shell.xmission.com (Kenny McCormack) writes: ... > Bridgit Bardot (in her prime) Who is that? A typo. Actually three, her firstname is Brigitte Nov 18 '08 #16

 P: n/a Joachim Schmitz wrote: Dik T. Winter wrote: >ga*****@shell.xmission.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]: Try the download section. Nov 19 '08 #17

 P: n/a CBFalconer wrote: Joachim Schmitz wrote: >Dik T. Winter wrote: >>ga*****@shell.xmission.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

 P: n/a On 17 Nov, 22:55, Stephen Sprunk 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 "implementation-defined" behavior, which can be avoided by anyone trying to write portable code but embraced by those who don't care about portability. -- Nick Keighley "The Dinosaurs have come and gone, we Theriodonts remain" Nov 19 '08 #19

 P: n/a In article

 P: n/a Dik T. Winter wrote: In article CBFalconer wrote: >>Joachim Schmitz wrote:Dik T. Winter wrote:ga*****@shell.xmission.com (Kenny McCormack) writes:>>Bridgit Bardot (in her prime)>Who is that?A typo. Actually three, her firstname is BrigitteI 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. Yes, I'm old enough to decipher that 8-)) But since her retirement some 35 years ago she has become politically involved, although not exactly correct. Partly true indeed... (her fight against seal hunting was OK, IMHO) Bye, Jojo Nov 19 '08 #21

### This discussion thread is closed

Replies have been disabled for this discussion.