tell me what is the disadvantage of #define? and what do u mean by run time error and compile time error??
Hi ankitsjain #define is not type safe #define's replaces any code that follows that in the main program where ever they are referred to.So more the code you place after #define more the compile time. Errors that arise during compilation due to syntax,semantics,type mismatch,etc.For example int main(void) { int a; //no closing braces Errors that arise at runtime.eg.stack overflow errors,errors due to insufficient memory,opening a file that does not exist,etc. Happy programming
Hi, Disadvantages of #define, 1. Preprocessor not doing type checking 2. May not store variable in symbol table 3. Need to care for parenthesis 4. Preprocessor hard code #define variable. Code size become bulky 5. Any changes in macro , need to re-compile all source code. 1. Compile Time error Error encounter while compiling program by compiler, like syntax error, type checking errors etc. For example, Declare preprocessor variable in header file and same use in another source file. At time of compilation, compiler never know about preprocessor variable as preprocessor replace it with it's macro value and Preprocessor variable may not store into symbol table for debugging purpose. It make sometime difficult to trace error root point if we had not written header file. 2. Run time Error Error encounter at time of executing program by operating system, like referencing null pointer, insufficient memory for allocation etc. Example, If macro body has not taken care with parenthesis on mathematical operations. Macro Example : Code: #include <iostream> using namespace std; #define MAX_SIZE 1000000000 #define FUNC(a,b) a*b int main() { //1. Compile Time Error int* pArr = new int[MAX_SIZE]; //2. Run Time Error cout<<FUNC(2+1,5+1); cin.get(); return 0; }
Because a-b=0, so following will happen run time error: int a=3; int b=3; int c=3/(a-b); When compile it, it can't discover a-b=0; The following will happen compile time error: int a=3; double b=3.1415; a=b;