Tuesday, August 4, 2020

Constants Or Literals In C language || Literals in programming Language || Computer Language

C Literals or Constants

 



A constant is a named memory location that holds only one value throughout the execution of the program.

In C programming language, the constant is similar to the variable, but the constant only holds one value during the execution of the program. This means that once a value is assigned to a constant, that value can not be changed during the execution of the program. Once the value is assigned to the constant, the value is fixed throughout the program. A constant can be defined as follows:

 

Type of C Constants

 

Numeric Constants

Integer Constant

An integer constant may be a decimal integer or an octal integer or a hexadecimal integer. The decimal integer value is specified as the direct integer value, while the octal integer value is prefixed with 'o', and the hexadecimal value is prefixed with 'OX.'

 

An integer constant may also be an unsigned type of integer constant or a long type of integer constant. The unsigned integer constant value is suffixed with 'u' and the long integer constant value is suffixed with 'l' while the unsigned long integer constant value is suffixed with 'ul.'

  

Integer Constants are following type:

Decimal --> 123

Octal --> 1576

Hexa --> 59AE

Unsigned --> 50u

Long --> 30I

unsigned long --> 100ul

 

 Floating Point constants

The floating-point constant must contain both the integer and the decimal components. It may also contain an exponent part some times. When a floating-point constant is shown as an exponent, the value must be suffixed with 'e' or 'E.'

 

For example,

 

The floating-point value of 3.14 is shown as 3E-14 in exponent form.

 

 

Character Constants

1.     Single Character Constants

 

A character constant is a symbol that is enclosed in a single quotation. The constant character has a maximum length of one character.

 

Example :

 

'S'

'5'

'>'

'$'

 

2.     Escape Sequence or backslash Characters

 

There are some predefined character constants called escape sequences in the C programming language. Each escape sequence has its special functionality, and each escape sequence is prefixed with the '\' symbol. These escape sequences are used for the output function called 'printf().'

 

Escape Sequence Table:

Escape Sequence name

Symbol

Description

Alert

\a

Make an Alert, such as a beep

Backspace

\b

Moves the cursor Back one space

Formfeed

\f

Move the cursor to next logical page, Formfeed Page Break

Newline

\n

Move the cursor to next line, Newline

Carriage return

\r

Move the cursor to the beginning of the line, Carriage Return

Horizontal tab

\t

Print a Horizontal Tab

Vertical tab

\v

Print a Vertical Tab

Backslash

\\

Print a Backslash

Single Quote

\'

Print a single-quote

Double Quote

\"

Print a double quote

Question Mark

\?

Print a Question mark

Octal/hex number

\(number)

Translates an octal / hex number from a char

 

 

 

 

3.     Trigraph Sequence

 

Release of various reasons for using trigraphs. One reason is that some keyboards may not have keys that cover the whole character set of the C Language, and therefore the input of a special character may be difficult. Using the Trigraph sequence concept, we can use these characters. Each sequence comes in three combinations of characters, i.e. two question marks??) (followed by a special character.

 

Example:

hash sign --> (??=)  

 

 Trigraph Sequence Table:

Trigraph Sequence

 

Replaced With

??=

# (hash) sign

??(

Left parenthesis

??)

Right parenthesis

??<

Left curly brace

??>

Right curly brace

??!

| vertical bar

??/

\ bach slash

??'

^ caret

??-

~ tilde

 

 

String Constants

The string constant is a series of zero or more characters. The string constant is a collection of characters, digits, special symbols, and escape sequences that are enclosed in double-quotes.

 

 

In a single line, we

define the string constant as follows ...

 

 

"This is the studybeat"

"This is a man"

 

 

We can define string constants using multiple lines as follows.

 

 

"This\ 

is\

the\    

studybeats"

 

 

We can also define the constant string by separating it with white space as follows ...

 

 

"This"  "is" "the" "studybeats"

 

 

 

 

How to Use of C constants

 

 

All of the above three define the same string constant.

 

In the C programming language, constants can be created using the following two rules:

 

1. Use the keyword 'const'

2. Use the '# define' preprocessor

 

 

Use the keyword 'const'

 

 

We create a constant for any data type using the 'const' keyword. To create a constant, we prefix the variable declaration with the keyword 'const.'

 

 

The general syntax for creating a constant using the keyword 'const' is as follows ...

 Const datatypename constantname;

OR

Const datatypeName  constantname= value;

 

 

 

Example

const int x = 10 ;

Here, 'x' is an integer constant with a fixed value 10.

 

 

Example Program

#include<stdio.h>

#include<conio.h>

void main()

 {

   int i = 9 ;

   const int x = 10 ;

   i = 15 ;

   printf("i = %d\nx = %d", i, x ) ;

}

Output:

i=15

x=10

Use the '# define' preprocessor

 

Constants can also be created using the '# define' preprocessor directive. When creating a constant using this preprocessor directive, it must be defined at the beginning of the program (because all preprocessor directives must be written before the global declaration is made).

 

 

Use the following syntax to create a constant using the '# define' preprocessor directive ...

 

#define constantname value

Example

#define x 10

Here, x is a constant with value 10

 

 

Example Program

#include<stdio.h>

#include<conio.h>

 

#define  x 10

 

void main()

{

   int y,mul;

   printf("Please enter the value of Y: ") ;

   scanf("%d", &y) ;

   mul = x*y ;

   printf("multiplication of Y with 10 is : %d", mul) ;

}


No comments:

Post a Comment

Thanks

Climate Crisis and Innovation: Navigating Earth's Future

Climate Change: Recent Events and Technological Solutions 1. The Escalating Climate Crisis The climate crisis has intensified in recent year...