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:

TypesKeywordsOverview
BooleanboolSolidity’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.
Integerint8,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 Integeruint8,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.
AddressaddressA 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.
StringstringSolidity 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.
Bytesbytes

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 Pointfixed8x1, fixed16x8, fixed24x8, fixed32x8, and fixed40x8There 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.
IMPORTANT: As an alternative, fixed-point numbers can be expressed as fixedMxN/ufixedMxN, where M represents how many bits each type takes and N represents how many decimal places it contains.
M must be divisible by 8 and has a range of 8 to 256.
The value of N can range from 0 to 80.

Solidity Variable Address

The address contains the 20 bytes that represent the size of an Ethereum address.

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest{address public mrx_address;constructor() public{ mrx_address=address(this); }function get_Address() public view returns(address){ return mrx_address; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

After compiling above example you will same output as below image:

Solidity Variables address output


Solidity Integers Variable

The integers data type in solidity contracts can be used as follow:

Example: 

// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest{ function show_value() public pure returns(int8,int16,int32,int64,int128,int256){int8 mrx_1=0; int16 mrx_2=-32761; int32 mrx_3= -2147483641; int64 mrx_4=-9223372036854775701; int128 mrx_5=134217721231231231231231231; int256 mrx_6=1231231231231231231543534545723423412312;return (mrx_1,mrx_2,mrx_3,mrx_4,mrx_5,mrx_6);}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity Unsigned Integers

The given example illustrates all the unsigned integer data types in solidity:

Example: 

// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest{ function show_value() public pure returns(uint8,uint16,uint32,uint64,uint128,uint256){uint8 mrx_1=0; uint16 mrx_2=32161; uint32 mrx_3= 2147483431; uint64 mrx_4=9223372053443545701; uint128 mrx_5=13421772124236224231231231; uint256 mrx_6=123123123454325345354322331543534545723423412312;return (mrx_1,mrx_2,mrx_3,mrx_4,mrx_5,mrx_6);}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Boolean In Solidity

There are only two types of boolean values in solidity programming which are shown as below:

Example: 

// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest{ function show_value() public pure returns(bool,bool){bool is_Solidity_Complex=false; bool is_Mr_Examples_making_it_easier_for_you_to_understand_solidity=true;return (is_Solidity_Complex,is_Mr_Examples_making_it_easier_for_you_to_understand_solidity);}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity Strings Variable

The integers data type in solidity contracts can be used as follow:

Example: 

// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest{ function show_string_name(string memory mrx) public pure returns(string memory){return mrx;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity Fixed Point Variable

The integers data type in solidity contracts can be used as follow:

Example: 

// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest{ufixed8x1 public constant Discount_percentage_in_points = 1.5;function calculatePrice(ufixed8x1 user_amount) public pure returns (ufixed8x1) { return user_amount * Discount_percentage_in_points; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Bytes In Solidity

The integers data type in solidity contracts can be used as follow:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{ function printS_String_From_Bytes(string memory ample) public pure returns (bytes memory) {// Enter the Following input in the Input box and then see the desired output // // 0x4865792055736572202121204920686f706520796f752077696c6c20626520686176696e672066756e206c6561726e696e6720736f6c69646974792066726f6d204d722e4578616d706c6573;return bytes(ample); } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

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.

Note: When selecting a data type, it is essential to take into account the data’s size and accuracy, as well as any storage or gas cost limitations.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *