0% found this document useful (0 votes)
10 views10 pages

Data Types Variables

The document provides an overview of Solidity, a statically typed programming language, detailing its variable types, including Value Types (like Boolean, Integer, and Address) and Reference Types (like Arrays, Strings, and Mappings). It explains the rules for naming variables, the declaration syntax, and the three types of variables: State Variables, Local Variables, and Global Variables. Additionally, it includes examples demonstrating the use of these variable types in Solidity contracts.

Uploaded by

Lahari Betavolu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Data Types Variables

The document provides an overview of Solidity, a statically typed programming language, detailing its variable types, including Value Types (like Boolean, Integer, and Address) and Reference Types (like Arrays, Strings, and Mappings). It explains the rules for naming variables, the declaration syntax, and the three types of variables: State Variables, Local Variables, and Global Variables. Additionally, it includes examples demonstrating the use of these variable types in Solidity contracts.

Uploaded by

Lahari Betavolu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Solidity is a statically typed language, which implies that the type

of each of the variables should be specified. Data types allow the


compiler to check the correct usage of the variables. The declared
types have some default values called Zero-State, for example for
bool the default value is False. Likewise other statically typed
languages Solidity has Value types and Reference types which are
defined below:

Value Types

Value-type variables store their own data. These are the basic data
types provided by solidity. These types of variables are always
passed by value. The variables are copied wherever they are used
in function arguments or assignments. Value type data types in
solidity are listed below:
 Boolean: This data type accepts only two values True or
False.
 Integer: This data type is used to store integer
values, int, and uint are used to
declare signed and unsigned integers respectively.
 Fixed Point Numbers: These data types are not fully
supported in solidity yet, as per the Solidity documentation.
They can be declared as fixed and unfixed for signed and
unsigned fixed-point numbers of varying sizes respectively.
 Address: Address hold a 20-byte value which represents
the size of an Ethereum address. An address can be used
to get a balance or to transfer a balance
by balance and transfer method respectively.
 Bytes: Although bytes are similar to strings, there are
some differences between them. bytes used to store a
fixed-sized character set while the string is used to store
the character set equal to or more than a byte. The length
of bytes is from 1 to 32, while the string has a dynamic
length. Byte has the advantage that it uses less gas, so
better to use when we know the length of data.
 Enums: It is used to create user-defined data types, used
to assign a name to an integral constant which makes the
contract more readable, maintainable, and less prone to
errors. Options of enums can be represented by unsigned
integer values starting from 0.

Example:
In the below example, the contract Types initialize the values of
different types of Values Types.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.4.22 <0.9.0;

/// @title A contract for demonstrate Value types

/// @author Jitendra Kumar

/// @notice For now, this contract just show how Value types works in solidity

