Integer literals vs Floating point literals in C#

CsharpProgrammingServer Side Programming

Integer Literals

An integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal. Here are some of the examples of integer literals −

10 // int
18u // unsigned int

Let’s use the above literal while declaring and initializing a variable −

// int
int a =10;

We will now print the values −

Example

 Live Demo

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         // int
         int a =200;
         Console.WriteLine(a);
      }
   }
}

Output

200

Floating-point Literal

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.

The following are some of the examples of floating point literals −

4.89f
314159E-5F

While representing in decimal form, you must include the decimal point, the exponent, or both; and while representing using exponential form you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.

Let us now print the floating point literals −

Example

 Live Demo

using System;
namespace Demo {
   class Program {

      static void Main(string[] args) {

         // float
         float a = 4.89f;
         Console.WriteLine(a);
      }
   }
}

Output

4.89
raja
Published on 10-Aug-2018 07:54:39
Advertisements