bscodinglab

Constants

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.By using constants instead of hard-coding values throughout a program, the program becomes more flexible and easier to maintain.

For example, some common types of Constant or literals in C include

  • Integer literals, such as 123 or -456.
  • Floating-point literals, such as 3.14 or -0.001.
  • Character literals, such as ‘a’ or ‘\n’.
  • String literals, such as “Hello, world!” or “123”.
  • Boolean literals, which can be either “true” or “false”.
  • Null pointer literals, which are represented by the value “NULL”.

Literals can be used in many different ways in C programming, such as initializing variables, comparing values, or performing mathematical operations.

Defining Constants

  • Using #define preprocessor.
  • Using const keyword.

1 . Using #define preprocessor.

In C language, the #define directive is used to define constants. A constant is a value that remains the same throughout the program’s execution and cannot be modified.

The #define directive is a preprocessor directive that is used to define a constant in a C program. It is followed by the name of the constant and the value of the constant, separated by a space. For example, the following line of code defines a constant named PI with a value of 3.14159.

Syntex :- #define varName  Value;

Example :- #define pai 3.14159

Once defined, the constant can be used throughout the program by referring to its name. For example, the following code calculates the circumference of a circle using the PI constant:

Example :-  float radius = 5.0;
                     float circumference = 2 * pai * radius;

Example :-

            #include <stdio.h>

            #define MAX_VALUE 100

            int main()

            {
                    int value = 50;

                    if(value > MAX_VALUE)

                   {
                           printf(“Value is greater than %d\n”, MAX_VALUE);
                    }

                    else {
                              printf(“Value is not greater than %d\n”, MAX_VALUE);
                     }

                     return 0;
               }

In this example, MAX_VALUE is defined as a constant with the value of 100 using the #define directive. The constant can be used throughout the program and its value cannot be changed. In the main() function, a variable value is initialized to 50, and the program checks if it’s greater than the MAX_VALUE constant. If it is, the program prints a message saying that the value is greater than the maximum value allowed. Otherwise, the program prints a message saying that the value is not greater than the maximum value allowed.

2 . Using const keyword.

In the C programming language, the const keyword is used to declare a variable as constant, meaning that its value cannot be changed during the execution of the program. The const keyword can be used with any data type, including integers, floating-point numbers, characters, arrays, and pointers.

When a variable is declared as const, it cannot be modified by the program. Any attempt to change its value will result in a compile-time error. This makes const variables useful when you want to ensure that a value remains constant throughout the program, for example, the value of pi, or the size of an array.

Here is an example of how to declare a const variable in C

Example :-  const int MAX_VALUE = 100;

In this example, MAX_VALUE is declared as a constant integer with a value of 100. Once this variable is defined, its value cannot be modified in the program.

It is important to note that const does not make a variable immutable, it only makes it read-only. That is, it prevents the program from changing the value of the variable, but it does not prevent the value from being changed by other means, such as through direct memory manipulation. Therefore, it is important to use const variables with care and only in cases where the value truly needs to remain constant.It is important to note that const does not make a variable immutable, it only makes it read-only.