.

Constant and Literals in C# Assignment Help

CONSTANTS AND LITERALS

  • Constants are fixed values in a program that cannot be altered during the execution of a program.
  • The fixed values are also called as literals.
  • Constants can be an integer constant, floating constant, character constant, or string literal.

Integer Literal

  • An integer literal value can be a decimal or hexadecimal number. A prefix specifies the base or radix: 0x or 0X for hexadecimal,
  • An integer literal can also have a suffix of U or u and L or l, for unsigned and long, respectively.

e.g. int i = 212;

Floating Point Literal


    Constant and Literals in C# Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com


  • A floating-point literal has an integer value part, a decimal point, a fractional part, and an exponent part.
  • It can be represented either in decimal form or exponential form.

e.g. double d=3.14159 ;

Character Constants

  • Character literals are enclosed in a ( ‘ ‘ ) single quotes
  • It can be a plain character like 'x', an escape sequence like '\t'

e.g. char c=’x’;

'x' and can be stored in a variable of char type.

Escape sequence Characters

Escape sequence

Explanation

\\

\ character

\”

” character

\’

’ character

\r

Carriage return

\a

Alert or bell

\f

Form feed

\b

Backspace

\n

Newline

\?

? character

String Literals

  • The chain of characters that are covered in (“ “) double quotes are string literals
  • A long line can be split into multiple lines using string literals and separating with whitespaces.

e.g. String s= "Hello, dear";

Defining Constants

  • Constants are declared with const keyword.

Syntax

const data_type constant_name = value;

e.g. const double pi = 3.14159;

The following program demonstrates the constant usages

constants and literals image 2
.