Variables
Declaration & Initialization
# declare
type variable_name;
# assign a value
variable_name = value;
# declare + initialize
type variable_name = value;
- Declaring a variable means creating a variable by specifying its name and type, without assigning a value to it,
int number;. - Initializing a variable means assigning a value to a varible for the first time,
number = 3;
If a variable is declared without initialization, the compiler automatically assigns a default value to it. Default values for each corresponding type are listed below
| type | default value |
|---|---|
| integer | 0 |
| floating-point | 0.0 |
| pointer | null |
| character | '\0' |
| string | "", '\0' |
| boolean | false |
| regular expression | \\ |
| enum | none* |
| struct | none* |
Basic types
In Cambo, there are many different kinds of data types, however here are some basic ones:
"".Example
1. Declare & initialize:
int main(){
int number = 3;
float pi = 3.14;
bool running = false;
char symbol = 'A';
string text = "hello, world!";
return 0;
}
2. Declare without initialization:
int main(){
int number;
print(number); # output: 0
bool running;
print(running); # output: false
return 0;
}
3. Declare then initilialize later:
int main(){
int number;
number = 3;
return 0;
}
To be precise, when a variable is declared without an explicit initilizer, the compiler automatically assigns a default value behind the scenes. As a result, initializing a value later is always an explicit reassignment, not an initial initialization. This means that initialization only occurs at the point of declaration.
As shown in third example above, the variable number is already initialized to 0 at the declaration line int number; by the compiler.
The line number = 3; is simply a reassignment of the variable number to the value of 3.
However, the compiler may optimize this by initializing number directly to 3, removing the default initialization to 0.