Constants in C Program

Constants in C Program

In this article, I am going to discuss Constants in C Program with examples. Please read our previous article, where we discussed the different parts of a C program. As part of this article, you will learn two things one is Character set and the other one is C Constants.

Character set in C

Character set refers to the characters that are recognized and accepted in C Language. C recognizes

  • All alphabets from A-Z & a-z.
  • All digits from 0 to 9.
  • Special characters such as #,*, /, &, <, >, (,), {,}, ; etc.
  • Space characters such as blank spaces and tab space etc.
  • Backslash character or escape sequences

Constants in C#

Constants in C

Constants are the terms that cannot be changed during the execution of a program. These fixed values are also called literals. A “constant” is a number, character, or character string that can be used as a value in a program.

Example: 1, 2.5, “C Programming”, etc.

C constants can be classified as:

  • Integer constant
  • Real constant
  • Character constant
  • String constant

Constants in C

Integer Constant:

An integer constant is the numeric constant (constant associated with numbers) without any fractional part or exponential part. Integers constant are always positive until we specify a negative (-) sign. It must have at least one digit. It must not have any decimal point. In between the integer constant we do not have any separators including comma (,). There are three types of integer constant in C language:

Decimal constant (base 10)
Decimal Digits: 0 1 2 3 4 5 6 7 8 9
Example: 0. -9, 22 etc.

Octal constant (base 8)
Octal Digits: 0 1 2 3 4 5 6 7
Example: 021, 077, 033 etc.

Hexadecimal constant (base 16)
Hexadecimal Digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Example: 0x7f, 0x2a, 0x521 etc.

Range of integer constants is: -32,768 to 32,767.

We can use small caps a, b, c instead of uppercase letters while writing a hexadecimal constant. Ever Octal constant stars with 0 and hexadecimal constant starts with 0x in C Programming.

Real Constant:

Any signed or unsigned number with the fractional part is called as real constant. It is also called as Floating-Point constant. Real constant are the numeric constants that have either fractional form or exponential form. It can be positive or negative but by default it is positive. Real constant must have at least one digit. It must have a decimal point. No comma and blank space are allowed within a real constant.

Range of Real Constants is: -3.4*1038 to 3.4*1038.
Decimal Form: Example: -2.0, 0.0000234,
Exponential Form: Example:-0.22E-5 (Here, E-5 represents 10-5. Thus, -0.22E-5 = -0.0000022)

Character Constant:

A character constant is a single digit, single alphabet, or a single special symbol. It must be enclosed within single quotes. Both the inverted commas should point to the left. We use escape character along with the character. The escape character says that it is not a normal character and do something. The maximum length can be 1 character. The value of a character constant is the numerical value of a character in the matching character set for the example in the ASCII character set, the character zero has a numerical value of 48 which is not at all related to the numerical value 0.

Example: ‘A’, ‘5’.
Range of Character Constants is: -128 to 127.

String Constant:

String Constant is a sequence of characters enclosed between double-quotes. The character may be letters, numbers, special characters, and blank space. For example (“helloworld”), here you can see helloworld is a sequence of character which is enclosed between double quotes (“ ”) so, this is a String constant. A string constant is an array of characters with a null character at the end of the string.

Example: “Hello”, “1987”, “?….!”, “x”, ” “, “” (null string constant).

HOW TO USE CONSTANTS IN A C PROGRAM?

We can define constants in a C program in the following ways.

By const keyword and by #define preprocessor directive. If you try to change constant values after defining in C program, it will throw you an error.

PROGRAM USING CONST KEYWORD IN C:

#include <stdio.h>
void main ()
{
  const int height = 100;	/*int constant */
  const float number = 3.14;	/*Real constant */
  const char letter = 'A';	/*char constant */
  const char letter_sequence[10] = "ABC";	/*string constant */
  const char backslash_char = '\?';	/*special char constant */
  printf ("value of height :%d \n", height);
  printf ("value of number : %f \n", number);
  printf ("value of letter : %c \n", letter);
  printf ("value of letter_sequence : %s \n", letter_sequence);
  printf ("value of backslash_char : %c \n", backslash_char);
}

Output:

HOW TO USE CONSTANTS IN A C PROGRAM?

PROGRAM USING #DEFINE PREPROCESSOR DIRECTIVE IN C:
#include <stdio.h>
#define height 100
#define number 3.14
#define letter 'A'
#define letter_sequence "ABC"
#define backslash_char '\?'
void main ()
{
  printf ("value of height : %d \n", height);
  printf ("value of number : %f \n", number);
  printf ("value of letter : %c \n", letter);
  printf ("value of letter_sequence : %s \n", letter_sequence);
  printf ("value of backslash_char : %c \n", backslash_char);
}

OUTPUT:

PROGRAM USING #DEFINE PREPROCESSOR DIRECTIVE IN C:

In the next article, I am going to discuss Operators in C with examples. Here, in this article, I try to explain the C Constants in detail and I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *