mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 15:10:12 +01:00
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>
2.1 KiB
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 typeu32- A 32-bit unsigned integer typef64- 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.