Header Ads Widget

Constants in C

In C programming language, a constant is a value that cannot be altered during the execution of the program. Constants can be used to define fixed values that are used repeatedly in a program. In this article, we will discuss constants in C, their types, and how to declare and use them in a program.

Types of Constants in C:

In C programming language, constants are classified into two categories:



Primary Constants:

Primary constants are the basic constants that are directly represented in the source code. They include integer constants, real constants, and character constants.
a. Integer Constants:
Integer constants are the whole number constants, which can be represented in decimal, octal, and hexadecimal forms. Decimal constants are represented in base 10, octal constants are represented in base 8, and hexadecimal constants are represented in base 16. For example:

int a = 10;        // Decimal representation of 10int b = 012;       // Octal representation of 10int c = 0xA;       // Hexadecimal representation of 10

b. Real Constants:

Real constants are the floating-point constants that represent real numbers. They can be represented in two forms: decimal form and exponential form. For example:

float a = 3.14;       // Decimal form of real constantfloat b = 3.14E2;     // Exponential form of real constant (314.0)

c. Character Constants:

Character constants are the constants that represent single characters enclosed within single quotes. They are represented in ASCII code format. For example:

char a = 'A';        // Character constant

Secondary Constants:

Secondary constants are the constants that are derived from the primary constants or other secondary constants using operators. They include the following types:

a. Enumeration Constants:

Enumeration constants are the symbolic names assigned to a set of related constants. They are defined using the enum keyword. For example:

enum colors {RED, GREEN, BLUE};    // Enumeration constant

b. Macro Constants:

Macro constants are the constants that are defined using the #define preprocessor directive. They are used to define symbolic constants or to define a set of instructions. For example:

#define PI 3.14       // Macro constant

Declaring and Using Constants in C:

To declare a constant in C programming language, we use the const keyword followed by the data type and the constant name. For example:

const int a = 10;    // Declaration of integer constantconst float b = 3.14; // Declaration of real constantconst char c = 'A';   // Declaration of character constant

Once a constant is declared, it cannot be modified during the execution of the program. Any attempt to modify a constant will result in a compilation error.

Constants can be used in expressions and statements just like variables. For example:

const int a = 10;int b = 20;int c = a + b;        // Constant used in an expression

Advantages of Using Constants:

There are several advantages of using constants in C programming language. Some of them are:

 

In C programming, a constant is a fixed value that cannot be changed during the execution of a program. Constants are used to represent fixed values that are used repeatedly in a program. There are two types of constants in C: literals and symbolic constants.

Literals are the fixed values that are directly used in a program. For example, the number 5 or the character ‘A’ are literals. There are four types of literals in C: integer literals, floating-point literals, character literals, and string literals.

On the other hand, symbolic constants are names given to fixed values. They are defined using the #define preprocessor directive. The value of a symbolic constant remains constant throughout the program, and any attempt to modify it will result in a compilation error.

Let’s look at some examples to understand constants in C programming better.

Example 1: Integer Constants

Integer constants are used to represent integer values. They can be written in decimal, octal, or hexadecimal formats.

#include <stdio.h>int main(){    int x = 10;     // decimal constant    int y = 012;    // octal constant    int z = 0xA;    // hexadecimal constant    printf("x = %d\n", x);    printf("y = %d\n", y);    printf("z = %d\n", z);    return 0;}

Output:

x = 10y = 10z = 10

In the above example, we have used three different formats to represent the integer constant 10. The decimal constant is represented using the decimal format, the octal constant is represented using the prefix ‘0’ followed by the octal digits 0 to 7, and the hexadecimal constant is represented using the prefix ‘0x’ or ‘0X’ followed by the hexadecimal digits 0 to 9 and A to F (or a to f).

Example 2: Floating-point Constants

Floating-point constants are used to represent floating-point values. They can be written in decimal or exponential formats.

#include <stdio.h>int main(){    float x = 3.14;     // decimal constant    float y = 3.14e-2;  // exponential constant    printf("x = %f\n", x);    printf("y = %f\n", y);    return 0;}

Output:

x = 3.140000y = 0.031400

In the above example, we have used two different formats to represent the floating-point constant 3.14. The decimal constant is represented using the decimal format, and the exponential constant is represented using the format aEb or aEB, where a is the mantissa and b is the exponent.

Example 3: Character Constants

Character constants are used to represent character values. They are enclosed in single quotes.

#include <stdio.h>int main(){    char ch1 = 'A';    char ch2 = '\n';    char ch3 = '\t';    printf("ch1 = %c\n", ch1);    printf("ch2 = %d\n", ch2);    printf("ch3 = %d\n", ch3);    return 0;}

Output:

ch1 = Ach2 =Enumeration Constants: Enumeration constants are also called enums, which are user-defined data types. They are used to assign names to the integral constants, which make it easy to read and maintain the code. Enums are declared using the keyword 'enum' and each enum constant is separated using a comma. For example:enum colors { RED, BLUE, GREEN };int main() {enum colors my_color = RED;printf("My favorite color is %d\n", my_color);return 0;}

In the above example, we have declared an enum called ‘colors’ with three constants: RED, BLUE, and GREEN. Inside the main function, we have declared a variable ‘my_color’ of type enum colors and assigned it the value RED. In the printf statement, we are printing the value of my_color using the %d format specifier, which will print the integral value assigned to RED.

Output: My favorite color is 0

String Constants:

String constants are also called string literals, which are sequences of characters enclosed in double-quotes. They are stored in the read-only part of memory and cannot be modified during program execution. String constants are of type char array and are null-terminated, i.e., they end with the null character ‘\0’.

For example:

#include <stdio.h>int main() {char* my_string = "Hello, World!";printf("%s\n", my_string);return 0;}

In the above example, we have declared a string constant called ‘my_string’ and assigned it the value “Hello, World!”. Inside the printf statement, we are using the %s format specifier to print the string.

Output: Hello, World!

Macro Constants:

Macro constants are also called macros, which are defined using the #define preprocessor directive. They are used to define constants that are not limited to a specific data type. Macros are replaced by their values during the preprocessing stage of the program.

For example:

#include <stdio.h>#define PI 3.14159int main() {double radius = 5;double area = PI * radius * radius;printf("The area of the circle is %lf\n", area);return 0;}

In the above example, we have defined a macro constant called PI and assigned it the value 3.14159. Inside the main function, we have declared a variable ‘radius’ and assigned it the value 5. We have then calculated the area of the circle using the formula PI * radius * radius and assigned it to the variable ‘area’. In the printf statement, we are printing the value of area using the %lf format specifier, which will print a double value.

Output: The area of the circle is 78.539750

Conclusion:

Constants in C are variables whose values cannot be changed during program execution. They are used to define values that are fixed and will not change during program execution. In this article, we have discussed different types of constants in C, including integer constants, floating-point constants, enumeration constants, string constants, and macro constants. We have also provided examples of each type of constant to help you understand how they are used in C programming.

Yhaa You have done it but next? if YOU Want to your Others Programming Then Follow US HERE and Join Telegram.

Post a Comment

0 Comments