the ++ operator

Steve Stavropoulos steve at math.upatras.gr
Fri Nov 21 04:20:01 EET 2003


On Thu, 20 Nov 2003, Vasilis Vasaitis wrote:

>   Πωπω, είστε τελείως φονιάδες και οι δύο. Τέτοιες παραστάσεις σαν
> αυτές που γράφετε για το c += ... είναι τελείως άτοπες, γιατί πολύ
> απλά δεν υπάρχει καμία εγγύηση για τη σειρά με την οποία θα υπολογίσει
> ο compiler τα επιμέρους ορίσματα της πρόσθεσης. Δηλαδή, όταν γράφουμε:
> 
> 	c = a[i] + b[i++];
> 
> τότε ο compiler δεν υποχρεούται να υπολογίσει τα a[i] και b[i++] με
> κάποια συγκεκριμένη σειρά, οπότε στο a[i] μπορεί το i να έχει ήδη
> προσαυξηθεί, μπορεί και όχι. Φυσικά, το ίδιο ακριβώς ισχύει αν έχουμε
> ++i, δεν αλλάζουν και πολλά. Η ουσία είναι ότι τέτοιες παραστάσεις
> έχουν undefined behaviour, όπως λέμε, δηλαδή ο compiler κάνει ό,τι
> γουστάρει, οπότε δε βασιζόμαστε και τις γράφουμε κάπως αλλιώς.
> 

 La8os! O compiler symfwna me thn ansi c exei _plhrws_ defined symperifora 
otan blepei to ++ ws prefix h postfix. Otan einai prefix tote to 
increment 8a ginei PRIN to evaluation ths ypoloiphs protashs, enw otan 
einai postfix tote to increment 8a ginei META to evaluation ths ypoloiphs 
protashs.
 Sto "The C programming language (ansi C)" second edition, page 46, twn 
Kernighan kai Richie anaferete:
 "But the expression ++n increments n _before_ its value is used, while 
n++ increments n _after_ its value has been used. This means that in a 
context where the value is being used, not just the effect, ++n and n++ 
are different. If n is 5, then
	x = n++;
sets x to 5, but
	x = ++n;
sets x to 6. In both cases, n becomes 6."




More information about the Linux-greek-users mailing list