| by Arround The Web | No comments

Constants in C

We need to declare and initialize the values, either changing or fixed in some places, to give a value or assign a weightage for the reference or the manipulation of the specific information while writing a program. For these assignments of the values, we store these values in variables and constants. Constant is the type of that variable whose value remains unchanged/fixed throughout the entire program. Primary constants represent the integers, real numbers, and character constants, whereas secondary constants represent the structures, arrays, unions, and pointers. When we initially declare a constant in a program, we cannot change its value to any other value afterward. We are obliged to use that value as it is in the entire code.

Procedure

Constants in the C are defined using the two different methods depending upon their types. We will implement all these methods in this article. The procedure will include the syntax for the declaration of the constants in the C. Following is the step-by-step process that you may observe to declare the constants with the two different methods in the program.

Syntax

We may define the constants in the program with the following two methods:

  • Constant Keyword
  • # define pre-processor

The syntax for the constant keyword is (const ‘data type’ ‘constant name’ = fixed value we want to assign to the constant). We may define the constant with the second method, i.e., # define pre-processor with syntax (# define “constant name”  “value”).

Example 01

We will solve an example for the primary constant. Various primary constants exist, but, in this example, we will only work with the integer constant. To begin with the example, we will create a project in Visual Studio C. After opening the visual studio, we would have to select the “file” option from the toolbar. We will select the option “New” and “Create project” from the file. This will create a project for your program. To add this project to the path of C files repositories, we will select the option “source file” and select “Add items”.

Then we will name our project with the extension “.c”. This will make our project run on the C fundamentals. Now that the project is successfully created in C, we will include the stdio with extension “.h” to make use of the header file “# include <stdio.h>”. This will let the program use the necessary functions of stdio, i.e., print(), reading input as scanf(), etc. Now, we will use the “$ const keyword” method to declare a primary constant integer. In the body of the main function with data type integer “int “, we will declare constant “def” with data type “int” and assign it some value “34” using the declaration method as “const int def = 34”.

Then we will print this constant by calling the display method printf() with format specification as “%i” and the name of the constant we have already declared. We will exit the main function after returning the value “0”. We have written this explanation of the example as a program so that you may execute it the following way:

#include<stdio.h>
int main () {
    const int def = 34;
    printf (" value of def is: %i", def);
    return 0;}

The declaration method used for the previous example displayed the program’s output as “34”. It has taken the value that we had initialized our constant with at the time of declaration.

Example 02

This example will discuss another method for the declaration of the primary constant in the program. To implement this program, we will create a new project in the C compiler and add the source file with the .c extension to the project’s name to allow the newly created project to run on the C language fundamentals. We will then allow the program to use the required information for displaying and reading output/ input by importing from C’s libraries the header file stdio.h as “#$ # include <stdio.h>”.

Since this example also deals with the definition of the primary constant as the previous example, it will use another method of constant declaration. We will choose the float constant and declare it with the “# define constant_name value” method for this example. We will declare the main function with data type “int”, and, in the body of this function, we will declare a constant having the name “dec” with the value “35.4” as “# define def 35.4″. Note that this declaration method does not specify the data type of the constant in its syntax, so specify the data type of the value “35.4” (that should be float since it is a decimal value). We will print the value of the constant by using the call method of printf() and will specify the type of the constant in the format specification of the printf() method as “%f” for a float with the name of the constant.

This way, the constant’s data type will be specified as a float. We can implement this detailed explanation of the example in the short 2–3 lines in the following form of the C program.

#include <stdio.h>
#define dec 35.14
int main () {
    printf ("%f", dec);
    return 0;
}

The output has displayed the value of the constant as “35.4” by taking its data type from the format specification in the printf() method. This declaration method can take any data type for the constant, which is defined in the format specification for the display method. If we had used the format specification as “%i” for the integer constant, then that would have encountered the compilation error as the value we had declared for the constant was decimal.

Example 03

The next example will deal with the declaration of the secondary constants, and we will declare a structure in this example. We can declare the structure of the program by executing the following code in the C compiler:

#include<stdio.h>
#include<string.h>
struct Cours {
    char abce [50];
    char def [50];
    int   age;
};

Conclusion

The constants can be declared in the C program using different methods. This guide shows the implementation of both methods for the declaration of either the primary constant or the secondary constant. We can get good know-how on the concept of constants in the programming language C with the help of these examples.

Share Button

Source: linuxhint.com

Leave a Reply