How to define variables in carbon language?

how to define variables in carbon language

To define variables in carbon language, use this syntax –

var identifier: < expression | auto > [ = value ];

Code Example

var x: i32 = 42;

Let’s understand this example –

  • var – To indicate that this is a variable.
  • x – Name of the variable
  • i32 – 32 bit integer
  • : – To separate variable name and it’s type
  • 42 – Value of the variable

We can also write this code using auto

var x: auto = 42;

auto results in a constant IntLiteral(42) value.

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like
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