Variables and Data types: Part1

Variables and data types
  • Computer memory

    • To be able to complete a task, program needs to have the ability to store its data
    • Program’s data is stored in computer’s memory
    • Computer Memory stores data in a sequence of bits (each bit can either store 0 or 1)
    • Even though the smallest unit is a bit, smallest usable unit in memory is a byte
    • Each byte (8 bits) in the memory has an unique location (its counting index)
  • Variable

    • Provides meaningful name for a location in memory
    • Is a location in the memory where value is stored and can be retrieved later
    • Variable must be declared before it can be used
  • Constants/Literals

    • Constants refer to fixed values. These fixed values are also called literals.
    • Examples, Integer constants: 1, 2, 3,…Floating constants: 1.1, 2.3, 4.0,...Character constants: ‘a’, ‘b’, ‘c’,…String literals: “Hello, there”, “Hi, there”, ...
    • Variables are used to hold constant values, but at different time it can hold different constant values
  • Data types

    • Variable must have a data type, which specifies
      • Which kind of data it can contain
      • Size of data to be stored in the memory (bytes)
      • How to convert from them from/to binary format
    • Data types can be divided into
      • Primitive data types e.g., int, float double, etc.
      • Reference data types e.g., array, pointer, etc.
  • Variable declaration

    • Before using a variable, we need to declare it
    • Declaring a variable: The name of the variable is preceded with the data type.
    • The name should follow naming conventions
      • No white spaces, special characters (*, !, ?, etc.)
      • Could include underscore (‘_’)
      • Could include digits (but not at the start of the name)
      • Etc.
    • Syntax
      datatype variableName;
    • Example
      int varName;
  • Basic data types

  • Type int

    • Store integer (numeric data), cannot store other data types like string "abc", or "Abc".
    • Declaration: int num;
    • Its size is depending on the OS in which our code is running, however, it's normally 4 bytes
    • Integers in the range: -32768 to 32767, Examples: 12322, 0, -232
  • Type float

    • Stores values containing decimal places. Cannot store other type e.g., “Alan” or “abc”. Precision of up to 6 digits.
    • Declaration: float num;
    • Its size is depending on the OS in which our code is running, however, it's normally 4 bytes
    • Examples: 12.1, 14.5, 15
  • Type double

    • Stores values containing decimal places. Cannot store other type e.g., “Alan” or “abc”. Precision of up to 10 digits.
    • Declaration: double num;
    • Its size is depending on the OS in which our code is running, however, it's normally 8 bytes
    • Examples: 15.0, 2.456, 1245
  • Type char

    • Stores single character information. Cannot store other type e.g., “Alan” or “abc”.
    • Declaration: char c;
    • Its size is depending on the OS in which our code is running, however, it's normally 1 byte
    • Examples: ‘a’, ‘b’, ‘$’, ‘%’, ‘1’, ‘2’
  • Type void

    • Stores nothing. This is used to indicate the compiler that there is nothing to expect.
  • Derived data types

  • Variable initialization

    • You could initialize values to variables at the time of declaring them.
    • Usage: dataType varName = value;
  • Type casting

    • An expression can be forced to be of a certain type by using a cast.
    • The general syntax of cast: dataType varName = (dataType)expression;
    • Examples: float x, y;
      y=45.6;
      int i = (int)y;
      x = (int)y;//Value of x will be 45