Introduction to Algorithms and Programming
Data types and variables
What do we mean when we say that the an item has the color value
could be represented
- Pantone 116
How do we represent the value
could be represented
Data values are concepts or ideas.
A value may have multiple data representations, but usually there is one that is conventionally used.
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
of binary encoded bits
can be used to represent integer
(whole number) values.
Binary encoding of characters
(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:
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.
- char = 1 byte
- short = 2 bytes
- int = 4 bytes
- long = 8 bytes
- 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
- The amount of memory to allocate, and
- 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
A data variable
- an identifier
- a data type
- a data value
are user friendly identifiers of specific locations in memory
Data variables are not
reserved keywords in C.
are user friendly names of variables
. 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.
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: