LT2 Variables (and Constants)
C++ Syntax
Compiler detects any violation of syntax
Compiler collects the characters of code into Tokens, which are separated by space
Tokens
-
Keywords: return, namespace, include, int
- Keywords are reserved
- Types:
int, bool, string, char, long, short, signed, unsigned, void
- Flow Control:
if, else, while, do, switch, case, break, continue, default
- Others:
using, namespace, true, false, new, public
-
Identifiers: user-defined variables, objects, function names
- The unique name of objects, variables, functions for identification
- Keywords cannot be identifiers
- letters, digits (no leftmost), underscores
_
, No Hyphen-
- Hyphen in C++ means Minus (-) Operator
String & Number constants: aka Literals in python
Operators:
+ - * / << >>
, etcPunctuators:
; , { } ( )
Variables and Constants
- Constant: A variable that can not be changed
Type, Name, Scope of Variables
- Type: either predefined or user-defined
- Name: the identifier
- Scope: the extent to which the variable can be accessed
C++ Predefined Data Types
- Numerical:
int, double, signed, unsigned, long, short
- Character:
char
- Logical:
bool
- Other:
void
-
int
- 4 Bytes = 32 bits
- -$2^{31} \sim 2^{31}-1$
- refer to 4-bit can represent -8 ~ 7
-
Overflow and Underflow happens when assigned value is higher / lower than the representable limits
- C++ does not inform the Overflows and Underflows
- int = 4 bytes / 32 bits, short = 2 bytes / 16 bits, long = 8 bytes / 64 bits
- unsigned: 0 ~ 2^32 - 1
-
char
: 1 byte, 8 bits- Store a single Character enclosed in Single Quotation Mark
'
- ASCII codes takes 1 byte = 8 bits in memory
- A char can represent a integer up to 255 (0 - 2^8 - 1)
- Store a single Character enclosed in Single Quotation Mark
-
cstring: an array of
char
- Delimited by Double Quotation Marks
// Definition must be followed by [], or begin with * char a[] char * a
-
Floating types:
float
anddouble
- Float: 4 bytes 32 bits, same as int
- Double: 8 bytes 64 bits, same as long
-
sizeof
operator: return the number of bytes
Type Conversion
- Implicit Type Conversion
- Lower ranked are promoted to be higher ranked type: double > int > char
- Assignment statement will assign the left to match the left type
- Explicit Type Conversion
int a = 1.9; // a = 1;
Constants
Also defined type, name, scope
However constants can not be changed once defined
const type_name constant_name = value
Variable Scope - Local and Global
Scope defines the accessibility boundary of a variable
- Local variables are defined in a block
{}
- and can only be accessed within this block
- Global variables are defined in the Global Definition Section of a program
- ie: Defined outside the function block
- Can be accessed by all functions
Conflict of Local and Global Variables with Same Names
The Local Variable hides the Global Variable within its scope
also applies to local local variables wrt local variables
Scope and namespace
A scope can be defined in many ways, eg by
- {}
- functions
- classe
- namespaces
Namespace is used to Explicitly define the scope.
Scope Operator ::
is used to identify same-name variables within different scopes