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

Total
0
Shares
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 –

  • 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 –

  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.

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like
introduction to carbon language - what and why it is created

Introduction to Carbon Language

Table of Contents Hide IntroductionFeatures of Carbon LanguageWhy developed Carbon? Why not upgraded C++?Where Carbon could be used?Carbon is written in which language?Code Example Carbon Language is the successor to…
View Post
functions in carbon language

Functions in Carbon Language

Table of Contents Hide IntroductionCode Example –Conclusion Functions in carbon language has the same role as in C, Php or any other functional language. It is used to separate a…
View Post
operators in carbon language

Operators in Carbon Language

Table of Contents Hide ArithmeticBitwise & BitshiftComparisonConversionLogicalIndexingFunction CallPointerMoveConclusion We have a number of operators in Carbon language like – Arithmetic, Bitwise, BitShift, Comparison, Conversion, Logical, Indexing, function call, pointer, move…
View Post