Types of Constants in C
Introduction:
Constants are essential in programming because they provide stable values that are true throughout the course of a program. Constants are divided into several categories and used for diverse purposes in the C programming language. It is crucial to comprehend the different sorts of constants if you want to write effective, error-free code. This article will examine the various constant types in C and go through their features and applications.
Types of Constants:
1.Integer Constant:
Integer constants represent whole numbers without decimal points. They are like:
- Decimal Integer Constants: These constants have a base-10 representation and have values between 0 and 9. For instance, Decimal integer constants include 25, 0, and -17.
- Octal Integer Constants: The leading zero (0) is used to denote octal constants in the base-8 system. They employ the numerals 0 through 7. Octal integer constants like 036 and -074 are two examples.
- Hexadecimal Integer Constants: Hexadecimal constants use the base-16 representation and start with the prefix "0x". They employ the letters A through F and the numbers 0 through 9. Examples comprise the values 0x1A, -0xC7, and 0XFF.
2.Floating-Point Constants:
The fractional component of real numbers is represented using floating-point constants. Either decimal notation or exponential notation can be used to express them.
- Decimal Floating point Constants: These constants contain integral portion, the fractional part, a decimal point, and a series of integers. 3.14, -0.75, and 1000.0 are a few examples of decimal floating-point constants.
- Exponential Floating-Point Constants: When employing scientific notation, exponential notation enables the representation of extremely big or tiny quantities. A decimal value and a power of 10 are combined to get the formula for the constant. It is represented by numerals, a possible decimal point, the letter e or 'E,' and an exponent. 6.022e23 and -2.5E-6 are two examples.
3.Character Constants:
Individual characters are represented by character constants, which are single quote marks (' '). They might be special characters, conventional characters, or escape sequences.
- Regular Character Constants: Simple characters wrapped in single quotations are known as regular character constants. For instance, the letters 'A', 'b', and '3' are typical character constants.
- Escape Sequence Character Constants: Escape sequences are unique character combinations that signify a certain concept. Backslashes () are used at the beginning, then a character. 'n' for a newline, 't' for a tab, and 'b' for a backspace are a few examples.
- Special Character Constants: The corresponding escape sequences for special characters are used to represent them. For instance, "0" stands for the null character, "" for a single quote mark, and "" for a backslash.
4.String Constants:
A string constant is a group of characters that are represented by double quotation marks (" "). They are employed to store several characters together.
- An illustration is the string constant "Hello, World!"
5.Enumeration Constants:
User-defined constants called enumeration constants let you provide a group of named values. They may be used to generate symbolic names for a variety of connected variables and are specified using the “enum” keyword.
#include<stdio.h> enum Weekdays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; int main() { enum Weekdays today = Wednesday; switch (today) { case Monday: printf("Today is Monday. "); break; case Tuesday: printf("Today is Tuesday. "); break; case Wednesday: printf("Today is Wednesday. "); break; case Thursday: printf("Today is Thursday. "); break; case Friday: printf("Today is Friday. "); break; case Saturday: printf("Today is Saturday. "); break; case Sunday: printf("Today is Sunday. "); break; default: printf("Invalid day. "); } return 0; }
Output:
6. Symbolic Constants:
The define preprocessor directive is used to define named constants and macros, which are also referred to as symbolic constants. They can be used to provide names to values that stay the same during the course of the programme. Constants written in symbols make code easier to read and maintain.
#include<stdio.h> #define PI 3.14159 #define MAX_VALUE 100 int main() { float radius = 5.0; int limit = MAX_VALUE; float area=PI * radius * radius; printf("The area of a circle with radius %.2f is %.2f ",radius, area); printf("The maximum value allowed is %d ",limit); return 0; }
Output:
Conclusion:
Writing effective and dependable code in C requires a solid understanding of the many sorts of constants. Programmers use a variety of constants for different purposes. These consist of integer constants that vary floating-point constants, text constants, string constants, enumerated constants, even symbolic variables. You may improve code readability, maintainability, and general programme functionality by utilising the right kind of constant.