Variables

进来学单词

Definitions

  • Object: region of memory with a type that specifies the representation of the data stored there and what operations can be performed on it

  • Variable: object with a name

  • Value: data that may be stored in a variable

  • Declaration: statement that gives a name to an object

  • Definition: declaration that also sets aside memory for a variable

    • Assignment: statement that gives value to an object

Names / Identifiers

  • For variables, functions, types, ...

  • Rules:

    • Start with a letters or underscore

    • Only composed of letters, digits and underscores (_)

    • Cannnot use keywords (e.g. int, if, while, double)

Variables

  • Identifier is associated with a specific location in memory (i.e. address)

  • Use them in programs as if they were the value

  • In the background, the compiler sets things up to dereference the variable identifier (i.e. get the value held in the address)

  • The variable type dictates how the bits will be interpreted. (More on types and data representation later...)

Declaration, Definition, & Initialization

  • Declare

    • Say what an identifier is and what type of object it refers to

    • Connects a name to an object

  • Define

    • Sets aside memory for the variable

  • Initialize

    • Assign value to variable for the first time

int z; //declaration and definition

extern int z; //declaration
//this is rare for variables of base types(i.e. we don't do this)

int z = 7; //declaration, definition, and initialization

Last updated

Was this helpful?