January 25, 2026

Capitalizations Index – B ∞/21M

Types of variables in a Smart Contract – DongTX – Medium

Types of variables in a Smart Contract – DongTX – Medium

Ethereum Developer: Learn Solidity from scratch

Solidity is a statically typed language. Which means that you must specify the type of variable that you’re creating before the name of it, just like in java.

Here are the variables that you can use in a Smart Contract:

Numbers: uint, uint8, uint16, uint24, uint32, uint64, uint128 and uint256

Don’t be scared by the number of similar variables. Uint means “unsigned integer” which is basically a positive number. It can’t be negative. If you try to store a negative number, the value will overflow. The number after uint indicates the size of the variable.

For instance uint8 is capable of storing any positive number with the maximum number being 2⁸ which is = 256. Any number above 255 like 257 will “overflow” which means that it will exceed the capacity of the variable. When you try to store 256 in a uint8 variable, the actual value becomes 0 because it’s the limit of the variable and it just “resets” itself starting again from zero. If you try to store 257 the value becomes 1. So if you read the value of the variable you’ll see 1 instead of 257. Hope that’s clear.

Same thing with uint16 and so on. The maximum value of uint24 is 2²⁴ = 16777216.

Exercise: What’s the maximum value that you can store in a uint256? What happens if you try to store that maximum value + 2?

Solution: 2²⁵⁶ = 1.1579208923731619542357098500869e+77; it becomes 2 instead of that maximum value + 2 because it overflows.

When you write uint without numbers it immediately becomes uint256. It’s just a shortcut to not write such a long number. My recommendation is to always write uint256 independently of the size of the variable because it takes the same amount of gas to process and it’s simpler for you since it adds clarity to the code.

In summary: Keep things simple. Always use uint256 to define numbers.

So how do we define a uint number?

Well let’s go back to the Example.sol file that you just created and define a number:

pragma solidity 0.4.20;
contract Example 

Simple isn’t it? You just defined the type of the variable and the name of it.

Note that you can convert types of numbers to other types easily. For instance, let’s say that you have a variable:

uint256 myNumber = 10;

And you want to convert it to uint8 because you want to store smaller numbers there. You can do so like this:

uint8 anotherNumber = uint8(myNumber); 
// Now it’s converted to uint8 from uint256

Just like that you got a uint8 number. Keep this in mind when you need a specific type of number. It also works with int, which is just a number that can be positive and negative.

By default, the value stored on myValue is 0. Let’s change that value with a function. Just copy the code and you’ll understand later the details of it, this will really help you understand how we do things in Smart Contracts:

pragma solidity 0.4.20; 
contract Example
}

Look at what you just did! You just created a function that changes the value of your variable in 3 lines of code. When you execute the function you are receiving a number called _value and changing the value of myValue to that number.

The public keyword is used to indicate that this function can be executed by any contract and user. Smart Contracts can execute other Smart Contracts and the public keyword of the functions indicate that this function can be executed by anybody. I recommend you to always define the visibility of the functions and variables but don’t worry about it for now. There’s a dedicated chapter about functions visibility later on.

Soon you’ll see how to actually execute this code on the real blockchain. Let’s continue learning all the types of variables that you can use in Solidity.

Addresses

An address is just the public address of every user. When you create an account, you get an address that people can use to send you ether. The good thing is that you can store such addresses on the contract for things like limiting the access to the contract to certain addresses or just logging who is using your code.

Here’s how you declare an address variable:

pragma solidity 0.4.20; 
contract Example
}

You declare them like strings but without quotes. For instance:

address owner = 0x026E36b2BB4eF4621F85e3e134c73A8DCb6ef58e;

Make sure to add the 0x at the beginning of the address.

By default, all addresses have the value 0x0 which is equal to 0x0000000000000000000000000000000000000000. That’s the value addresses have before you give them a value.

Strings and Bytes

Strings and bytes store the same information. The difference is that bytes allows you to store additional raw byte-data while string is only for text.

You can also use bytes1, bytes2, bytes3 up to bytes32. These are used for the same purpose, for storing text and byte data but they use less gas, which is better for the users because they will have to pay less ether for interacting with those variables.

Bytes32 are limited to 32 characters.

In summary: You want to use bytes32 in 99% of the situations because it’s the most efficient way to store text compared to strings. However, it’s limited in size so you if you want to store a big amount of text, use string instead.

Here’s how you use them:

pragma solidity 0.4.20; 
contract Example
}

Booleans

This is the simplest type of variable. It can be either true or false. By default, all Booleans are false so if you don’t assign any value to a boolean, it will be false. Here’s how you define them:

bool isCompleted; 

That’s pretty much how they work. The naming of the booleans usually starts with “is” for readability reasons. That way you can understand quickly that it is a boolean.

Mappings

Mappings are a special type of variable in the sense that they allow you to store unlimited information. They are lists with a value. For instance, if you want to store this information about person and age:

Name: John, Age: 35

Name: Jessica, Age: 31

Name: Lew, Age: 25

You could use a mapping like this:

mapping(string => uint256) peopleAges;

It’s that simple. You use the keyword mapping and then you define the types. The information will be stored like this:

John => 35 
Jessica => 31
Lew => 25

And you’ll be able to access the information like in an array:

