Introduction to Algorithms and Programming

Data types and variables

What do we mean when we say that the an item has the color value of yellow?

The value of yellow could be represented as:

How do we represent the value of 65?

The value of 65 could be represented as:

Data values

Data values are concepts or ideas.

A value may have multiple data representations, but usually there is one that is conventionally used.

Digital data

A bit is the smallest unit of digital data. There are two possible values:

A combination of multiple bits can be used to store and encode more complex and useful data.

Binary encoding of numeric data

The sum of binary encoded bits can be used to represent integer (whole number) values.

Binary encoding of characters

ASCII (American Standard Code for Information Interchange) is a standard for using binary encoding to represent character symbols.

ASCII uses 8 bits to encode 128 characters:

Part of the table of ASCII codes:

Source: ASCII. (2011). Retrieved from http://en.wikipedia.org/wiki/Ascii

Basic data types

There are 3 basic data type categories in the C language.
  1. void
  2. integer
    • char = 1 byte
      • unsigned char
    • short = 2 bytes
      • unsigned short
    • int = 4 bytes
      • unsigned int
    • long = 8 bytes
      • unsigned long
  3. floating point
    • float = 4 bytes = 6 digits of decimal precision
    • double = 8 bytes = 15 digits of decimal precision
    • long double = 16 bytes = 18 digits of decimal precision

Data type indicates:
  1. The amount of memory to allocate, and
  2. How to interpret the bits of data stored at the allocated location in memory

Data types increases portability of software between different hardware.

They are reserved keywords in C.

Data variables

A data variable has:
  1. an identifier
  2. a data type
  3. a data value

Data variables are user friendly identifiers of specific locations in memory.

Data variables are not reserved keywords in C.

Identifiers are user friendly names of variables, functions and constants. They are case sensitive and have no length restrictions. However, they must follow these rules:

Data variables must be declared before they can be used in a C program.

Optionally, data variables may be initialised

The behaviour of data types when exceeding the system defined limits is not standardized and entirely dependent on the hardware.

Boolean values

The C language does not have a data type for boolean values of true and false. Instead, it uses the following rules:

Reading an input value from stdin

There are detailed formatting rules to specify the data type when obtaining input:

Printing an output value to stdout

There are detailed formatting rules to specify the data type when displaying output: