At first, sorry for my bad english. I have fragment of code:
long x = 9223372036854775807L;
double f = x;
Console.WriteLine(x);
Console.WriteLine(f);
Output is:
9223372036854775807
9,22337203685478E+18
I'm not getting any errors while compiling and execution this code. We have a loss of precision while converting Long to Double. Why C# does not require explicit casting in that case ?
Thanks all.
Why Learn C? There are an awful lot of programming languages available right now -- everything from the extremely high level (such as Visual Basic) to the low level power of assembly, and a good variety of specialized options in between ( Perl, Ruby, and Python are good choices for many tasks).
Plus, with C, you get lots of strong opinions mixed with insights that you can understand. As a result of its age and its use as the language of system programming for Unix, C has become something of the lingua franca of programming. C is a great language for expressing common ideas in programming in a way that most people are comfortable with.
While it's an integer, the %c interprets its numeric value as a character value for display. For instance for the character a: If you used %d you'd get an integer, e.g., 97, the internal representation of the character a using %c to display the character ' a ' itself (if using ASCII)
Also, when using %c you want a character printed, not a number. In the D language, you would always use %s and let the compiler worry about the types. @MatejNanut No, integer types smaller than int are promoted to int when being passed into a variadic function. @Pubby: thank you, I didn't know that.
The language has some implicit conversion built into it.
The following table is from the documentation, which is why you are allowed to assign the value without an explicit cast or conversion:
From To
===============================================================================
sbyte short , int, long, float, double, or decimal
byte short , ushort, int, uint, long, ulong, float, double, or decimal
short int , long, float, double, or decimal
ushort int , uint, long, ulong, float, double, or decimal
int long , float, double, or decimal
uint long , ulong, float, double, or decimal
long float , double, or decimal
char ushort , int, uint, long, ulong, float, double, or decimal
float double
ulong float , double, or decimal
And in the documentation it states (emphasis mine):
Precision but not magnitude might be lost in the conversions from int, uint, long, or ulong to float and from long or ulong to double.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With