Basics | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comments// This comment terminates at end of line.
/* Comment (possibly many lines)
terminates at */
Reserved Words
asm auto bool break
case catch
char class const const_cast continue
default delete do double dynamic_cast
else enum explicit export extern
false float for friend goto
if inline int long mutable
namespace new
operator private protected public
register reinterpret_cast return
short signed sizeof static static_cast
struct switch template this throw
true try typedef typeid typename union
unsigned using virtual void volatile
wchar_t while
Operators and Puctuation() {} [] + - * / % ++ -- < <= == != >= > && || ! << >> ?: ; , . = += -= ...& | ^ ~ :: -> ->* .* |
Identifiers
| |||||||||||||||||||||||||||||||||||||||||||||||||||
Simple and String Types | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Integer TypesThe integer types: char, short, int, long, and bool are binary integers. All except bool are signed (char is usually signed), but can be declared unsigned. Below are some typical characteristics of the basic types. The standard allows compilers to have different implementations.
Floating-point TypesThe floating-point types, float and double, are usually stored in IEE-754 format. Calculations may produce NaN (Not a Number); or positive or negative infinity.
|
CharactersCharacters are stored in one byte (char ) or two bytes (wchar_t ).
char is an integer type.
Literals
C-stringsC-strings are arrays of chars with a terminating zero ('\0'). Enclose C-string literals in double quotes. C-strings are typechar[] or equivalently char * .
char* message = "Time to go home"; char message[] = "Time to go home";Assignment and concatenation operators are overloaded to allow use with string variables.
string typeThis object type requires the<string> header.
string s; s = s + " more"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
Declarations | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Simple variable declarationsWrite the type, a variable name, an optional initial value, and a comment. It's possible to write a comma-separated list of variable names after the type or to omit the comment, but it's not a good style.int t; // temp in Celsius char separator = ','; // name separator unsigned short h; // height in pixels |
Constant declarationsUse the keyword const int MAX_SIZE = 20; // Max number of keys. Enumeration TypesA sequence of integer constants can be defined with enum.enum Direction (NORTH, EAST, SOUTH, WEST); // NORTH is 0, EAST is 1, ... Direction myWay; |