contract Types {

// Initializing Bool variable

bool public boolean = false;

// Initializing Integer variable

int32 public int_var = -60313;

// Initializing String variable

string public str = "GeeksforGeeks";

// Initializing Byte variable

bytes1 public b = "a";

// Defining an enumerator

enum my_enum { geeks_, _for, _geeks }

// Defining a function to return

// values stored in an enumerator

function Enum() public pure returns(

my_enum) {

return my_enum._geeks;

}
OUTPUT:

Reference Types

Reference type variables store the location of the data. They don’t
share the data directly. With the help of reference type, two
different variables can refer to the same location where any change
in one variable can affect the other one. Reference types in solidity
are listed below:
 Arrays: An array is a group of variables of the same data
type in which the variable has a particular location known
as an index. By using the index location, the desired
variable can be accessed. The array size can be fixed or
dynamic.
 Strings: Strings are like arrays of characters. When we use
them, we might occupy bigger or shorter storage space.
 Struct: Solidity allows users to create and define their own
type in the form of structures. The structure is a group of
different types even though it’s not possible to contain a
member of its own type. The structure is a reference type
variable that can contain both value type and reference
type
 Mapping: Mapping is the most used reference type, that
stores the data in a key-value pair where a key can be any
value type. It is like a hash table or dictionary as in any
other programming language, where data can be retrieved
by key.
Example: In the below example, the contract Types initialize the
values of various Reference Types.
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.4.22 <0.9.0;

/// @title A contract for demonstrate Reference types

/// @author Jitendra Kumar

/// @notice For now, this contract just show how Reference types works in solidity

contract mapping_example {

// Defining an array

uint[5] public array

= [uint(1), 2, 3, 4, 5] ;

// Defining a Structure

struct student {

string name;

string subject;

uint8 marks;

// Creating a structure object

student public std1;

// Defining a function to return

// values of the elements of the structure

function structure() public returns(

string memory, string memory, uint){

std1.name = "John";

std1.subject = "Chemistry";

std1.marks = 88;
return (

std1.name, std1.subject, std1.marks);

// Creating a mapping

mapping (address => student) result;

address[] student_result;

OUTPUT:

Solidity – Variables

A Variable is basically a placeholder for the data which can be


manipulated at runtime. Variables allow users to retrieve and
change the stored information.

Rules For Naming Variables

1. A variable name should not match with reserved keywords.


2. Variable names must start with a letter or an underscore (_), and
may contain letters from “a to z” or “A to Z” or digits from “0 to 9”
and characters also.
Example:
Geeks123, geeks, _123geeks are valid variable names
123geeks, $Geeks, 12_geeks are invalid variable names
3. The name of variables are case sensitive i.e.
Example:
Geeks123 and geeks123 are different variables

Declaration of Variables

In Solidity declaration of variables is a little bit different, to declare


a variable the user has to specify the data type first followed by
access modifier.
Syntax:
<type> <access modifier> <variable name> ;
Example:
int public int_var;

Types of Variables

Solidity is a statically typed language i.e. each declared variable


always has a default value based on its data type, which means
there is no concept of ‘null’ or ‘undefined’. Solidity supports three
types of variables:
1. State Variables: Values of these variables are permanently
stored in the contract storage. Each function has its own scope, and
state variables should always be defined outside of that scope.
Example: In the below example. the contract
Solidity_var_Test initializes the values of an unsigned integer state
variable using a constructor.
// Solidity program to

// demonstrate state

// variables

pragma solidity ^0.5.0;

// Creating a contract

contract Solidity_var_Test {

// Declaring a state variable


uint8 public state_var;

// Defining a constructor

constructor() public {

state_var = 16;

OUTPUT:

2. Local Variable: Values of these variables are present till the


function executes and it cannot be accessed outside that function.
This type of variable is usually used to store temporary values.
Example: In the below example, the contract
Solidity_var_Test defines a function to declare and initialize the
local variables and return the sum of the two local variables.
// Solidity program to demonstrate

// local variables

pragma solidity ^0.5.0;

// Creating a contract

contract Solidity_var_Test {

// Defining function to show the declaration and

// scope of local variables

function getResult() public view returns(uint){

// Initializing local variables

uint local_var1 = 1;

uint local_var2 = 2;
uint result = local_var1 + local_var2;

// Access the local variable

return result;

OUTPUT:

3. Global Variables: These are some special variables that can be


used globally and give information about the transactions and
blockChain properties. Some of the global variables are listed below
:
Variable Return value

blockhash(uint blockNumber) Hash of a given block, works for only 256 most
returns (bytes32) recent transactions excluding current blocks

block.coinbase (address
Address of current blocks miner
payable)

block.difficulty (uint) The difficulty of the current block

block.gaslimit (uint) Gaslimit of the current block

block.number (uint) Block number of the current block

The timestamp of the current block as seconds


block.timestamp (uint)
since Unix epoch

gasleft() returns (uint256) Amount of gas left

msg.data (bytes calldata) Complete call data of block


Variable Return value

msg.sender (address payable) The sender of message i.e. current caller

msg.sig (bytes4) First four bytes of call data i.e. function identifier

msg.value (uint) Amount of Wei sent with a message

now (uint) The timestamp of the current block

gasleft() returns (uint256) Amount of gas left

tx.gasprice (uint) Price of gas for the transaction

tx.origin (address payable) Transaction sender

Example: In the below example, the contact Test uses


the msg.sender variable to access the address of the person
deploying the contract.
 Solidity

// Solidity program to
// show Global variables
pragma solidity ^0.5.0;

// Creating a contract
contract Test {

// Defining a variable
address public admin;

// Creating a constructor to
// use Global variable
constructor() public {
admin = msg.sender;
}
}

Output:

You might also like