peopleAges[“John”];// Will return 35

You can add unlimited entries to the mapping and you can make mappings of mappings:

mapping(string => mapping(string => uint256)) peopleAges;

This will be like a multidimensional array (this is just a representation):

Jessica => John => 35 
=> Lew => 25
=> Matthew => 31

In reality, you will access that information with this syntax:

peopleAges[“Jessica”][“Lew”];// Will return 25
peopleAges[“Jessica”][“Matthew”];// Will return 31

Remember that the name of the mapping is at the end of the mapping declaration, in this case it’s called peopleAges but you can use any name you like.

The variable names in Solidity use the camelCase style where each word is capitalized. Just like in Javascript.

Mappings are very powerful to store huge amounts of information, but they can’t be accessed in order. If you have a mapping of strings to uint256 you won’t be able to see each record individually like in an array because you first have to know the string to get the value. Which means that you won’t be able to loop between the first value and the last value because there is no last value. By default all uints are 0 and addresses are 0x0 which is zero in hexadecimal.

If you want to loop over a mapping, you’ll have to create an array that stores the key of the mapping.

Structs

Struct means structure. Structs allow you to create objects with attributes. It’s like an object in Javascript where you can define properties or “internal variables”. It’s a little bit complicated to explain them so let’s start with an example of how a struct looks like:

struct Tree 

You just defined a struct called Tree with id, name and age. Now you can create instances of that tree like this:

Tree myTree = Tree(1, “Tree name”, 25);

You usually want to store structs in a mapping or in an array since you’ll want to create several different instances of trees.

Remember that each property of the struct, the “internal variables” must end with a semicolon. You can add as many properties as you want, even arrays and mappings.

Also note that the struct in itself doesn’t have a default value.

Arrays

You can make an array of anything but mappings by adding the brackets at the end of the variable keyword. For instance:

uint256[] myNumbers; 
bytes32[] myStrings;
string[] myTexts;
Tree[] myTrees;
// Tree is a struct that we defined earlier and this array contains instances of those structs

You can loop through them with a for or while loop easily since the values are ordered and they always have a last value. You can access each value with:

myNumbers[3];// Returns 93 
myStrings[5];// Returns Laura

There’re 2 types of arrays. The fixed-size arrays and the dynamic-sized arrays. Here’s how they work:

Fixed-size arrays: They are arrays that have a limited size. For instance, an array of 5 strings:
string[5] myNames;
To add values to this array you use the number of the element to modify. The function push does not work in fixed-size arrays:
myNames[2] = ‘John’;
// Ok
myNames.push(‘John’);
// Error this doesn’t work with this type of array
Dynamic-sized arrays: They have unlimited size, so you can keep adding elements as long as you want. You must use the function push to add new elements:
myNames.push(‘John’);
// Ok
myNames[40] = ‘John’;
// It won’t work unless that element has been already modified with push

This is pretty much all the variables that you’ll see in a Smart Contract. There are other types like byte and int but they are not used that much. Int is for storing negative and positive numbers.

Important Global Variables

In every Smart Contract there are specific variables that contain useful information about the request. These are:

  • now or block.timestamp: Both return the same value, the unix value of the current time, a 10 numbers value. It’s very useful to store the time that a specific action was taken. Remember that all the code gets executed when a block is mined so the time will be defined in the block by the miners. This means that the time won’t be exactly the current time, it will always be a little bit less. Keep that in mind.
  • mgs.sender, msg.gas and msg.value: Msg sender contains the address of the user that executed that function, msg gas the remaining gas available at that point of the function and msg value is the amount of Ether sent to that function in wei. A wei is just the smallest unit of an ether. For instance, 1 Ether is 1 * 10¹⁸ wei.
  • days, seconds, minutes, hours, weeks and years: Those are just utilities for time. For instance, 1 minute is 60 seconds and a year is 365 days in seconds. All those variables return the value in seconds. Here’s how you use them:
uint256 myTime = 25 days;
uint256 mySecondTime = 1 minutes;
  • this: Returns the current Smart Contract’s address. You can also use this.balance to get how much ether this contract has in itself in wei.

All this information is useless if it’s not applied in a real-world example. Let’s do that. I’ll walk you through the steps to create a simple Smart Contract that will store To Do notes. Those are the type of notes that some people like to use as a remainder of things that must be done today or in the short term.

Published at Wed, 20 Feb 2019 11:06:09 +0000

Previous Article

Bitcoin Price Looks North As Trading Volumes Hit 9-Month Highs

Next Article

Blo – Okan Yılmaz – Medium

You might be interested in …

Unboxing the $20,000 smartphone

Unboxing The $20,000 Smartphone

Unboxing The $20,000 Smartphone This is one of the most expensive Android smartphones in the world. Sirin Labs Solarin – https://www.sirinlabs.com/ FOLLOW ME IN THESE PLACES FOR UPDATES Twitter – http://twitter.com/unboxtherapy Facebook – http://facebook.com/lewis.hilsenteger Instagram […]

Omicron and hercules review

Omicron and hercules Review

Omicron and hercules Review i sit down with Samurai Sam from Uptech, and he takes us thru the line up of Omicron and Hercules Oil Cartridge Vaporizers. These guys have taken pen cartridges to the […]