Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Constants in C

A constant is a name given to the variable whose values can’t be altered or changed. A constant is very similar to variables in the C programming language, but it can hold only a single variable during the execution of a program. It means that once we assign value to the constant, then we can’t change it throughout the execution of a program- it stays fixed.

In this article, we will take a closer look at Constants in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Use of the Constants in C

A constant is basically a named memory location in a program that holds a single value throughout the execution of that program. It can be of any data type- character, floating-point, string and double, integer, etc. There are various types of constants in C. It has two major categories- primary and secondary constants. Character constants, real constants, and integer constants, etc., are types of primary constants. Structure, array, pointer, union, etc., are types of secondary constants.

What are Literals in C?

Literals are referred to as the values that we assign to the variables that remain constant throughout the execution of a program. Generally, we can use both the terms- literal and constants interchangeably. For instance, the expression “const int = 7;”, is a type of constant expression, while we refer to the value 7 as a constant integer literal.

Types of Constants in C

Type of Constants Data type Example of Data type
Integer constants

int

23, 738, -1278, etc.

unsigned int 2000u, 5000U, etc.
long int, long long int 325,647 1,245,473,940
Floating-point or Real constants

doule

500.987654321

float 20.987654
Octal constant int Example: 013 /*starts with 0 */
Hexadecimal constant int Example: 0x90 /*starts with 0x*/
character constants char Example: ‘X’, ‘Y’, ‘Z’
string constants char Example: “PQRS”, “ABCD”

More About Types of Constants in C

Integer Constants

It can be an octal integer or a decimal integer or even a hexadecimal integer. We specify a decimal integer value as a direct integer value, while we prefix the octal integer values with ‘o’. We also prefix the hexadecimal integer values with ‘0x’.

The integer constant used in a program can also be of an unsigned type or a long type. We suffix the unsigned constant value with ‘u’ and we suffix the long integer constant value with ‘l’. Also, we suffix the unsigned long integer constant value using ‘ul’.

Examples,

55 —> Decimal Integer Constant

0x5B —> Hexa Decimal Integer Constant

O23 —> Octal Integer Constant

68ul —> Unsigned Long Integer Constant

50l —> Long Integer Constant

30u —> Unsigned Integer Constant

Floating Point Constants / Real Constants

This type of constant must contain both the parts- decimal as well as integers. Sometimes, the floating-point constant may also contain the exponential part. In such a case when the floating-point constant gets represented in an exponential form, its value must be suffixed using ‘E’ or ‘e’.

Example,

We represent the floating-point value 3.14 as 3E-14 in its exponent form.

Character Constants

The character constants are symbols that are enclosed in one single quotation. The maximum length of a character quotation is of one character only.

Example,

‘B’

‘5’

‘+’

Some predefined character constants exist in the C programming language, known as escape sequences. Each escape sequence consists of a special functionality of its own, and each of these sequences gets prefixed with a ‘/’ symbol. We use these escape sequences in output functions known as ‘printf()’.

String Constants

The string constants are a collection of various special symbols, digits, characters, and escape sequences that get enclosed in double quotations.

The definition of a string constant occurs in a single line:

“This is Cookie”

We can define this with the use of constant multiple lines as well:

” This\

is\

Cookie”

The definition of the same string constant can also occur using white spaces:

“This” “is” “Cookie”

All the three mentioned above define the very same string constant.

Rules of Constructing Constants in C

Here is how we construct these constants in a given program:

Integer Constants

  • It must have at least one digit.
  • There must be no decimal point.
  • It does not allow any blanks or commas.
  • An integer constant can be both negative or positive.
  • We assume an integer constant to be positive if there is no sign in front of that constant.
  • The allowable range for this type of constant is from -32768 to 32767.

Real Constants

  • This type of constant must consist of one digit at least.
  • There must not be any decimal point.
  • This type of constant can be both negative or positive.
  • It does not allow any blanks or commas within.
  • We assume an integer constant to be positive if there is no sign in front of that constant.

