Integer literals vs Floating point literals in C#

In C#, literals are fixed values written directly in the source code. There are two main categories of numeric literals: integer literals for whole numbers and floating-point literals for decimal numbers. Understanding the difference between these literals is essential for proper variable declaration and avoiding compilation errors.

Integer Literals

An integer literal represents a whole number without a decimal point. Integer literals can be decimal (base 10) or hexadecimal (base 16). A prefix specifies the base: 0x or 0X for hexadecimal, with no prefix for decimal.

Syntax

decimal_literal     // 10, 100, 200
hexadecimal_literal // 0x1A, 0xFF
suffixed_literal    // 10u, 100L, 200UL

Integer Literal Types and Suffixes

Suffix Type Example
None int 10
u or U unsigned int 18u
l or L long 100L
ul or UL unsigned long 200UL

Example

using System;

class Program {
   static void Main(string[] args) {
      // Decimal integer literals
      int decimal_val = 200;
      uint unsigned_val = 18u;
      long long_val = 1000L;
      
      // Hexadecimal integer literals
      int hex_val = 0xFF;
      int hex_val2 = 0x1A;
      
      Console.WriteLine("Decimal: " + decimal_val);
      Console.WriteLine("Unsigned: " + unsigned_val);
      Console.WriteLine("Long: " + long_val);
      Console.WriteLine("Hex FF: " + hex_val);
      Console.WriteLine("Hex 1A: " + hex_val2);
   }
}

The output of the above code is −

Decimal: 200
Unsigned: 18
Long: 1000
Hex FF: 255
Hex 1A: 26

Floating-Point Literals

A floating-point literal represents a decimal number with fractional parts. It consists of an integer part, a decimal point, a fractional part, and optionally an exponent part. Floating-point literals can be represented in decimal form or exponential form using e or E.

Syntax

decimal_form     // 3.14, 0.5, 10.0
exponential_form // 1.5e2, 314159E-5F
suffixed_form    // 4.89f, 2.5d, 1.0m

Floating-Point Literal Types and Suffixes

Suffix Type Example
f or F float 4.89f
d or D double 3.14d
m or M decimal 2.5m
None double (default) 3.14

Example

using System;

class Program {
   static void Main(string[] args) {
      // Decimal form
      float float_val = 4.89f;
      double double_val = 3.14159;
      decimal decimal_val = 2.5m;
      
      // Exponential form
      float exp_val1 = 1.5e2f;      // 1.5 * 10^2 = 150
      double exp_val2 = 314159E-5;  // 314159 * 10^-5 = 3.14159
      
      Console.WriteLine("Float: " + float_val);
      Console.WriteLine("Double: " + double_val);
      Console.WriteLine("Decimal: " + decimal_val);
      Console.WriteLine("Exponential 1: " + exp_val1);
      Console.WriteLine("Exponential 2: " + exp_val2);
   }
}

The output of the above code is −

Float: 4.89
Double: 3.14159
Decimal: 2.5
Exponential 1: 150
Exponential 2: 3.14159

Key Differences

Aspect Integer Literals Floating-Point Literals
Purpose Represent whole numbers Represent decimal numbers
Decimal Point Not allowed Required or implied
Default Type int double
Examples 10, 0xFF, 100L 3.14, 1.5e2f, 2.5m

Conclusion

Integer literals represent whole numbers and use suffixes like u, L to specify types, while floating-point literals represent decimal numbers and use suffixes like f, d, m. Understanding these literals ensures proper variable declaration and prevents type conversion errors in C# programs.

Updated on: 2026-03-17T07:04:35+05:30

480 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements