C - Constants

<<Previous

Next >>





Constant is applicable to the values which do not change during the program execution. A constant is a name that has same value during the execution of program. These fixed values are called literal. The literal is not a name, it is the value. The literal can be a number value, character value or string value. For example: A= 10, where A is a variable name, 10 is a literal.

Types of Constant

  • Numeric Constants
    1. Integer Constants
    2. Real constants
  • Character constants
    1. Single character Constants
    2. String Constants

Integer Constants

Integer constants include the numbers from 0 to 9 without decimal point or fractional part or any other symbol. The integer literal requires minimum of 2 bytes and maximum of 4 bytes memory space. The integer constants have the range from -32768 to 32767.

The integer constants can be a decimal, octal and hexadecimal. For identifying each integer constant, we can write the number with different prefix. A prefix specifies the base of the number system.

Number Base CharactersPrefixExamples
Decimal100, 1, 2, 3, 4, 5, 6, 7, 8, 9 No Prefix24, 50, 150000, 98534, 2147483647
Octal 80, 1, 2, 3, 4, 5, 6, 7 0010, 07, 09, 076, 054, 070, 067
Hexadecimal160, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E, F0x or 0X 0x09, 0X1A, 0x2, 0x2344, -0x10000

We can also specify the type of integer constant by using suffix letter. We can use the letters U or u for unsigned, L or l for long and UL or ul for both unsigned and long respectively.

Real Constants

It is also called floating point constants. Actually an integer is not enough to represent many parameters. So we need a real constants. The real constant contains an integer part, a decimal point, a fractional part and may also contain an exponential part. The floating point literal may be positive or negative but it must have a decimal point.

Examples

2.5
0.125
3.4e-4
-200.4

In other words, It contains mantissa and an exponent in which mantissa can be either real number or integer number where as an exponent can be an integer number which shows the ten power of these number. An exponent number may be positive (+ve) or negative (-ve).

Example

      The number 2455.321 is equivalent to the value as 2.455e3 or 2.455E3

In general, we can use the floating point literal in our C program with or without suffix letters. The optional suffix letters are ; f (or) F for float and l (or) L for long double.

Single Character Constants

The characters are represented with a single digit or a single special symbol or white space enclosed within a pair of single quote marks. The character constants are stored in the variable of char data type. The character constants have the integer value which is known as ASCII value that is stored internally.

Example


' a '   ' 5 '    ' # '

String Constants

A sequence of characters are enclosed within double quote marks. A string may be a combination of all kind of symbols

Examples


  " Hello "    " length of circle "      " 58889 "   " A#0 "   " number 10 "

Escape Sequence Characters

We can use any character in the ASCII table which may contains some special characters like tab, space and so on. Those special characters are impossible to enter as plain characters from keyboard. In order to handle this issues, the C includes some sets of character which have special meaning. For specifying special characters in our program, we can use backslash and following special character. Following are the list of escape sequence characters and their meaning.

Special CharactersMeaning
\bBackspace
\fForm feed
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
\" Double quote
\'Single quote
\\Backslash
\a Audible alarm
\0 Null character
\? Question mark

How to define Constants?

There are two ways in C for defining constants.

  • Using "const" keyword
  • Using "#define" preprocessor directive

Using "const" Keyword

We can define the constant by using const keyword as a prefix of variable declaration with specific type.

Syntax

	const data-type variable-name = Constant value;

Example

	const int average = 10;

following is an example program using #define directive.

#include <stdio.h>

void main()
{
    const int Num_One = 20;
    const float Num_Two = 50.7;
    const char C_letter = 'C';

    printf("The Num_One value:\t %d \n ",Num_One);
    printf("The Num_Two value:\t %f \n ",Num_Two);
    printf("The C-letter value:\t %c",C_letter);

}

When you compile and execute the above program, you will get the following output.

Output

The Num_One value: 20
The Num_Two value: 50.7
The C_letter value: C

Using "#define" Preprocessor Directive

We can also define constants by using " #define " preprocessor directive.

Syntax

> #define identifier Constant-value

Example

 	#define average 10

Example program using #define directive.

#include <stdio.h>
#define Num_One 20
#define Num_Two 50.7
#define C_letter 'C'

void main()
{
    printf("The Num_One value:\t %d \n ",Num_One);
    printf("The Num_Two value:\t %f \n ",Num_Two);
    printf("The C-letter value:\t %c",C_letter);

}

When you compile and execute the above program, you will get the following output.

Output


The Num_One value: 20
The Num_Two value: 50.7
The C_letter value: C

<< Previous

Next >>







C - Constants

<<Previous

Next >>





Constant is applicable to the values which do not change during the program execution. A constant is a name that has same value during the execution of program. These fixed values are called literal. The literal is not a name, it is the value. The literal can be a number value, character value or string value. For example: A= 10, where A is a variable name, 10 is a literal.

Types of Constant

  • Numeric Constants
    1. Integer Constants
    2. Real constants
  • Character constants
    1. Single character Constants
    2. String Constants

Integer Constants

Integer constants include the numbers from 0 to 9 without decimal point or fractional part or any other symbol. The integer literal requires minimum of 2 bytes and maximum of 4 bytes memory space. The integer constants have the range from -32768 to 32767.

The integer constants can be a decimal, octal and hexadecimal. For identifying each integer constant, we can write the number with different prefix. A prefix specifies the base of the number system.

Number Base CharactersPrefixExamples
Decimal100, 1, 2, 3, 4, 5, 6, 7, 8, 9 No Prefix24, 50, 150000, 98534, 2147483647
Octal 80, 1, 2, 3, 4, 5, 6, 7 0010, 07, 09, 076, 054, 070, 067
Hexadecimal160, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E, F0x or 0X 0x09, 0X1A, 0x2, 0x2344, -0x10000

We can also specify the type of integer constant by using suffix letter. We can use the letters U or u for unsigned, L or l for long and UL or ul for both unsigned and long respectively.

Real Constants

It is also called floating point constants. Actually an integer is not enough to represent many parameters. So we need a real constants. The real constant contains an integer part, a decimal point, a fractional part and may also contain an exponential part. The floating point literal may be positive or negative but it must have a decimal point.

Examples

2.5
0.125
3.4e-4
-200.4

In other words, It contains mantissa and an exponent in which mantissa can be either real number or integer number where as an exponent can be an integer number which shows the ten power of these number. An exponent number may be positive (+ve) or negative (-ve).

Example

      The number 2455.321 is equivalent to the value as 2.455e3 or 2.455E3

In general, we can use the floating point literal in our C program with or without suffix letters. The optional suffix letters are ; f (or) F for float and l (or) L for long double.

Single Character Constants

The characters are represented with a single digit or a single special symbol or white space enclosed within a pair of single quote marks. The character constants are stored in the variable of char data type. The character constants have the integer value which is known as ASCII value that is stored internally.

Example


' a '   ' 5 '    ' # '

String Constants

A sequence of characters are enclosed within double quote marks. A string may be a combination of all kind of symbols

Examples


  " Hello "    " length of circle "      " 58889 "   " A#0 "   " number 10 "

Escape Sequence Characters

We can use any character in the ASCII table which may contains some special characters like tab, space and so on. Those special characters are impossible to enter as plain characters from keyboard. In order to handle this issues, the C includes some sets of character which have special meaning. For specifying special characters in our program, we can use backslash and following special character. Following are the list of escape sequence characters and their meaning.

Special CharactersMeaning
\bBackspace
\fForm feed
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
\" Double quote
\'Single quote
\\Backslash
\a Audible alarm
\0 Null character
\? Question mark

How to define Constants?

There are two ways in C for defining constants.

  • Using "const" keyword
  • Using "#define" preprocessor directive

Using "const" Keyword

We can define the constant by using const keyword as a prefix of variable declaration with specific type.

Syntax

	const data-type variable-name = Constant value;

Example

	const int average = 10;

following is an example program using #define directive.

#include <stdio.h>

void main()
{
    const int Num_One = 20;
    const float Num_Two = 50.7;
    const char C_letter = 'C';

    printf("The Num_One value:\t %d \n ",Num_One);
    printf("The Num_Two value:\t %f \n ",Num_Two);
    printf("The C-letter value:\t %c",C_letter);

}

When you compile and execute the above program, you will get the following output.

Output

The Num_One value: 20
The Num_Two value: 50.7
The C_letter value: C

Using "#define" Preprocessor Directive

We can also define constants by using " #define " preprocessor directive.

Syntax

> #define identifier Constant-value

Example

 	#define average 10

Example program using #define directive.

#include <stdio.h>
#define Num_One 20
#define Num_Two 50.7
#define C_letter 'C'

void main()
{
    printf("The Num_One value:\t %d \n ",Num_One);
    printf("The Num_Two value:\t %f \n ",Num_Two);
    printf("The C-letter value:\t %c",C_letter);

}

When you compile and execute the above program, you will get the following output.

Output


The Num_One value: 20
The Num_Two value: 50.7
The C_letter value: C

<< Previous

Next >>







Learn about Hashing and Hash table

Learn Expression Tree

OS Kernel Architecture

OS Arcitecture of Windows, Linux and MacOS

Learn OS scheduling Algorithms