Files
carbon-lang/docs/design/expressions/literals.md
T
dd2f7d732c Design updates for #2015 numeric type literal syntax (#2410)
Edit numeric literal design for the literal type proposal and add reference where #2015 was mentioned.

Closes #2159 

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2022-12-09 11:01:21 -08:00

2.1 KiB

Literal expressions

Table of contents

Overview

This document is intended to cover all literal expressions, excluding numeric, floats and strings, which are covered in the Lexical Conventions section. For now, the document explains the numeric type literals.

Numeric type literals

Carbon has a simple keyword-like syntax of iN, uN, and fN for two's complement integers, unsigned integers, and IEEE-754 floating-point numbers, respectively. Here, N can be a positive multiple of 8, including the common power-of-two sizes (for example, N = 8, 16, 32).

Examples of this syntax include:

  • i16 - A 16-bit two's complement signed integer type
  • u32 - A 32-bit unsigned integer type
  • f64 - A 64-bit IEEE-754 binary floating-point number type

Usage

package sample api;

fn Sum(x: i32, y: i32) -> i32 {
  return x + y;
}

fn Main() -> i32 {
  return Sum(4, 2);
}

In the above example, Sum has parameters x and y, each of which is typed as a 32-bit two's complement signed integer. Main then returns the output of Sum as a 32-bit two's complement signed integer.

Alternatives considered

References