Constants in C and C++ - const c, String Constants

<<Previous

Next >>





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

Types of Constants in C and C++

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

Integer Constants in C and C++

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 system and Possible values

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

Real constants are also called floating point constants. Actually an integer is not enough to represent many parameters. So we need 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.


Real Constants in C and C++- 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).


Exponent numbers in C and C++

      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 in C and C++

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

String Constants - 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 in C and C++?

There are two ways in C for defining constants.

  • Using const keyword
  • Using #define preprocessor directive

Using const Keyword in C and C++

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

const - Syntax

	const data-type variable-name = Constant value;

const - Example

	const int average = 10;

Example program in C and C++ using const keyword

#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


#define Preprocessor Directive in C and C++

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

#define Syntax

 	#define identifier Constant-value

#define 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 >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length




Constants in C and C++ - const c, String Constants

<<Previous

Next >>





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

Types of Constants in C and C++

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

Integer Constants in C and C++

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 system and Possible values

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

Real constants are also called floating point constants. Actually an integer is not enough to represent many parameters. So we need 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.


Real Constants in C and C++- 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).


Exponent numbers in C and C++

      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 in C and C++

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

String Constants - 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 in C and C++?

There are two ways in C for defining constants.

  • Using const keyword
  • Using #define preprocessor directive

Using const Keyword in C and C++

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

const - Syntax

	const data-type variable-name = Constant value;

const - Example

	const int average = 10;

Example program in C and C++ using const keyword

#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


#define Preprocessor Directive in C and C++

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

#define Syntax

 	#define identifier Constant-value

#define 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 >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy