There are 4 primitive data types in Carbon language – Bool
, Int
, Float
and String
.
Bool
Similar to other languages, bool
represents Boolean. It’s values are True and False. It is used in conditions and Loops.
Code Example –
var i: bool = True;
Int
The Signed integer datatype could be define using Carbon.Int(N)
, where N
is the bit width. For convenience Carbon provides –
-
i8
– 8 bit integer -
i16
– 16 bit integer -
i32
– 32 bit integer -
i64
– 64 bit integer -
i128
– 128 bit integer -
i256
– 256 bit integer
Whereas, the unsigned integer can be defined using Carbon.UInt(N)
. Convenience notations are –
-
u8
– 8 bit unsigned integer -
u16
– 16 bit unsigned integer -
u32
– 32 bit unsigned integer -
u64
– 64 bit unsigned integer -
u128
– 128 bit unsigned integer -
u256
– 256 bit unsigned integer
Integer literals could be represented as decimal, hexadecimal and binary –
-
37834
– Decimal -
0x4A59DB
– Hexadecimal (0x) -
0b100010
– Binary (0b)
0x
and 0b
must be in lowercase letters. Hexadecimal alphabets must be in uppercase letters.
Code Example –
var a: Carbon.Int(32) = 55; var b: i8 = 120; var c: i16 = -4000; var d: i32 = 1000000; var e: i64 = -1000000000; var f: i128 = 1000000000000000000; var g: i256 = 100000000000000000000000000; var ua: Carbon.UInt(32) = 55; var ub: i8 = 120; var uc: i16 = 4000; var ud: i32 = 1000000; var ue: i64 = 1000000000; var uf: i128 = 1000000000000000000; var ug: i256 = 100000000000000000000000000;
Float
Floating point data type is represented as –
-
f16
– 16 bit float -
f32
– 32 bit float -
f64
– 64 bit float -
f128
– 128 bit float
In floats both decimal and hexadecimal are supported. Some examples are –
- 123.456
- 123.456e+12
- 123.456e-12
- 0x23.Ap+34
- 0x23.Cp-34
Code Example –
var a: f16 = 123.456 var b: f32 = 123.456e+12 var c: f64 = 123.456e-55 var d: f128 = 0x23.Ap+72
String
String starts and ends with "
. There are two ways to declare strings in Carbon –
- Single Line – Start and end the string with
"
. For example – “Hello World”. - Multi Line – For multi line or block strings, we need to start and end it with
"""
.
There is one more string type – StringView
. It is used to create a read only reference of provided string.
Code Example –
var str1: String = "Hello World!"; var str2: String = """ This is some string which spans over multiple lines. To deal with it in carbon, We use triple quotes. """; var readOnlyString: StringView = "I am read only";
Conclusion
In this article, we saw 4 different primitive datatypes in Carbon language – Bool, Int, Float and String. In next article we will learn about Tuples, Structs, Arrays and Pointers.
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