Bifalgorithm post · Computer science one · Comments

How does the #define macro create an alias for a value?

How does the #define macro create an alias for a value? (Computer science one · Comments) — "#define MILES_PER_KM 1.609\n\nThe macro defines an “alias” for the…

Ideas in this flowchart

  • Les constantes macros en C donnent des noms clairs à des valeurs réutilisables.
  • #define MILES_PER_KM 1.609 associe l’identifiant MILES_PER_KM à la valeur 1.609.
  • Le préprocesseur remplace chaque occurrence de MILES_PER_KM par 1.609.
  • L’identifiant évite d’utiliser des nombres magiques dont le sens est difficile à comprendre.
  • Une modification de la valeur se fait uniquement dans la définition de la macro.
  • Les constantes macros utilisent généralement des majuscules et des traits de soulignement.
  • La bibliothèque mathématique standard fournit aussi des macros pour des constantes courantes.
  • Des exemples sont M_PI, M_E et M_SQRT2, représentant π, e et √2.
Feed/

How does the #define macro create an alias for a value?

No cover

Channel

Computer science one

1 page

"#define MILES_PER_KM 1.609\n\nThe macro defines an “alias” for the MILES_PER_KM identifier as the value 1.609. Essentially, the C preprocessor will go through the code and any instance of MILES_PER_KM will be replaced with 1.609. The advantage of using a macro like this is that we can use the identifier MILES_PER_KM throughout our program instead of magic numbers whose meaning and intent may not be immediately clear. Moreover, if we want to change the definition (say make it more precise, using 1.60934 instead) then we only need to change the macro instead of making the same change throughout our program. As a stylistic note: macro constants in C are usually associated with uppercase underscore casing as in our example. Also, the math standard library defines several macros for common mathematical constants such as π, e, and √2 (M_PI, M_E, and M_SQRT2 respectively) among others."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.