Difference Between 0 And 0u - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Difference between 0 and 0u Ask Question Asked 9 years, 7 months ago Modified 9 years, 7 months ago Viewed 13k times 5

Text from a textbook:

The uint type is 32-bit unsigned integer type. Its default value is the number 0u or 0U (the two are equivalent). The 'u' letter indicates that the number is of type uint (otherwise it is understood as int).

Then is there a difference between 0 and 0u? If not, then why is the default value '0u'. What is the advantage of suffixing an integer with 'u'?

Why would I ever have to use, say '5u', instead of just using '5'?

Share Improve this question Follow asked Apr 12, 2015 at 3:22 discussedtree's user avatar discussedtreediscussedtree 2432 gold badges4 silver badges13 bronze badges 5
  • To show that it is unsigned. Some values are better represented as an absolute, like distances. – Carcigenicate Commented Apr 12, 2015 at 3:24
  • One possible use of '5u' is if you like to use the keyword 'var' a lot. If you write " var j = 5; ", then it would be stored as an int and if you write " var j = 5u; ", then it will be stored as a uint. – discussedtree Commented Apr 12, 2015 at 3:32
  • @Carcigenicate " uint distance = 730480; ". Didn't need to use the suffix 'u' here. – discussedtree Commented Apr 12, 2015 at 3:35
  • 1. Hungarian notation (putting a type identifier in the name) is usually frowned up. 2. The idea is, if you know that the number will never require a sign, it's good practice to use a type that shows that rule. That extra bit you gain doubles the number's capacity too for the same amount of space, which can be useful. – Carcigenicate Commented Apr 12, 2015 at 3:39
  • 1. Where did I put a type identifier in the name? 'uint' was meant to be the type identifier, it is not included in the variable name. The variable name is 'distance' 2. The type identifier 'uint' means 'unsigned integer' and automatically gives me an extra bit. I wouldn't have to suffix 'u' for that. – discussedtree Commented Apr 12, 2015 at 3:54
Add a comment |

2 Answers 2

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 5

By default, the compiler infers a numeric literal to be either double or integral type.

  • If the literal contains a decimal point or the exponential symbol, it is a double.
  • Otherwise, the literal's type is the first type in this list that can fit the literal's value: int,uint,long,ulong.

The suffixes u (or U) and l (or L) are rarely necessary, because the uint, long and ulong types can nearly always be either inferred or implicitly converted from int:

long i=5; //implicit lossless conversion from int literal to long

Similarly d suffix is also redundant, in that all literals with a decimal point are inferred to be double.

double x= 4.0d; //(Here, d is redundant)

The f (float) and m (decimal) suffixes are the most useful and should always be applied when specifying float or decimal literals. Without the f suffix following lines would not compile, because 3.5 would be inferred to be type double, which has no implicit conversion to float.

float f= 3.5f;

Similarly, the following line would not compile without the m (decimal) suffix.

decimal d=-1.25m;

In conclusion, suffixes instructs compilers to consider integral literal (like 123) to be certain type of number- for example long. Similarly, double literal to be considered other types - for example float, decimal and so on.

Share Improve this answer Follow answered Apr 12, 2015 at 4:50 ANewGuyInTown's user avatar ANewGuyInTownANewGuyInTown 6,3875 gold badges35 silver badges46 bronze badges 2
  • In long i=5, is 5 an int literal or is it inferred as a long literal? If the former, does that mean conversion is performed when it is assigned to i? – bgfvdu3w Commented Jan 31, 2018 at 23:40
  • 1 @Mark, yes there is implicit (lossless) conversion from int to long. – ANewGuyInTown Commented May 16, 2019 at 2:02
Add a comment | 3

Their type is different. While an unsuffixed integer literal can be interpreted as either uint or int depending on the context, the u suffix forces it to an unsigned type. For instance, you can't assign 5u to an int variable without a cast because it has a uint value.

You would use it when the type of the expression matters. For instance, var i = 5 makes i an int, but var i = 5u makes i a uint.

You can also use it to select a uint overload when passing a literal as a parameter. For instance:

void Foo(int x); void Foo(uint x); Foo(5); Foo(5u);

In general, though, you don't need it often.

Share Improve this answer Follow answered Apr 12, 2015 at 3:33 zneak's user avatar zneakzneak 138k46 gold badges265 silver badges335 bronze badges 2
  • Why is the default value of uint '0u', and not '0'? – discussedtree Commented Apr 12, 2015 at 3:40
  • 1 Because 0 and 0u don't have the same type, and 0u is the uint one. – zneak Commented Apr 12, 2015 at 3:51
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

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.

  • The Overflow Blog
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...
31 Why do I have to assign a value to an int in C# when defaults to 0? 10 C# unsigned int default value 4 C# 0 (minus) uint = unsigned result? 3 Does an int field have a default of 0? 14 What does 0u mean in c#? 42 Differences between default(int) vs int = 0 5 Difference between default(int?) vs (int?)null 0 By default datatype of literal 0 is int? 0 Why default operator for int? is not consistent? 0 C# How set a non-numeric value to 0

Hot Network Questions

  • What does one contemplate to become a sotāpanna?
  • Boy who can see EM waves but loses the ability because of a thunderstorm
  • Power series of the reciprocal of f defined as a power series
  • What is the best language to speak with locals in Singapore?
  • Calculate mean/variance of sums of randomly chosen numbers from an array
  • Can I pretend that Spearman's = Pearson's correlation coefficients for meta analysis?
  • Can we use the simple present tense to express a habit that starts just about 24 hours or less ago?
  • Categories in which isomorphism of stalks does not imply isomorphism of sheaves
  • How to handle players campaign, inside another player?
  • In what order should I watch the Hunger Games films?
  • Does Windows 11 Pin Behavior Break Password Security Conventions?
  • Gifting $10k to my 17 year old nephew
  • How to stop Apple Sports live activities watch notifications?
  • I want to be a research technician. What consequences could dropping my PhD impose given that I only need a Master's at most to be a technician?
  • Why does C#'s Thread.MemoryBarrier() use "lock or" instead of mfence?
  • What is the correct article before "/s/ sound"?
  • What is the basis for the view of Oneness in Theravadha?
  • Does launch on warning assume incoming ICBMs carry nuclear warheads?
  • Why aren't there any large Ultraviolet (UV) space telescopes?
  • Is there any information in Query Store that can be used to find block leaders?
  • Dantzig-Wolfe Decomposition for nurse Scheduling problem
  • Was it really possible to damage my VGA card by programming it in assembly through its latches registers?
  • Animated short - brief history of mankind
  • Do rediscoveries justify publication?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-cs

Từ khóa » C 0 Vs 0u