C/C++ compiler ερώτηση

Christos Ricudis ricudis at paiko.net
Sat Jun 23 10:38:56 EEST 2012


On 06/23/2012 09:50 AM, Theodore Lytras wrote:
> Καλησπέρα στη λίστα,
>
> θα ήθελα να κάνω μια ερώτηση, δε ξέρω αν είναι αυτονόητη, απλά ήθελα να είμαι
> σίγουρος.
>
> Σε ένα λογικό έλεγχο της μορφής:
> if (A&&  B) { blabla(); }
> μπορώ να είμαι σίγουρος οτι το A θα ελεγχθεί πάντα πριν από το B, και οτι αν
> το A είναι ψευδές το B δε θα ελεγχθεί καθόλου???
>
> Αντίστοιχα μπορώ να είμαι σίγουρος για το:
> if (A || B) { blabla(); }
> οτι αν το A είναι αληθές το B δε θα ελεγχθεί καθόλου?

Στην C τουλαχιστον, για τους || και && operators, αυτη η συμπεριφορα 
ειναι μερος του προτυπου :

    Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation; there is a sequence point after the
evaluation of the first operand.  If the first operand compares equal
to 0, the second operand is not evaluated.

    Unlike the bitwise | operator, the || operator guarantees
left-to-right evaluation; there is a sequence point after the
evaluation of the first operand.  If the first operand compares
unequal to 0, the second operand is not evaluated.

(http://flash-gordon.me.uk/ansi.c.txt 3.3.13, 3.3.24).

Γι αυτο το λογο, μπορεις να γραψεις αφοβα πραγματα οπως

if (karoto && karoto->color==COL_ORANGE) ...

χωρις να φοβασαι null pointer dereferences.




More information about the Linux-greek-users mailing list