CS Basics: Data Types & Variables

How programming languages classify, store, and manipulate data using types like int, float, char, and string.

Originally written by Carl Mills · December 15, 2017

Why Data Types Matter

Every variable has a type that defines what kind of data it holds and how much memory it occupies. Data types ensure values are valid, memory is used efficiently, and operations behave as expected.

Common Data Types

Int (Integer)

Stores whole numbers. Allocates 4 bytes (32 bits) of memory. Range: roughly -2 billion to +2 billion. Exceeding this range causes an overflow.

Unsigned Int

Similar to int, but without negative values - allowing a range of about 0 to 4 billion.

Char (Character)

Uses 1 byte to store a single character: a letter, number, or symbol.

Float (Floating Point)

Holds numbers with decimals. Uses 4 bytes. Prone to precision errors due to limited memory.

Double

Uses 8 bytes, offering higher precision for decimal values - solving most float accuracy issues.

Void

Represents the absence of a value. Commonly used for functions that do not return anything.

Boolean

Stores only two possible values: true or false. Crucial for conditions and logic flow.

String

Holds sequences of characters - words, sentences, or paragraphs. This paragraph itself is represented as a string in memory.

Declaring a Variable

In many modern languages, types are automatically inferred. For example, in JavaScript:

var myVariable = "Hello World";

The variable myVariable is automatically treated as a string since the value is text.

Type Conversion

Data types aren't fixed. You can convert one type to another - for instance, turning a string into a number or a number into a boolean. This flexibility allows developers to manipulate and format data as needed.

Next in the Series

Tomorrow, we'll explore how these data types interact in expressions and how variables evolve within program logic.

Continue to Operators & Expressions →