<http://lib.cnfolio.com/ENG421DataTypesAndVariables>
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:
• Yellow
• #FFFF00
• Pantone 116
• Y
• 2

How do we represent the value of 65?

The value of 65 could be represented as:
• 65
• 65.0000
• 65.4
• 65.9
• A
• 0x41

### 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:
• zero = switch off = low voltage
• one = switch on = high voltage

A combination of multiple bits can be used to store and encode more complex and useful data.
• 8 bits = 1 byte
• 16 bits = 2 bytes = 1 word

### 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:
• 0–31 = control characters
• 32–63 = numbers and punctuation
• 64–95 = uppercase letters
• 96–127 = lowercase letters

Part of the table of ASCII codes: ### 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:
• First character must not be a digit
• Consists of a sequence of letters (A-Z, a-z), digits (0-9) and underscores (_)

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:
• Any value equal to zero is considered to be equivalent to boolean value false.
• Any value not equal to zero is considered to be equivalent to boolean value true.

### 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: