1. Macros and Global Variables #

Created Thursday 26 December 2019


Macros #

**Syntax: **

#define macro_template macro_expansion

#define h "Hello, world" // example

Reason/Motivation: use-case: Suppose we want to make a program which works with circles, we need to use π = 3.1416.

Explanation(Working): We need the best of both world’s, i.e both maintainance and performance/checkingfor_correctness. We use #define. Consider the known pre-procesor directive case, #include, this copies file content, from the header files. So we can use all the basic code. #define pi 3.14, is also a pre-processor directive. It replaces each ‘pi’ written in our code by 3.14. This is done before compilation, i.e the compiler is not involved. _It’s basically text substitution in code, before compilation. Compiler will recieve code which has 3.14 written everywhere in place of pi. **Note: **Any errors in the #define mostly go undetected. https://www.geeksforgeeks.org/cc-preprocessors/ About Macros:

Note:

e.g AREA(2+3, 4+5) will be replaced by 2+3*4+5 = 2+12+5 =19


  1. Global Variables

**Syntax: **Just define the variable outside of outside of every function, i.e it’ll be accessible anywhere(assuming it is not shadowed😆️). Reason/Motivation:

**Explanation(Working): ** Basic declaration and memory allocation. It is saved in a different part of the code, it’s lifetime is the same as that of the program. About Global variables: It is a very **bad **practice to use global variables, if other ways like(call by reference are available). It can lead to very unintuitive scenarios, where we will have to check the whole program to find the bugs. **Conceptual problems: **This is conceptually problematic as a variable that is used by all, if mishandled by some function, can jeopardize the whole program.