Constants in C

Constants in C Language with Examples

In this article, I am going to discuss Constants in C Language with Examples. Please read our previous article, discussing the different parts of a C program. As part of this article, you will learn two things: Character Set and C Constants.

Constants in C Language

Constants in the C programming language are fixed values that do not change during the execution of a program. They are also known as literals. Constants can be of various types, such as:

  • Integer Constants: These are numeric values without decimal points. They can be of different types like int, long, short, etc. Examples include 100, -25, 0.
  • Floating-Point Constants: These are numeric values with decimal points or in exponential notation. Examples are 3.14, -0.001, 1.2e-5.
  • Character Constants: These are single characters enclosed in single quotes, like ‘a’, ‘1’, ‘@’. They represent ASCII values in memory.
  • String Constants: Also known as string literals, they are a sequence of characters enclosed in double quotes, such as “Hello, World!”. They are actually arrays of characters ending with a null character \0.
  • Enumeration Constants: Defined using the enum keyword, these constants make a program easy to read and maintain. The enum keyword automatically assigns names to integral constants, making the code more readable.
  • #define Constants: These are symbolic literals. They are defined using the #define preprocessor directive. For example, #define PI 3.14159 defines PI as a constant with a value of 3.14159.
  • const Keyword: The const keyword can be used to declare a variable as a constant, meaning its value cannot be changed after initialization. For example, const int max = 100;.

Constants are important in C as they improve the readability and maintainability of the code, and they help prevent bugs by protecting against accidental changes to values that should remain constant.

Integer Constant in C Language:

An integer constant is a numeric constant (constant associated with numbers) without any fractional or exponential part. Integer constants 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 have no separators, including commas (,). There are three types of integer constants in the 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
Examples: 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.

The range of integer constants is -32,768 to 32,767.

We can use small caps a, b, and 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 in C Language:

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

The 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, a 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 the escape character along with the character. The escape character says that it is not a normal character and does something. The maximum length can be one character. The value of a character constant is the numerical value of a character in the matching character set. For example, in the ASCII character set, character zero has a numerical value of 48, unrelated to the numerical value 0.

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

String Constant:

A String Constant is a sequence of characters enclosed between double quotes. The characters may be letters, numbers, special characters, and blank spaces. For example (“HelloWorld”), here you can see HelloWorld is a sequence of characters that 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 do you 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. Trying to change constant values after defining them in the C program will throw you an error.

Example using const keyword in C Language:
#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:

Example using const keyword in C Language

Example 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:

Example using #define preprocessor directive in C

Character Set in C

The character set in C is based on the ASCII (American Standard Code for Information Interchange) character encoding, which is a widely used standard to represent text in computers. Each character in the ASCII set is represented by a unique integer value, known as its ASCII code. For example, the ASCII code for the character ‘A’ is 65, and for ‘a’ is 97.

In C programming, the character set is a defined standard that represents letters, digits, and other symbols that can be used within the language. This set includes the following types of characters:

Alphabets:
  • Uppercase letters: A to Z.
  • Lowercase letters: a to z.
Digits:
  • Numeric characters: 0 to 9.
Special Characters:
  • Arithmetic operators: +, -, *, /, %
  • Relational operators: ==, !=, >, <, >=, <=
  • Logical operators: &&, ||, !
  • Assignment operator: =
  • Increment and decrement: ++, —
  • Conditional operator: ? :
  • Bitwise operators: &, |, ^, ~, <<, >>
  • Other symbols: ;, ,, . (dot), : (colon), #, (, ), [, ], {, }, ‘, ” (quotes)
Whitespace Characters:
  • Space: (space character).
  • Horizontal tab: \t.
  • Vertical tab: \v.
  • New line: \n.
  • Form feed: \f.
  • Carriage return: \r.
Escape Sequences:

These are special characters used to represent whitespace or control characters, like \n (new line), \t (tab), \b (backspace), \r (carriage return), \\ (backslash), \’ (single quote), \” (double quote), and \0 (null character). 

  • Newline: \n
  • Horizontal tab: \t
  • Backspace: \b
  • Return: \r
  • Form feed: \f
  • Alert (bell): \a
  • Backslash: \\
  • Single quote: \’
  • Double quote: \”
  • Null character: \0

Understanding the character set is important for string manipulation, character handling, and input/output operations in C programming.

In the next article, I will discuss Operators in C Language with examples. In this article, I try to explain Constants in C Language with examples, and I hope you enjoy this article.

Leave a Reply

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