0% found this document useful (0 votes)
33 views2 pages

Constant

The document discusses different types of constants in C programming including integer, floating point, character, and string constants. It explains how to declare constants using the const keyword and preprocessor directives like #define. Integer, floating point, character, and string constants are declared using const data_type var_name = value. Preprocessor directives like #define name value are also used to declare constants.

Uploaded by

raiayi752
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

Constant

The document discusses different types of constants in C programming including integer, floating point, character, and string constants. It explains how to declare constants using the const keyword and preprocessor directives like #define. Integer, floating point, character, and string constants are declared using const data_type var_name = value. Preprocessor directives like #define name value are also used to declare constants.

Uploaded by

raiayi752
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.What is Constant in C programming?

In C programming, a constant refers to a read-only variable whose value cannot be


modified once it is declared in the program. Constant variables have to be
initialized at the time of its declaration. Otherwise, it will store a garbage
value. Modifications in the value of a constant cannot be made afterwards. This is
also called immutability. To define a constant in C language, we use the syntax
const data_type variable_name = value;

2. Define the different types of Constant and give example for each type.

There are different types of Constant in C programming:


a. integer constant - the constant that refers to whole integer numbers or a
sequence of digits. For example, const int int_const = 16;
b. floating point constant - the type of constant that contains numbers that have
fractiona; parts. For example, const float float_const = 6.21;
c. character constant - the constant type that contains a single character enclosed
within single quotation marks. For example, const char char_const = 'L';
d. string constant - the constant that contains more than one character enclosed
within double quotation marks. It may include letters, digits, special characters,
and blank space. For example, const char *str = "Lance"
Note that "G" and 'G' are different - because "G" represents a string as it is
enclosed within a pair of double quotes, whereas 'G' means a single character.

3. Discuss how to declare constant using the following:


a. const keyword

To declare a constant using const keyword, first is, we have to use the proper
syntax: const data_type var_name = value; . Next is to identify the data type (int,
float, char, string) and we just have to add the const keyword at the start of the
variable declaration. It is important to note that we have to initialize the
constant variable at declaration to prevent it from storing some garbage value as
we wouldn't be able to modify it afterwards.

How to declare constants using const keywords:


const int a = 21; (integer constant)
const float b = 12.16; (floating point constant)
const char c = 'C'; (character constant)
const char *him = "I love you!"; (string constant)

a, b, c, and d are constant. Once their values are set to 21, 12.16, 'c', and
'lans' respectively, these values cannot be modified in the program.

b. define pre-processor directives

A preprocessore directive is a line included in a program that begins with the


character # (hash symbol). It is used to process some programs before compilation.
There are several preprocessor directives commonly used in C programming, such as
#define, #undef, #include, #if, #else, and etc. Here, we use #define pre-processor
directive to declare a constant. The syntax is as follows: #define <VAR_NAME>
<VALUE>, where <VAR_NAME> is the name of the constant, and <VALUE> as the variable
value.

#define A 21
#define B 12.16
#define C 'C'
#define HIM "I love you!"

In these examples, A, B, C, and HIM are constants. Their values are set to 21,
12.16, 'C', and "I love you!" respectively, and these values cannot be modified in
the program.

References:
Constant (computer programming). (2023, November 19). Wikipedia.
https://en.wikipedia.org/wiki/Constant_(computer_programming)

C/C++ Preprocessors. (2023, June 30). GeeksforGeeks.


https://www.geeksforgeeks.org/cc-preprocessors/

Constants in C. (2023, November 1). GeeksforGeeks.


https://www.geeksforgeeks.org/constants-in-c

You might also like