Variables and Constants

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
  1. Keywords: return, namespace, include, int

    • Keywords are reserved
    1. Types: int, bool, string, char, long, short, signed, unsigned, void
    2. Flow Control: if, else, while, do, switch, case, break, continue, default
    3. Others: using, namespace, true, false, new, public
  2. 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
  3. String & Number constants: aka Literals in python

  4. Operators: + - * / << >>, etc

  5. Punctuators: ; , { } ( )

Variables and Constants

  • Constant: A variable that can not be changed
Type, Name, Scope of Variables
  1. Type: either predefined or user-defined
  2. Name: the identifier
  3. Scope: the extent to which the variable can be accessed

C++ Predefined Data Types

  1. Numerical: int, double, signed, unsigned, long, short
  2. Character: char
  3. Logical: bool
  4. 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)
  • 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 and double
    • 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

  1. 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
  2. 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

你可能感兴趣的:(Variables and Constants)