To define variables in carbon language, use this syntax –
var identifier: < expression | auto > [ = value ];
Code Example
var x: i32 = 42;
Let’s understand this example –
-
var– To indicate that this is a variable. -
x– Name of the variable -
i32– 32 bit integer -
:– To separate variable name and it’s type -
42– Value of the variable
We can also write this code using auto –
var x: auto = 42;
auto results in a constant IntLiteral(42) value.
Carbon Language Series
- Introduction to Carbon Language.
- How to define variables in Carbon.
- Primitive Types – Bool, Int, Float, String
- Tuple in Carbon
- Struct in Carbon
- Pointers in Carbon
- Operators in Carbon Language
- Conditions & Control Flow in Carbon
- Ternary Operator (if expression) in Carbon
- Switch conditional in Carbon using Match
- Loops in Carbon
- Functions in Carbon