Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# suffix behind numeric literal

I am new to C# and want to understand how values work. If I look at a normal integer value, it has 3 important parts in it: the type, name and value.

int testInt = 3;  |    |       | Type Name   Value 

But when I see a float value it confuses me a bit because of the suffix F.

float testFloat = 3.0F;   |      |        |  |  Type   Name  Value Type 

Now there are two types in it, and without the F suffix the value would be a double. But why is this happening when I can declare the double variable with

double testDouble = 3.0D; 

The double as the first word should be enough, shouldn't it? The same goes for the decimal value with the suffix M:

decimal testDecimal = 3.0M; 

Then it starts really confusing me when it comes to the other suffixes:

ulong bigOne = 2985825802805280508UL; 

I used ulong in a test before and know that the u is for "unsigned" and lets the value be twice as high as normal. Then you get the U again as suffix and the L for literal as google said. As I understand it, "literals" are value types that contain numbers. But what I don't understand is, why does this ulong work even without the suffix?

ulong bigOne = 2985825802805280508; 

Then I tried something different to understand the importance of the suffix

byte testLong = 12312UL; 

This didn't work because the value is too high for byte (254) and the suffix does not convert it to an long variable.

Why isn't the first word (type) not enough for a declaration? The first word should be enough to tell the type. Is the best practice to always give the values a suffix?

like image 423
user3772108 Avatar asked Jan 06 '16 16:01

user3772108


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

You are confusing two different things here:

float testFloat = 3.0F; 

The float tells the compiler that the variable testFloat will be a floating point value. The F tells the compiler that the literal 3.0 is a float. The compiler needs to know both pieces before it can decide whether or not it can assign the literal to the variable with either no conversion or an implicit conversion.

For example, you can do this:

float testFloat = 3; 

And that's okay. Because the compiler will see 3 as a literal integer, but it knows it can assign that to a float without loss of precision (this is implicit conversion). But if you do this:

float testFloat = 3.0; 

3.0 is a literal double (because that's the default without a suffix) and it can't implicitly (i.e. automatically) convert a double to a float because a float has less precision. In other words, information might be lost. So you either tell the compiler that it's a literal float:

float testFloat = 3.0f; 

Or you tell it you are okay with any loss of precision by using an explicit cast:

float testFloat = (float)3.0; 
like image 127
Matt Burland Avatar answered Oct 05 '22 23:10

Matt Burland


All1 expressions need to be resolvable to a type. So the expression 42 always needs to have exactly one type (it happens to be an int). It can't be an int if you assign it to an int variable and a double if you assign it to a double. The context that an expression is used in is never1 used to determine what type it resolves to.

This is why numeric literals can have suffixes; it's a way of defining the type of that expression in that expression.

Note that there are also implicit conversions between many of the numeric types, so if you write double d = 42; the expression 42 is actually an integer, but there is an implicit conversion operator being performed on it that will convert it into a double before the assignment.

1 There are a few exceptions here, such as lambdas, to which the type of the expression is dependent on how its used, and method groups; in a vacuum these expressions have no type.

like image 32
Servy Avatar answered Oct 06 '22 01:10

Servy