C Data Types
Fundamental Data Types
int
- Integer:- The
int
data type is designed for storing whole numbers, or integers. - Internally, integers occupy 4 bytes (equivalent to 32 bits) of memory space.
- This limitation means that
int
variables can represent values within the range of 32 bits.
- The
unsigned int
- Unsigned Integer:- Adding the "unsigned" qualifier to certain types, including
int
, extends the positive range of variables. - While this increases the positive value range, it restricts the use of negative values.
- Adding the "unsigned" qualifier to certain types, including
char
- Character:char
is used for storing single characters, such as letters, digits, or symbols.- Each character takes up 1 byte (8 bits) in memory, allowing for 256 possible character representations.
float
- Floating-Point:- The
float
data type handles real numbers with decimal points, known as floating-point values. - Floating-point values occupy 4 bytes (32 bits) in memory and offer precision for scientific and mathematical calculations.
- The
double
- Double Precision:double
is also used for storing floating-point values, but it provides double the precision compared tofloat
.- These variables consume 8 bytes (64 bits) of memory, making them ideal for complex calculations that demand high accuracy.
void
- Placeholder for "Nothing":- Although not a data type in the traditional sense,
void
serves as a placeholder. - Functions can have a
void
return type, indicating that they do not return any value. - Similarly, a function's parameter list can be void, indicating it takes no parameters.
- Although not a data type in the traditional sense,
Additional Types (CS50 Library)
bool
- Boolean:- The
bool
data type is specifically for storing Boolean values. - Booleans can hold only one of two values:
true
orfalse
, making them ideal for representing binary choices in logic.
- The
string
- String:- The
string
data type is designed for handling sequences of characters. - Programmers often use strings to work with text and manipulate collections of characters in their code.
- The
Understanding data types is fundamental for any programmer, as it dictates how data is handled and processed in a computer program. Armed with this knowledge, you can efficiently manage variables, perform calculations, and build robust software solutions tailored to your needs.