Solidity

Literals And Integers in Solidity

Literals

Solidity gives the usage of literal for assignments to variables. Literals don’t have names; they’re the values themselves. Variables can change their values during program execution, but a literal remains an equivalent value throughout. Take a glance at the subsequent samples of various literals:

  • Ilustrations of integer literals are 1, 10, 1,000, -1, and -100.
  • Examples of string literals are “Ritesh” and ‘Modi’. String literals are often in single or quotation marks.
  • Examples of address literals are 0xca35b7d915458ef540ade6068dfe2f44e8fa733c and 0x1111111111111111111111111111111111111111.
  • Hexadecimal literals are prefixed with the hex keyword. An example of hexadecimal literals is hex”1A2B3F”.
  • Solidity supports decimal literals with the utilization of dots. Examples include 4.5 and 0.2.

Integers

Integers help in storing numbers in contracts. Solidity provides the subsequent two sorts of integer:

  • Signed integers: Signed integers may hold both negative and positive values.
  • Unsigned integers: Unsigned integers can hold only positive values alongside zero. they will also hold negative values aside from positive and 0 values.

There are multiple flavors of literals and integers in solidity  for each of those types. Solidity gives uint8 type to represent 8-bit unsigned integer and thereon in multiples of 8 till it reaches 256. In short, there can be 32 different declarations of unit with different multiples of 8, like uint8, uint16, unit24, as far as uint256 bit. 
Mathematical operations like addition, subtraction, multiplication, division, exponential, negation, post-increment, and pre-increment are often performed on integers. the subsequent screenshot shows a number of these examples:

Similarly, there are equivalent data types for integers like int8, int16 till int256. counting on requirements, an appropriately sized integer should be chosen. for instance, while storing values between 0 and 255 uint8 is acceptable, and while storing values between -128 to 127 int8 is more suitable. For higher values, larger integers are often used. The default value for both signed and unsigned integers is zero, to which they’re initialized automatically at the time of declaration. Integers are value types; however, when used as an array they’re mentioned as reference types.

Solidity

Solidity has a number literal type for each rational number. Integer literals and rational number literals belong to number literal types. Moreover, all number literal expressions (i.e. the expressions that contain only number literals and operators) belong to number literal types.
Solidity
Following are the types of Solidity:
  1. Solidity value types include Booleans, integers, fixed point numbers, addresses, contract types, fixed-size byte arrays, rational and integer literals, and enums.
  2. Reference types such as arrays and structs can be stored in these options: memory , storage and calldata .
Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top