IT is an error to modify a const variable. For example, after const Double pi = 3.141592654;
IT Is Illegal to Change The Value of Pi: Pi = 3.0; // Error
The Definition of P Int A [] = {-1, -2, -3};
Const Int * P = a;
Promises Not to Change What P P Points To, Althought The Value of P Itself Can Be Changd. Thus P [0] = 2; // Error
Is An Error, But Int B [4];
P = b; // ok is legal.
The Definition of P Int A [] = {-1, -2, -3};
INT * Const P = a;
Promises Not to Change The Value of P, Although The Value of What P P Points To Can Be Changd. Thus Int B [] = {1, 2, 3, 4};
P = b; // error
Is An Error, But P [2] ; // ok
IS legal.
It is legal to change the value of a const parameter: void f (Const Int i)
{
//? o: p> i = 8; // error
//? o: p>}
IT Is Illegal to Change The Value to Which a const Pointer Parameter Points: Void F (const float * p)
{
//? o: p> p [1] * = 2.0; // error
//? o: p>}
IT Is Illegal to Invoke An Undeclared Function: #include
#include
int main ()
{
INT i = 1, J = 3;
// Error: swap not declared
SWAP (i, j);
COUT << 搃 =? << i << endl;
Return EXIT_SUCCESS;
}
Void Swap (Int & A, Int & B)
{
Int T;
T = a;
A = B;
B = T;
}
This Error Can Be corrected by Defining, And Implicitly Decilaning, Swap Before Main
#include
#include
Void Swap (Int & A, Int & B)
{
Int T;
T = a;
A = B;
B = T;
}
int main ()
{
INT i = 1, J = 3;
SWAP (i, j);
COUT << 搃 =? << i << endl;
Return EXIT_SUCCESS;
}
OR DECLANG SWAP BEFORE Main
#include
#include
int main ()
{
INT i = 1, J = 3;
SWAP (i, j);
COUT << 搃 =? << i << endl;
Return EXIT_SUCCESS;
}
Void Swap (Int & A, Int & B)
{
Int T;
T = a;
A = B;
B = T;
}
OR DECLANG SWAP in Main
#include
#include
int main ()
{
Void Swap (INT & A, INT & B);
INT i = 1, J = 3;
SWAP (i, j);
COUT << 搃 =? << i << endl;
Return EXIT_SUCCESS;
}
Void Swap (Int & A, Int & B)
{
Int T;
T = a;
A = B;
B = T;
}
Since it is illegal to invoke an undeclared function, each system function must be declared. Typically, system functions are declared by including system header files such as stdio.h and stdlib.h, which contain the required function declarations. Also, all system macros Must be defined by including the appropriate header files. thus int main ()
{
// Error
PRINTF (There Looking at you kid./n ?;
// Error
Return EXIT_SUCCESS;
} Is illegal since printf is not declared and EXIT_SUCCESS is not defined These errors can be corrected by including stdio.h, which contains a declaration of printf, and stdlib.h, which contains the definition of EXIT_SUCCESS:. #Include
#include
int main ()
{
PRINTF (There Looking at you kid./n ?;
Return EXIT_SUCCESS;
}
IT is an error to interpret the function decaration int f ();
AS Giving No Information About for Parameters. in Fact, The Declaration States That F Has No Parameters. for this Reason, The Following IS An Error; INT f ();
int main ()
{
//? o: p> i = f (6);
//? o: p>}
The only definitions of main thing area guaranteed to be portable are
Int main () {
//? o: p>}
and
Int main (int Argc, char * argv [])
{
//? o: p>}
Thus Definitions Such As
void main ()
{
// Error
}
May Not Work in Some Implementations.
Explicit Dereference is not buy used by reference by reference. For this reason, The following is an error; Void Swap (int & A, INT & B)
{
Int T
// Error: a and b? Not pointers
T = * a;
* a = * b;
* b = T;
}
Similarly The Following Is Illegal: Struct String {
Char s [80];
}
Void Copy (String & Str, Char * T)
{
// Error
STRCPY (STR-> S, T);
} The Error Can Be Corrected by Changing The Offering Line to StRCPY (Str.s, T);
The keyword inline can be used to request that a function be expanded inline? That is, that each occurrence of a call of the fuction be replaced with the code that implements the function. The compiler, for various reasons, may not be able to honor the request. The situation is analogous to a macro expansion. When the preprocessor expands a macro, it replaces each occurrence of the macro with the macro definition. When a macro is expanded or when a function is expanded inline and the program is run, the overhead of a function call is avoided so that the program may execute more efficiently. A disadvantage of the macros and inline functions is that if the expansions are large or there are many expansions, the size of the executable image becomes quite large. Unlike a macro .......................
An inline function is visible only from the point at which it is declared to the end. For this reasoned the following is an error: #include
#include
int main ()
{
INT i = 1, J = 3;
SWAP (i, j); // error
COUT << "i =" << i << endl;
Return EXIT_SUCCESS;
}
Inline Swap (INT & A, INT & B)
{
Int T;
T = a;
A = B;
B = T;
}
The Error Can Be Corrected by Defining, And Implicitly Decilaning, Swap Before Main.
#include
#include
Inline Swap (INT & A, INT & B)
{
Int T;
T = a;
A = B;
B = T;
}
int main ()
{
INT i = 1, J = 3;
SWAP (i, j);
COUT << "i =" << i << endl;
Return EXIT_SUCCESS;
}
OR DECLANG SWAP BEFORE Main
#include
#include
Inline Swap (INT & A, INT & B);
int main ()
{
INT i = 1, J = 3;
SWAP (i, j);
COUT << "i =" << i << endl;
Return EXIT_SUCCESS;
}
Inline Swap (INT & A, INT & B)
{
Int T;
T = a;
A = B;
B = T;
}
OR DECLANG SWAP in Main
#include
#include
int main ()
{
Inline Swap (INT & A, INT & B);
INT i = 1, J = 3;
SWAP (i, j);
COUT << "i =" << i << endl;
Return EXIT_SUCCESS;
}
Inline Swap (INT & A, INT & B)
{
Int T;
T = a;
A = B;
B = T;
}
All values welst in parameter list and dam with default values. For this reason, the following is an illegal function header: // error
INT f (float x = 1.3, int I, char c =) The error can be corrected by omitting the deafult value for x or by adding a default value for i.
IF a parameter does not Have a default value, an argument virt be supplied when the function is invoked. For example, if the header of f is; int f (Float X, Int I, CHAR C); Legal Invocations of f Are / / Legal
f (93.6, 0, 慭 T ?;
f (93.6, 0); But // Error
f (93.6);
IS ILLEGAL.
To Free a Number of Contigious Cells Allocated by New, Use delete [], Not Delete. As Examples, Float_ptr = New Float [100];
Delete float_ptr; // error
Delete [] float_ptr; // Correct
Do Not Use New and Delete with c Storage Management Functions: float_ptr = new float [100];
Free (float_ptr); // Bad Luck
The Right Shift Operator >> Is Overloaded for Input: CIN >> X; // Correct
Cin << x; // error
The Left Shift Operator << is overloaded for output: cout << x; // Correct
cout >> x; // error
To use the c classes and output, the file iostream.h must be incruded: // error
#include
#include
int main ()
{
Cout << 揋 o cubs!? << endl;
Return EXIT_SUCCESS;
}
The Method Get, When Used In the form, cin.get (buff, max_line);
Does Not Store The Terminating New Line and It Does Not Remove It From The Standard Input. for this Reason, The Following IS An Infinite Loop: Char Buff [80];
// Error Infinite Loop
While (cin.get (buff, 80))
Cout << BUFF << Endl;
After a manipulator is placed into the stream, all subsequent input or output is formatted accordingly except for the field width, which reverts to zero after a string or number is printed. Do not assume that at the end of a statement, all input / output Reverts to Default Settings. for Example, In The Following Code, The Input IS Written in Decimal, THE IN Hexadecimal TWICE: INT N = 100; cout << n << endl;
Cout << HEX << n << endl;
COUT << n << endl; // still hex
MIXING THE C AND C Input / Output Facilities May Produce Unexpected Results Unless The function ios :: sync_with_stdio
Is Invoked. as Examples, The Code #include
#include
#include
int main ()
{
INT A = 2, b = 5;
// risky
PRINTF (? D? a);
Cout << b << endl;
Return EXIT_SUCCESS;
}
IS risky, but
#include
#include
#include
int main ()
{
INT A = 2, b = 5;
// ok
ios :: sync_with_stdio ();
PRINTF (? D? a);
Cout << b << endl;
Return EXIT_SUCCESS;
}
Continue tomorrow ...