Difference Between 0 And 0u - Stack Overflow
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
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 CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet 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 5Text 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 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
2 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 5By 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 longSimilarly 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 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
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 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
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 discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy 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...
Related
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 0Hot 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?
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-csTừ khóa » C 0 Vs 0u
-
Difference Between ~0U And ~0 | C Programming - Coding Forums
-
What Does The Assignment Of ~0u To A Variable Mean In C++?
-
Zero (0 Or 0U) Is Not Equal To Null. And How To Use That With ... - MSDN
-
Unsigned And Signed 0 - C / C++ - Bytes Developer Community
-
What Is The Meaning Of 1u And 0u In The Following Code? - Reddit
-
Placing Suffix U With Unsigned Variables - Keil Forum - Arm Community
-
Why Doesn't Work (unsigned Int) For The Difference Of Two Integer ...
-
C99 Preprocessor Features Adopted In C++0x - IBM
-
C语言中的0U或1U是什么意思? - CSDN博客
-
What Is The Type Of A Constant In C? - Jim Fisher