This tutorial covers a Datatype supported by OCaml Language.

There are two types of datatypes.

  • Built-in Datatypes: These are scalar data types that hold a single value.
  • Custom Datatypes: It is a new data type defined to store multiple primitive types. Example is tuple

OCaml provides Built In Datatypes.

  • Integers
  • Floating Numbers
  • Character
  • Strings
  • booleans
  • uint

OCaml Integer types

Ocaml supports numerical fixed numbers that can have 30 bits (60 bits) and it supports 32 and 64-bit integers.

Datatype is int. The range of values for integers is - 2 power 30 to 2 power 30 -1.

Integer values can be 11, 25, 121. and Underscore characters in numbers are also accepted.

The default value is a Base of 10.

Octal numbers are supported by prefixing with 00, Example values are 0o123 Binary numbers are supported by prefixing with 0b, Example values are 0b10 Hexa Numbers are declared by prefixing with 0x.

Here is an example

let number1 : int = 11
let number2 : int = 1_200
let number3 : int =0o123

OCaml Float types

The data type is float in OCaml. It represents IEEE 754 double-precision numbers.

It contains two parts separated by a dot(.). The first part contains several 53 bits and the second part is -1022 to 1023.

Example numbers 1.5, 2.0, and 11.23.

Here is an example

let number1 : float = 11.11
let number2 : float = 34.667
let number3 : float = -89.90

OCaml Char types

Characters declared using char type with literal, enclosed in a single quote. Each character represents 1 byte. i.e. 8 bits.

It contains characters of ISO-8859-1. Example characters are ’e’, and’\n’. Each character represents an ASCII code value.

You can convert an integer to a character using int_of_char, convert a character to an integer using char_of_int.

let char1 : char = 'e'
let char2 : char = '\n'
let char3 : char = 't'

OCaml String types

the variable string is a group of characters enclosed in double-quotes. The data type is string.

String literals can also contain special characters.

Example string declaration

 let name = "one";
let dostring="one\ntwo\nthree";

String in OCaml are mutable and can be changed using inbuilt functions.

OCaml Boolean types

Bool provides conditional expression values true and false.

let isCorrect : bool = true
let isNull : bool = false

OCaml unit type

unit is an inbuilt type that represents the unit value type.

It is used to declare an expression with no value returned.


let expr : unit = ()
let isNull : bool = false