Carbon Language – Bool, Int, Float, String Primitive Types

carbon language primitive datatypes int float bool string
Table of Contents Hide
  1. Bool
  2. Int
  3. Float
  4. String
  5. Conclusion

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 –

Whereas, the unsigned integer can be defined using Carbon.UInt(N). Convenience notations are –

Integer literals could be represented as decimal, hexadecimal and binary –

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 –

In floats both decimal and hexadecimal are supported. Some examples are –

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 –

  1. Single Line – Start and end the string with ". For example – “Hello World”.
  2. 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.