Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In the C programming language, we have constants, which are variables which cannot be modified once defined, that is, their value cannot be changed once initialized. They are also known as literals. Constants can be of any data type like int, char, etc. There are enumeration constants as well.

Also Read : C Static Function, Tribonacci Series

Types of constants

Integer Constants

These are the constants which can be an octal integer, decimal integer, or hexadecimal. A prefix is used to specify the base, like ‘O’ is used as a prefix with octal integer, ‘0x’ is used as a prefix with a hexadecimal integer. While we specify decimal integer value directly as an integer value. Integer constants can be of the unsigned or long type also. ‘u’ is used as a suffix with unsigned constant value whereas ‘l’ is used as a suffix with long integer constant value and ‘ul’ is used as a suffix with unsigned long integer constant.

E.g

74         - decimal 
0321       - octal 
0x3a       - hexadecimal 
45         - int 
42u        - unsigned int 
31l         - long
65ul       - unsigned long

 

Floating-Point Constants

These constants are made up of integer part, decimal part, fractional part, and exponent part. When represented using the decimal form: the decimal point, the exponent, or both must be included. Whereas when represented using exponent form: the integer part, fractional part, or both must be included. The signed exponent is represented by e or E.

E.g., floating-point value 3.14 is represented as 3E-14 in exponent form.

Also read - Bit stuffing program in c

Character Constants

These constants are enclosed within single quotes. It can contain only one character. In the C language, there are certain predefined character constants known as escape sequences, where each sequence has its own special functionality. ‘\’ is used as a prefix with each of these sequences. Escape sequences are used in output functions such as printf().

E.g., ‘\t’ for horizontal tab, ‘\n’ for new line.

 

String Constants

These are the constants which are a collection of various special symbols, digits, characters, and escape sequences. String constants are enclosed within double-quotes. A long line can be divided into multiple lines using string constants and are separated using white spaces.

E.g.,

"Hello World"
"Hello \
World"
"Hello " "World"

 

All of the above-mentioned forms yield the same identical string.

You can also read about the jump statement and Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Rules for Constructing Constants

Integer Constants

  • The constant must contain at least one digit.
  • The constant cannot have a decimal point, blanks, or commas.
  • The constant can be both positive or negative. By default, if there is no sign, the integer constant is assumed to be positive. 

 

String and Character Constants

  • A character constant can be either a single alphabet, a single digit, or a single special symbol. 
  • A character constant must be enclosed within single quotes.
  • The maximum length of a character constant is one single character.
  • The string constant must be enclosed within double-quotes.

Defining Constants

There are two ways in which we can declare and define a constant in C, which are as follows:

Using #define preprocessor

‘#define’ is a preprocessor directive which is used to create constants in the C language. When creating constants using ‘#define’, we must define them in the beginning of the program as all the preprocessor directives are written before the global declaration.

Syntax

#define identifierName value

 

Example

#include <stdio.h>
#define l 20   
#define b  3
#define change_line '\n'

int main() {
  int area;  
  
  area = l * b;
  printf("Area of rectangle is : %d", area);
  printf("%c", change_line);

  return 0;
}

 

Output

Area of rectangle is : 60

 

Using the const Keyword

The keyword ‘const’ is used to create constants of any data type in a program. The keyword ‘const’ is used as a prefix when declaring a constant. The method used for declaring constants using the keyword ‘const’ is the same as defining a variable, with the only difference being we have to use ‘const’ as a prefix.

Syntax 

const dataType variableName = value;

 

Example

#include <stdio.h>
  
int main()
{
    // int constant
    const int intVal = 63; 
  
    // char constant 
    const char charVal = 'X'; 
  
    // string constant
    const char stringVal[10] = "CODING"; 
      
    printf("The value of Integer constant:%d \n", intVal);
    printf("The value of Character constant: %c\n", charVal);
    printf("The value of String constant: %s\n", stringVal);
      
    return 0;
}


Output

The value of Integer constant:63 
The value of Character constant: X
The value of String constant: CODING

For practice, you can implement it on a C compiler for better understanding.

Frequently Asked Questions

  1. What is the difference between integer and floating-point constants?
    Integer constants represent whole number values like -16, 1846, etc., whereas floating constants represent fractional numbers like -14.08, 625.218, etc.
  2. What is the difference between character and string constants?
    Character constants are enclosed within a pair of single quotes, whereas string constants are enclosed within a pair of double quotes. A character constant can have only one character, whereas a string constant can have multiple characters.
  3. What is the key difference between declaring a constant using the #define and using the keyword ‘const’?  
    When we declare a constant using the #define method, the constant is declared in global scope, whereas when we declare a constant using the ‘const’ keyword, it is declared in the scope of that particular block.

Key Takeaways:

In this blog, we have covered the following topics:

  • We first discussed what constants in the C programming language are?
  • Then we discussed the various types of constants in the C programming language.
  • Finally, we discussed how we could declare constants in the C language.

C has many Data structures to offer, which are used for various different purposes like arrays for storing continuous data, strings to represent a sequence of characters, and many more.

In order to write a well-structured code, knowledge of functionsstructures, and different data types is very important.

Topics covered
1.
Introduction
2.
Types of constants
2.1.
Integer Constants
2.2.
Floating-Point Constants
2.3.
Character Constants
2.4.
String Constants
3.
Rules for Constructing Constants
3.1.
Integer Constants
3.2.
String and Character Constants
4.
Defining Constants
4.1.
Using #define preprocessor
4.2.
Using the const Keyword
5.
Frequently Asked Questions
6.
Key Takeaways: