Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 40 ยท Language Basics

Basic Types: int, float, bool, char, string

A tour of OCaml's built-in types: integers, floats, booleans, characters, strings and unit, with literal syntax, ranges, escapes, string functions and common pitfalls.

Every OCaml value has exactly one type, and the type decides which operations are allowed. This lesson covers the built-in basic types you will use every day: int, float, bool, char, string and unit. All of them show up in the toplevel answer, so you can always check.

Integers

OCAML
let a = 42
let b = -7
let big = 1_000_000          (* underscores are allowed for readability *)
let hex = 0xFF               (* 255 *)
let bin = 0b1010             (* 10 *)

int is a 63-bit signed integer on 64-bit systems (one bit is used internally by the runtime). The range is therefore about plus or minus 4.6 quintillion: max_int is 4611686018427387903. Integer arithmetic wraps around silently on overflow, so max_int + 1 equals min_int. For arbitrary-size numbers, use the Zarith library, covered later.

Integer division truncates toward zero and mod gives the remainder:

OCAML
# 7 / 2;;
- : int = 3
# (-7) / 2;;
- : int = -3
# 7 mod 3;;
- : int = 1

Floats

OCAML
let pi = 3.14159
let small = 1e-3
let two = 2.          (* the dot makes it a float *)

float is a 64-bit IEEE 754 double. A literal without a dot, such as 2, is an int, never a float. There are special values infinity, neg_infinity and nan.

OCAML
# 1.0 /. 0.0;;
- : float = infinity

Booleans

bool has two values, true and false. Operators: && (and), || (or), not. Both && and || are short-circuit: the right side is evaluated only if needed.

OCAML
let safe_div a b = b <> 0 && a / b > 1

If b is 0, the division never runs.

Characters

char is a single byte (a Latin-1 character), written in single quotes:

OCAML
let letter = 'a'
let newline = '\n'
let tab = '\t'
let quote = '\''
let code = '\065'       (* decimal escape: 'A' *)

Convert with Char.code : char -> int and Char.chr : int -> char:

OCAML
# Char.code 'A';;
- : int = 65
# Char.chr 98;;
- : char = 'b'

Because char is only a byte, a character like รฉ or an emoji is not a single char. Strings are sequences of bytes, and OCaml treats UTF-8 text as bytes unless you use a Unicode library or the String.get_utf_8_uchar functions.

Strings

OCAML
let s = "Hello"
let multi = "line one\nline two"
let quoted = "She said \"hi\""

Strings are immutable sequences of bytes. The essentials:

OCAML
# "Hello" ^ ", " ^ "world";;
- : string = "Hello, world"
# String.length "OCaml";;
- : int = 5
# "OCaml".[0];;
- : char = 'O'
# String.sub "functional" 0 4;;
- : string = "func"
# String.uppercase_ascii "abc";;
- : string = "ABC"
# String.concat ", " ["a"; "b"; "c"];;
- : string = "a, b, c"
# String.split_on_char ',' "x,y,z";;
- : string list = ["x"; "y"; "z"]

Out-of-range access raises Invalid_argument "index out of bounds". Strings compare with = and < by their bytes, so comparison is case-sensitive.

Quoted strings

For text with many quotes or backslashes, OCaml has quoted strings, where nothing is escaped:

OCAML
let json = {|{"name": "Ada", "age": 36}|}
let regex = {re|\d+|re}

Converting to and from strings

OCAML
# string_of_int 42;;
- : string = "42"
# int_of_string "42";;
- : int = 42
# float_of_string "3.5";;
- : float = 3.5
# int_of_string_opt "oops";;
- : int option = None

int_of_string "oops" raises Failure "int_of_string". The _opt variant returns an option instead, which is usually safer.

Unit

unit has exactly one value, written (). It is what functions return when they have nothing useful to return, such as print_endline:

OCAML
# print_endline "hi";;
hi
- : unit = ()

You will read more in Tuples and unit.

Type summary

Type Examples Operations
int 0, -3, 1_000 + - * / mod
float 3.14, 1e5 +. -. *. /. **
bool true, false && || not
char 'a', '\n' Char.code, Char.chr
string "hi" ^, String.*
unit () none

A stand-in: type tags

This stand-in classifies JavaScript values into OCaml's type names, to practice spotting which type each literal has. The rule to remember: a literal with a dot is a float, without a dot is an int.

JS Stand-in: which OCaml type is it?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the type of the literal 2.?

    A numeric literal with a dot is a float, even with no digits after the dot.
  2. 2. What does (-7) / 2 evaluate to?

    Integer division truncates toward zero, so the result is -3.
  3. 3. Which expression gives the first character of the string s?

    String indexing uses s.[i] and returns a char.
  4. 4. Which function converts a string to an int without raising an exception on bad input?

    int_of_string_opt returns None for invalid input instead of raising Failure.
  5. 5. Is 'รฉ' a valid single char literal in source code?

    A char is one byte. UTF-8 characters beyond ASCII need several bytes and are handled as strings or Uchar values.

Summary

  • int is 63-bit and wraps; float is a double; literals with a dot are floats.
  • bool uses &&, || and not with short-circuiting.
  • char is a byte; string is an immutable byte sequence with a rich String module.
  • Use _opt conversion functions to avoid exceptions.
  • unit has one value, ().

Next: Operators and conversions