Variables in Solidity

Learn about defining variables and operations in Solidity.

We can’t write a useful contract without defining variables. In this lesson, we'll learn more about how we can define variables in Solidity, their types, and the operators we can use to manipulate them.

Types

As we’ve learned in the previous lessons, we need to specify a type for state variables, local variables, and function parameters. Let’s see what the main types that we can use in Solidity are:

  • uint8, uint16, …, uint256: These are unsigned integers from 8 bits to 256 bits in size.

  • int8, int16, …, int256: These are signed integers from 8 bits to 256 bits in size.

  • unit: This is the same as uint256.

  • int: This is the same as int256.

  • bool: This is a boolean value that can either be true or false.

  • fixed: This denotes fixed point numbers of various sizes.

  • address: This is a type representing an Ethereum account address. It can be an address of a smart contract or an address of an externally owned account.

  • payable address: This is a special address type that we can send Ether to from a smart contract. (We'll look into payments in smart contracts later.)

  • string: This is a sequence of characters to represent text.

As of now, Solidity doesn't fully support fixed point numbers. We can declare a variable of the fixed type but can't assign values to it. Solidity also doesn't support floating point numbers (float and double in other programming languages).

Solidity also has more complex types, such as arrays and mappings, but we'll learn about them later in the course.

Operators

Variables aren't very useful if we can’t perform any operations with them. Like most other languages, Solidity has a basic set of operators that we'll briefly look into.

Solidity has common arithmetic operators, as follows:

Get hands-on with 1200+ tech skills courses.