Variables in Carbon are like containers that store data. They have a name and a data type. The data type determines the kind of values a variable can hold.
Data Types in Carbon are essential for defining the nature of data. They specify how much memory a variable will occupy and the operations that can be performed on it. Carbon offers a variety of data types, including:
- Numeric Data Types:
int: Represents integers (whole numbers without decimal points).float: Represents floating-point numbers (numbers with decimal points).double: Represents double-precision floating-point numbers (provides greater precision thanfloat).
- Character Data Types:
char: Represents a single character.string: Represents a sequence of characters (text).
- Boolean Data Type:
bool: Represents a logical value (eithertrueorfalse).
Output in Carbon is the process of displaying information on the console. The print function is used to print values of variables or expressions. Here’s an example:
var message: string = "Hello, world!";
print(message);
This code will print the message “Hello, world!” to the console.
Formatting Output
Carbon provides various ways to format output:
String Interpolation:
Use $ to embed expressions within strings. For example:
var name: string = "Alice";
print("Hello, $name!");
String Formatting:
Use the format method on strings to create formatted output. For example:
var number: int = 123;
print("The number is: {:d}".format(number));
Data Types and Memory
The amount of memory allocated to a variable depends on its data type. For example, an int typically requires 4 bytes of memory, while a double requires 8 bytes. Understanding data types and memory usage is crucial for efficient memory management in Carbon programs.

