Data types
To understand data types, you need to understand that there are two ‘types’ of programming languages:
- statically typed languages
- dynamically typed languages
What are dynamically typed languages?
Section titled “What are dynamically typed languages?”Dynamically typed languages don’t require you to give a variable a data type when you create it. Instead, types don’t really exist and the language just figures out how best to store the variable based on the value you give it.
An example of a dynamically typed language is Python. We can give any variable data of any type:
x = 5 # x is an integerx = "hello" # now x is a stringWhat are statically typed languages?
Section titled “What are statically typed languages?”Statically typed languages (such as C) do require you to give a variable a data type when you create it. This means that the variable can only hold data of that type.
For example, in C we can create an integer variable like this:
int x = 5; // x is an integerIf we try to give x a string value later on, we’ll get an error:
x = "hello"; // Error: cannot convert char* to intCommon data types
Section titled “Common data types”These are some important data types that we’ll cover in future chapters!
| Data Type | Description | Example Values |
|---|---|---|
| int | Integer (whole) numbers | 1, -5, 42 |
| float | Floating-point (decimal) numbers | 3.14, -0.001, 2.0 |
| string | Text, really an array of chars | ”hello world”, “123” |
| bool | Boolean values (true or false) | true, false |
| char | Single character | ’a’, ‘Z’, ‘1’ |
| array | Collection of values of the same type | [1, 2, 3], [“a”, “b”] |
| object | An instance of a struct | struct Person {char* name; int age;} |