0

Given the following code, we can omit the u in the first line, the l in the second line, and the ul in the third line, and the values will be automatically converted to uint, long and ulong, respectively.

So why do we need the suffix (u, l, ul) in this case?

uint uintnum = 7u;
long longnum = 7l;
ulong ulongnum = 7ul;
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Hava Darabi
  • 497
  • 1
  • 5
  • 10
  • For a good explanation, see http://www.dotnetperls.com/suffix – nithins Aug 09 '13 at 01:45
  • 1
    have a look at [this](http://stackoverflow.com/questions/10457333/is-there-any-c-style-guide-that-talks-about-numeric-literal-suffixes) and [this](http://www.parashift.com/c++-faq/numeric-literal-suffixes.html) for about *why* you should use these. – Michi Aug 09 '13 at 01:54
  • please understand my question exactly,then give me a negative vote!! – Hava Darabi Aug 09 '13 at 01:54
  • @GT_mh yes,I got it,and would you please explain the reason of this action in casting conversion for me? – Hava Darabi Aug 09 '13 at 01:59
  • 1
    Why downvotes? Seems like a reasonable question. Wording isn't great, but English may not be this person's first language. – Ryan Aug 09 '13 at 02:07
  • @Ryan,yes English is not my first language,so my Wording isn't great.thanks for your attention! – Hava Darabi Aug 09 '13 at 02:23
  • @Hava Darabi Sorry, I don't understand your second question. Could you elaborate on it? – Michi Aug 09 '13 at 08:34

1 Answers1

4

You don't have to. Both the suffix and the explicit type declaration give the compiler information about how to store the variable. You would need the suffix if you were doing this instead:

var uintnum = 7u;
var longnum = 7l;
var ulongnum = 7ul;
Yuck
  • 49,664
  • 13
  • 105
  • 135
  • ,I know that,I mean why some programmers may do such this? – Hava Darabi Aug 09 '13 at 01:47
  • 2
    If you see programs which use **both** an explicit type declaration **and** the suffix, it's just being redundant. In your examples above only one or the other is actually needed. – Yuck Aug 09 '13 at 01:48
  • ,my teacher said that in other calculation,it may affect,would you please explain me that affect? – Hava Darabi Aug 09 '13 at 01:52
  • 2
    @HavaDarabi: If there is more going on to the right of the assignment operator then the suffix can be important. In cases like that types are determined by the expression on the right before finally being assigned to the left, so that expression can have unexpected results if the types aren't explicitly given. – David Aug 09 '13 at 01:57