मराठी

Constants

Advertisements
  • Introduction to Constants
  • Numerical Constants 
  • Defined Constants
  • Symbolic Constants  
Maharashtra State Board: Class 11

Introduction to Constants

Constants are declared with the "const" keyword, a type, their name, and an assignment of their constant value. The value must match the declared type and is fixed for the duration of the program 

Syntax: const data type const_name = value; 
e.g. const float Pi = 3.14; 
const char Blank = ' ';

Constants are of three types 

  • Numerical Constants 
  • Defined Constants
  • Symbolic Constants  
Maharashtra State Board: Class 11

Numerical Constants

A numerical constant in C++ is a fixed numeric value (integer or floating-point) that does not change during program execution. 

  • Integer Constants 
  • Character Constants 
  • Floating Point Constants 
  • String Constants 
  • Hexadecimal Constants 
  • Octal Constants 

1) Integer Constants : 
It is declared with const keyword and has fixed integer value and must have least one digit, either positive or negative, while declaring no special characters are allowed It is assigned the smallest integer type that can hold its value. Suffixes can be used to change the type 
u for Unsigned or L for Long 
For example : const int i = 10;

2) Character Constants :
It is declared with const keyword and has fixed character value It is either a single alphabet, a single digit or a special symbol and enclosed with single inverted commas, and maximum length of a character constant can be 1 character 
For example : const char a = '\t';

  • Backslash Character Constants 
    Backslash character constants are escape sequences. They are not printable characters but they are used in output functions
Code Meaning
\b Moves the cursor one step back
\n Moves the cursor to the next line 
\t Adds a horizontal tab
\' Inserts a single quote 
\\\ Inserts a single quote 
\a Makes a beep sound
\N Not valid in C++; it will cause an error
\f Moves the cursor to the start of the next page
\r Moves the cursor to the beginning of the current line
\" Inserts a double quote character
\0 Represents a null character
\v Adds a vertical tab moves down but not to a new line
\? Inserts a question mark used to avoid trigraph confusion
\xN Inserts a character based on its hexadecimal value N 

3) Floating Point Constant : 
Floating-point constants are also called real constants. Real constants are fractional numbers. They could be written in two forms, fractional form and exponential form. It contains a default to type double. Suffixes can be used f for float or L for long double 
For example : const double pi = 3.14

Real constants in exponential form are used when the values are too large or too small.
They are represented in two parts: 

  • Mantissa: The part before 'e'
  • Exponent: The part after 'e'

Rules for Construction :

  • Mantissa and exponent must be separated by the letter 'e'
  • Depends on how floating-point numbers are represented. A typical valid range: -3.4e38 to 3.4e38 
  • May have a positive or negative sign. The default sign is positive.
  • Must have at least one digit positive or negative Default sign is positive 

For example : 
4.1e32 (i.e., 4.1 x 1032)
5.0e-5 (i.e., 5.0 x 10-5)

4) String Constant :
A sequence of two  or more characters surrounded by double quotes is called a string constant It can be also defined as an array of character constants 

For example : char arr [5] = "abcde." 

5) Hexadecimal constants :
A hexadecimal constant is an integer constant in base 16, using digits 0–9 and letters A–F (A = 10 to F = 15). 

For example : 0xAB is a hexadecimal number

6) Octal constants :
An octal constant is an integer constant in base 8, using digits 0–7 only. 

For example : 0156 is a octal number

Maharashtra State Board: Class 11

Defined Constants

#define Directive (Preprocessor Directive)
The #define directive causes a macro or symbolic name to be defined as a macro. i.e., it sets up an equivalence between an identifier and a text phrase. 

Syntax: #define macro_name replacement_list  OR 
             #define identifier text phrase 

For example:
#define PI 3.14159 
#define NEWLINE '\n' 
#define arrSize 100 

Maharashtra State Board: Class 11

Symbolic Constants

An enumeration is a user-defined data type that consists of a set of named integer constants. It is useful for representing a group of related constant values.
The default numbering of enumerators can be overruled by explicit initialization

Syntax : enum typename {enumeration - list}

  • enum : keyword
  • typename : identifies the name type being defined
  • enumeration - list : list of symbolic constant names

For example :
enum {north , south , east , west}
enum Days {Sunday , Monday , Tuesday , Wednesday , Thursday , Friday , Saturday};

Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×