Basic datatypes
Variables are declared to be of a certain datatype,
or just type.
We use different types to represent the permissible values that a
program's variable has.
For example,
if we're using a variable to just count things,
we'll use an integer variable to hold the count;
if performing trigonometry on angles expressed in radians,
we'll use floating-point variables to hold values with both an
integral and a fractional part.
C provides a number of standard,
or base types to hold commonly required values,
and later we'll see how we can also define our own
user-defined types to meet our needs.
Let's look quickly at some of C's base datatypes:
typename |
description, and an example of variable initialization |
bool |
Boolean (truth values), which may only hold the values
of either true or false
e.g. bool finished = false;
|
char |
character values, to each hold a single values such as an
alphabetic character, a digit character, a space, a tab...
e.g. char initial = 'C';
|
int |
integer values, negative, positive, and zero
e.g. int year = 2006;
|
float |
floating point values,
with a typical precision of 10 decimal digits (on our lab machines)
e.g. float inflation = 5.1;
|
double |
"bigger" floating point values,
with a typical precision of 17 decimal digits (on our lab machines)
e.g. double pi = 3.1415926535897932;
|
Some textbooks will (too quickly) focus on the actual storage size of these
basic types, and emphasise the ranges of permissible values.
When writing truly portable programs -
that can execute consistently across different hardware architectures
and operating systems -
it's important to be aware of, and avoid, their differences.
We'll examine this issue later,
but for now we'll focus on using these basic types in their most obvious ways.
From where does the bool datatype get its name? -
the 19th century mathematician and philosopher,
George Boole.
CITS2002 Systems Programming, Lecture 2, p5, 24th July 2024.
|