27

Is uint the same as uint256 in Solidity?

If this is the case, then why do a lot smart contracts make extensive use of uint256 and not just write uint instead? Is this merely to be explicit?

5 Answers 5

28

Yes, they are exact aliases.

Some developers (including me) prefer to use uint256 because it is consistent with other uint data types, which also specify their size, and also because making the size of the data explicit reminds the developer and the reader how much data they've got to play with, which may help prevent or detect bugs.

Some other languages have left out the bare uint version for just these reasons. For example, Bamboo only has uint256.

10

In addition to what @EdmundEdgar said, it's also better to be explicit when constructing method signature ID's.

For example if doing bytes4(keccak('transfer(address, uint)')), you'll get a different method sig ID than bytes4(keccak('transfer(address, uint256)')) and smart contracts will only understand the latter when comparing method sig IDs.

1
  • Don't be so self demeaning, your answer is superior.
    – Qwerty
    Commented Jan 19, 2022 at 19:35
2

I hate to say RTFM, but this one was too trivial. "uint and int are aliases for uint256 and int256, respectively." --https://solidity.readthedocs.io/en/v0.4.21/types.html#value-types

4
  • Thanks. I was actually reading that exact page and missed it unfortunately. But in any case, I assumed they were the same, but am still unclear why people use uint256 instead of uint in contracts? I see this a lot and it seems unnecessary Commented Mar 19, 2018 at 21:44
  • When I googled it, I came across a hit for an app that was changing all instances of uint to uint256. I'm guessing the reason is to avoid incompatibility if, at some point in the future, the meaning of uint changes to uint512 or something. Much like C and "long" v "long long" v "int" or "short". Commented Mar 19, 2018 at 21:47
  • PS. I've been using uint, myself ... Commented Mar 19, 2018 at 21:48
  • @EliezerSteinbock people use uint256 either to be explicit with their code in case of any changes to the alias of uint and it's visually easier to read.
    – ReyHaynes
    Commented Mar 19, 2018 at 22:05
1

I think it's best to use uint256. It brings about readability and consistency in your code, and it allows you to adhere to best practices in smart contracts.

0

I'll leave something related in case you want to see another example of the consequences of using uint.

https://medium.com/@mattaereal/jackpot-or-honeypot-a-quick-lesson-on-solcs-uint-and-function-selectors-cc1ea7f6afce

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.