09.01.2015 Views

Langage C - Pages de Michel Deloizy - Free

Langage C - Pages de Michel Deloizy - Free

Langage C - Pages de Michel Deloizy - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

LICENCE SPI-EEA LANGAGE C M. <strong>Deloizy</strong><br />

beaucoup plus efficace en terme <strong>de</strong> rapidité dřexécution ; Par contre, la taille du co<strong>de</strong> généré sera plus<br />

importante.<br />

Exemple :<br />

#<strong>de</strong>fine produit(a,b) a*b<br />

On pourra ensuite utiliser la macro-fonction produit <strong>de</strong> la manière suivante :<br />

y = produit(3.0,4) ;<br />

Le premier paramètre (a) sera remplacé par (3.0), et le second (b) par (4). Le compilateur verra donc la<br />

ligne :<br />

y = 3.0*4 ;<br />

On peut utiliser la macro-fonction produit avec <strong>de</strong>s expressions plus compliquées :<br />

y = produit(3+1,4*2) ;<br />

ce qui sera transposé par :<br />

y = 3+1*4*2 ;<br />

qui, compte tenu <strong>de</strong> la priorité <strong>de</strong>s opérateurs, donne comme résultat 11 au lieu <strong>de</strong> 32 attendu.<br />

Pour éviter ce type <strong>de</strong> problème, il est préférable dřencadrer lřexpression et les paramètres par <strong>de</strong>s<br />

parenthèses lors <strong>de</strong> la définition <strong>de</strong> macro-fonctions :<br />

#<strong>de</strong>fine produit(a,b) ((a)*(b))<br />

Ainsi, la substitution <strong>de</strong> texte donnera la ligne suivante :<br />

y = ((3+1)*(4*2)) ;<br />

qui est cette fois correcte.<br />

XIV.3. Compilation conditionnelle<br />

La compilation conditionnelle permet au programmeur dřexclure ou non <strong>de</strong>s lignes <strong>de</strong> programmes selon la valeur ou<br />

lřexistence <strong>de</strong> constantes.<br />

Exemples :<br />

#if test==1<br />

… lignes source C 1<br />

#else<br />

… lignes source C 2<br />

#endif<br />

optionnel<br />

Les lignes « source C 1 » seront compilées si la constante test vaut 1. Sinon, seules les lignes « source C 2 » seront<br />

compilées.<br />

On peut également utiliser à la place <strong>de</strong> la comman<strong>de</strong> if les comman<strong>de</strong>s :<br />

#if<strong>de</strong>f nom_<strong>de</strong>_constante<br />

#ifn<strong>de</strong>f nom_<strong>de</strong>_constante<br />

Ces comman<strong>de</strong>s permettent <strong>de</strong> tester lřexistence ou non dřune constante et <strong>de</strong> compiler ou non la suite.<br />

Exemples :<br />

#ifn<strong>de</strong>f DEJA_COMP<br />

#<strong>de</strong>fine DEJA_COMP<br />

…<br />

#endif<br />

#if __TURBOC__<br />

#inclu<strong>de</strong> <br />

#endif<br />

Permet <strong>de</strong> ne pas compiler plusieurs fois la même section <strong>de</strong><br />

programme (utile lors <strong>de</strong> l’inclusion <strong>de</strong> fichiers).<br />

Détection <strong>de</strong> la "signature" du compilateur afin d’inclure un<br />

fichier d’en-tête non portable.<br />

-30-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!