The preprocessor is a specialized text editor that is not (usually) part of the compiler. It uses a different syntax than C++.
Examples:
Each library has a standard header file. The   #include   gives a program access to a library and causes the preprocessor to insert definitions from its standard header file into a program before compilation. The directive #include<iostream> gives a program access to the I/O library, its constants, and input/output functions and objects.
The directive #define YEAR 1776 is a constant macro; YEAR is replaced everywhere it appears in the code by the value 1776 before the program is sent to the compiler. By convention, constant macros are all capital letters.
/***********************************************
Read in 3 integers, display average
input: 3 integers
output: average of input integers
***********************************************/
#include <iostream>
#define NUMBER 3.0
using namespace std;
int main()
{
int x, y, z; // integers to read and add
double avg; // average of three integers
cout << "Please enter three integers: ";
cin >> x >> y >> z;
avg = (x + y + z) / NUMBER;
cout << "The average is " << avg << ".\n";
return 0; // successful termination; exit function
}
|
Input: -23 107 50   Output: The average is 44.6667. |
// This is one kind of comment, usually used for one-line comments.
/* This kind is usually used for multi-line comments */
/**** So is this. ****/
/* We cannot /* embed */ comments */
In the program above, the first 7 lines are header block comments.
The header block contains:
By convention, variable names are mostly lower case; constants are upper case. Variables and constants are typed (int, float, double, char, etc.). Each holds a single value of its type. Named entities (variables, constants, function names, classes, objects, etc.) are identifiers, and the names must follow certain rules. Use mnemonic names!
| Valid:   | _OK |
num6 |
one_time |
oneTime |
a724xyz_97 |
| Invalid:   | *bucks |
4Aces |
A-Z |
||
| Note:   Linux and C++ are case sensitive! | |||||
The four standard types in C++ are
int, float, double, and char.
C++ also has other types including bool, long,
short, signed, unsigned, and more.
double: double precision, also used for floating point values (8 bytes)
Both float and double values require a decimal point. The system will convert integers to floating point values when necessary.
| Examples:  | 3.14 |     -0.452 |     -.452 |     +761.3214 |
| Examples:  |   6 |   +97 |   0 |   -14 |   145792 |
double   (8 bytes)
- precision of 15 decimal digits
- range of positive values ~ 10 -308 and 10 308
with a similar range of negative values
integer   (4 bytes)
- can be represented exactly
- range of values is up to +- 2,147,483,647
| Examples:  |   'R' |     'r' |     '$' |     '6' |     '' |     ' ' |     '?' |
int age; // value of age is undefined
char letter;
int Sum, sum, SUM; // valid but BAD FORM
int num = 100; // value of num is 100, assigned at compile time
char flag = '4'; // character '4' does NOT equal integer 4!!
double rate = 0.25;
double cost, net_pay, average;
bool sorted; // can be false (0) or true (1)
Constant Declaration Examples:
const   int   CARDS   =   52; // cards in the deck
const   float   PI   =   3.14159; // math constant to five decimal places
const   char   CODE   =   'd'; // abbreviation for Thursday
const   int   FALSE   =   0; // expressions evaluated to zero are false
What is the meaning of each?
#define DEMO1 'Y' // Usage: ch_var = DEMO1;
const char DEMO2 = 'Y'; // Usage: ch_var = DEMO2;
char demo3 = 'Y';   // Usage: demo3 = ch_var; -OR- char_var = demo3;
What happens if you omit the quotes?
Notice in #define there are no types, get-symbols, or semi-colons. This is because of the way the preprocessor replaces the constant macro name with the constant value before the program is compiled.
It is legal to use arithmetic expressions and other defined constants in #define   directives.
#define   PI   3.14159
#define   RADIUS   100
#define   AREA   PI   *   RADIUS   *   RADIUS
What is the semantics (meaning)?
The expression on the right is evaluated; if the result (the rvalue)
is type-compatible with the variable on the left (the lvalue),
the result is assigned to the variable on the left.
Examples:
// Assume appropriate declarations and initialization x = 7; letter = 'M'; y = y + 1; // add 1 to y y++; // add 1 to y ++y; // add 1 to y area = PI * radius * radius; value = (a * x + b) / (y * y); x = x; // WHAT?? |
Assign values at compile time (often a bad practice):
#define TAX 0.0925 int number_items = 0; double cost = 24.95 + TAX; char code = 'X'; |
Assign values at run time:
I/O is accomplished with streams of characters. Object cout
sends a stream of characters to standard output. For us, stream object
std::cout is connected to the monitor or screen; stream object
std::cin is connected to the keyboard.
Operators   <<   and   >>   are
overloaded operators,
which means that they may be used in different ways at different
times. >> is the stream extractor operator;   << is the stream
insertion operator.
#define TAX 0.0925
int number_items;
double cost;
char code;
number_items = 0;
cost = 24.95 + TAX;
code = 'X' + 1;
cout << "Enter a code and a cost: "; // prompt for input
cin >> code >> cost; // read values from standard input
cout << "The answer is " << cost << endl; // write to standard output
cout << "The answer is " << cost << "\n"; // same as above
\n  is one of several
escape sequences defined by C++ to represent special characters.
It is the line feed character and it moves the cursor to the
beginning of the next line. Two consecutive line feed characters flush
the output buffer. \t  the tab character;
\f  the form feed character;
\b  the backspace character.
cout Examples:
cout << "I have " << number << " baseball cards.\n"; cout << "Average: " << average << "  Grade: " << letter << endl; cout << "It costs $" << cost << ".\n"; cout << "Hello!"; cout << "Hello!"; cout << "\n"; // go to a new line cout << "\n\n"; // skip a line cout << "\\ \' \"\n"; // display characters \  '  " |
Assume these values:
number: 500
letter: B
average: 89.965
cost: 6.4
Output: |
______________________________________________
cin Examples:
cin >> x;
cin >> x >> y;
cin >> code >> cost >> debt;
Example:
double radius; // radius of pool
int length; // length of diving board
cin >> radius >> length;
cin >> radius >> length;
cout << radius << "   " << length << endl;
input: 30.7 9 40 6 3.1 output?
![]() |
What happens to the input value 3.1?
What (not how) does this code do?
__________________________
Example:
  int x, y, z;
  cin >> x >> y >> z;
cout << x << " " << y << " " << z << endl;
cout << z << " " << x << " " << y << endl;
input: 40 -8 6
output: 40 -8 6
      6 40 -8
Final values:   x: 40 y: -8 z: 6
What (not how) does this code do?
__________________________
Example:
cin >> y >> z >> x;
cout << x << " " << y << " " << z << endl;
input: 40 -8 6
output: 6 40 -8
Final values? x: 6 y: 40 z: -8
What (not how) does this code do?
__________________________
Example:
cin >> x >> y >> x;
cout << x << " " << y << " " << x << endl;
input: 40 -8 6 23
output: 6 -8 6
Final values? x: 6 y: -8
What (not how) does this code do?
__________________________
Example:
cin >> y >> x >> y;
cout << x << " " << y << " " << x << " " << x << endl;
Final values? x: -8 y: 6
What (not how) does this code do?