C ++ Variables, Identifiers, Assignments, Input/Output
Variable
variable can hold a number or a data of other types, it always holds something. A variable has a name
the data held in variable is called value
variables are implemented as memory locations and assigned certain memory address. The exact address depends on computer and compiler.
we think as though the memory locations are actually labeled with variable names
Identifiers
identifier – name of a variable or any other named construct
identifier must start with a letter or underscore symbol (_), the rest of the characters should be letters, digits or underscores
the following are legal identifiers:
x x1 x_1 _abc sum RateAveragE
the following are not legal identifiers. Why?
13 3X %change data-1 my.identifier a(3)
C++ is case sensitive:
MyVar and myvar are different identifiers
Identifiers style
careful selection of identifiers makes your program clearer
identifiers should be
short enough to be reasonable to type (single word is norm)
standard abbreviations are acceptable
long enough to be understandable
two styles of identifiers
C-style – terse, use abbreviations and underscores to separate the words, never use capital letters for variables
Camel Case – if multiple words: capitalize, do not use underscores
variant: first letter lowercased
pick style and use consistently
ex: Camel Case 1 C-style Camel Case 2
Min min min
Temperature temperature temperature
CameraAngle camera_angle cameraAngle
CurrentNumberPoints cur_point_nmbr currentNumberPoints
Keywords
keywords are identifiers reserved as part of the language
int, return, float, double
cannot be used by the programmer to name things
consist of lowercase letters only
have special meaning to the compiler
asm do if return typedef
auto double inline short typeid
bool dynamic_cast int signed typename
break delete long sizeof union
case else mutable static unsigned
catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned
Variable declaration
every variable in C++ program needs to be declared
type – the kind of data stored in a variable
declaration – introduces a variable to a compiler.
variable has to be declared before use
a variable declaration specifies
type
name
declaration syntax:
two commonly used numeric types are:
int – whole positive or negative numbers:
1,2, -1,0,-288, etc.
double – positive or negative numbers with fractional part:
1.75, -0.55
example declarations:
int numberOfBars;
double weight, totalWeight;