String and Character Constants

  • This type of constant can be a single digit, a single alphabet, or even a single special symbol that stays enclosed within single quotes.
  • The string constants get enclosed within double quotes.
  • The maximum length of this type of constant is a single character.

Backslash Character Constants

  • These are some types of characters that have a special type of meaning in the C language.
  • These types of constants must be preceded by a backslash symbol so that the program can use the special function in them.
  • Here is a list of all the special characters used in the C language and their purpose:
Meaning of Character Backslash Character
Backspace \b
New line \n
Form feed \f
Horizontal tab \t
Carriage return \r
Single quote \’
Double quote \”
Vertical tab \v
Backslash \\
Question mark \?
Alert or bell \a
Hexadecimal constant (Here, N – hex.dcml cnst) \XN
Octal constant (Here, N is an octal constant) \N

Creation and Use of Constants in C

We can create constants in the C programming language by using two of the concepts mentioned below:

  • By using the ‘#define’ preprocessor
  • By using the ‘const’ keyword.

Use of the ‘const’ Keyword

The ‘const’ keyword is used to create a constant of any given datatype in a program. For creating a constant, we have to prefix the declaration of the variable with the ‘const’ keyword. Here is the general syntax that we follow when using the ‘const’ keyword:

const datatype constantName = value ;

OR

const datatype constantName ;

Let us look at an example to understand this better

const int a = 10 ;

In this case, a is an integer constant that has a fixed value of 10.

The program will run as follows:

#include<stdio.h>

#include<conio.h>

void main(){

int q = 9 ;

const int a = 10 ;

q = 15 ;

a = 100 ; // creates an error

printf(“q = %d\na = %d”, q, a ) ;

}

The program given above creates an error. It is because we are trying to change the value of the constant variable (a = 100).

Use of the ‘#define’ preprocessor

One can also use the ‘#define’ preprocessor directive to create the constants. And when we create the constants by making use of the preprocessor directive, we must define it in the very beginning of the program. It is because we must write all the preprocessor directives before the global declaration.

Here is the syntax that we must use for creating a constant by making use of the ‘#define’ preprocessor directive:

#define CONSTANTNAME value

Let us look at an example to understand this better,

#define PI 3.14

In this above-mentioned case, PI is a constant, and it has a value of 3.14.

We can run a program for this as follows:

#include<stdio.h>

#include<conio.h>

#define PI 3.14

void main(){

int a, area ;

printf(“Enter the radius of the given circle here : “) ;

scanf(“%d”, &a) ;

area = PI * (a * a) ;

printf(“The area of the circle is = %d”, area) ;

}

Practice Problems on Constants in C

1. Which of these is a constant variable in the C language?

1. String constant

2. Character constant

3. Real constant

4. Integer constant

A. 2, 3

B. 1, 3, 4

C. 1, 2, 4

D. All of the above

Answer – D. All of the above

2. The constant “Hello” is a:

A. Character Constant

B. String Constant

C. Decimal Constant

D. Octal Constant

Answer – B. String Constant

3. A character constant can be:

A. Single Special Symbol

B. Single alphabet

C. Single digit

D. All of the above

Answer – D. All of the above

4. The statement “A real constant is positive by default, but it can be both negative or positive” is:

A. True

B. False

Answer – A. True


FAQs

Q1

Is there a difference between floating-point constants and real constants?

No. Any unsigned or signed number that has a fractional part is known as a real constant. The real constant is also known as a floating constant.

Q2

Are double quotes a prerequisite when representing a string constant?

Yes. We represent a string constant using double-quotes. The definition of a string constant occurs in a single line:
“This is Cookie”
We can define this with the use of constant multiple lines as well:
” This\
is\
Cookie”
The definition of the same string constant can also occur using white spaces:
“This” “is” “Cookie”
All the three mentioned above define the very same string constant.

Q3

Shall we use small caps or uppercase letters for writing the hexadecimal constant?

We can use both of these when writing a hexadecimal code. These have a suffix of ‘0x’, and it can have both- digits as well as numbers. For example, 0x8B, 0x9A, 0x754, -0x999, etc.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*