Quick Guide To Variables In Solidity
This article aims to provide a better knowledge regarding solidity variable and its types with examples, in order to get a clear understanding of solidity variable type and their usage in building solidity contracts.
What is Variable?
In computer programming, a variable is a named location in memory that stores a value or a reference to a value. Variables are used to store and manipulate data within a program, and they can be modified during the execution of the program. The value of a variable can be of different data types, such as:
- Numbers
- Text
- Boolean
- Objects
Solidity Variables
When writing a program in Solidity, you will need to use various variables to store multiple data.
Solidity variable is nothing more than a reserved memory location where values can be stored.
A variable occupies some memory space when it is created.
You may want to store data of different types, including:
- Character
- Wide character
- Integer
- Floating point
- Double floating point
- Boolean.
The memory allocation and data storage limitations of a reserved memory location are determined by the operating system based on the variable’s data type.
Solidity Variable Types
Solidity is a statically-typed language, which means that every variable must be declared with a data type. In this section of article, we will explore Solidity variables types and how to declare and use them in smart contracts.
One of the most important aspects of writing a Solidity smart contract is choosing the right data type for your variables.
Solidity offers a rich assortment of built-in as well as user-defined data types.
Solidity provides programmers with a wide variety of pre-existing and customizable data types to choose from.
There are seven basic Solidity programming data types listed below:
Types | Keywords | Overview |
Boolean | bool | Solidity’s Boolean variables can only take two values: true or false. Binary decisions or conditions are commonly represented by this symbol. As an example, you can use a Boolean variable to check whether or not a certain address has been approved to access a certain resource. |
Integer | int8,int16 int32, int64, int128 and int256. | In Solidity, developers can store different ranges of values using several integer types. There are six types of integers: int8, int16, int32, int64, int128, and int256. Integer types can store different values depending on their size. The value of an int8 variable can range between -128 and 127, while the value of an int256 variable can range between -2^255 and 2^255 -1. |
Unsigned Integer | uint8,uint16 uint32,uint64 uint128 and uint256. | Solidity’s unsigned integer variables can only store positive values. In common usage, it represents quantities or counts. Unsigned integer types supported by Solidity include uint8, uint16, uint32, uint64, uint128, and uint256. Unsigned integer types can store a range of values based on their size. The value of a uint8 variable can range from 0 to 255, whereas the value of a uint256 variable can range from 0 to 2^256 – 1. |
Address | address | A Solidity address variable represents an Ethereum address. The value is usually expressed as a hexadecimal string consisting of 20 bytes. A number of built-in functions are available in Solidity that can be implemented to work with address variables. You can check an address’s balance using the balance function, while you can send Ether to an address using the transfer function. |
String | string | Solidity uses string variables to store text data. Any sequence of Unicode characters can be stored in this field, and it is not limited in size. String variables can be manipulated with Solidity’s built-in string functions. String length can be obtained with the length function, and string concatenation can be accomplished with the concat function. |
Bytes | bytes bytes2 bytes4 | Solidity uses bytes variables to store binary data. Any sequence of bytes can be stored in it, and its size is not fixed. Solidity comes with several built-in functions that can be used to manipulate bytes variables. A bytes variable’s length can be calculated by the length function, and a bytes variable can be combined using the concat function. |
Fixed Point | fixed8x1, fixed16x8, fixed24x8, fixed32x8, and fixed40x8 | There are fixed-point variables in Solidity that represent decimal numbers with a specific number of digits after the decimal point. There are several fixed-point types in Solidity, including fixed8x1, fixed16x8, fixed24x8, fixed32x8, and fixed40x8. Initially, the type name indicates the number of bits used for the integer part, while the second number indicates the fractional part. For example, a fixed8x1 variable can store values with one decimal place, while a fixed16x8 variable can store values with eight decimal places. |
Solidity Variable Address
The address contains the 20 bytes that represent the size of an Ethereum address.
Example: 
After compiling above example you will same output as below image:
Solidity Integers Variable
The integers data type in solidity contracts can be used as follow:
Example: 
Solidity Unsigned Integers
The given example illustrates all the unsigned integer data types in solidity:
Example: 
Boolean In Solidity
Example: 
Solidity Strings Variable
The integers data type in solidity contracts can be used as follow:
Example: 
Solidity Fixed Point Variable
Example: 
Bytes In Solidity
Example: 
Conclusion
Any programming language relies heavily on variables as they are the building blocks for them.
Similarly, in Solidity, there are different types of data to represent distinct types of data. Basic data types like:
- bool
- int
- uint
- address
- string
- fixed
- unfixed
- bytes
alongside more advanced ones such as structs, arrays, and mappings, comprise Solidity variables data types.
It is crucial to have a good grasp of these data types and their effective usage to create secure and efficient smart contracts.