Constants in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Variables having fixed values that don’t change and cannot be changed throughout the execution of the program once initialized are called Constants.

There are mainly two types of constants: primary and secondary. Primary and secondary constants are once again divided into subcategories. Constants in C can be declared in two ways, viz. Use the const keyword, or the #define preprocessor directive.

Constants in C

Variables having fixed values that don’t change and cannot be changed throughout the execution of the program once initialized are called Constants.

There are mainly two types of constants: primary and secondary. Primary and secondary constants are once again divided into subcategories. Constants in C can be declared in two ways, viz. Use the const keyword, or the #define preprocessor directive.

Use of the Constants in C

In the C programming language, A variable can be used as a constant by the following methods:

  • Using const keyword.
  • Using the #define preprocessor.

What are Literals in C?

In the C programming language, a literal is a notation used to represent a fixed value in the source code. Literals can be thought of as constants that are directly specified within the program and do not change during its execution. These values can represent various data types, including integers, floating-point numbers, characters, and more.

Types of Constants in C

Here is a table summarizing the various types of constants in the C programming language:

Type of ConstantDescription
Primary ConstantsConstants of type float, integer, and character.
Numeric ConstantsConstants representing numbers in different bases: Decimal Integer, Octal Integer, Hexadecimal Integer, Real Constants (with decimals).
Character ConstantsConstants representing single characters or strings enclosed in single or double quotes, including Single Character Constants, String Constants, and Backslash Character Constants.
Secondary ConstantsConstants related to secondary data types such as arrays, pointers, structures, unions, and enums, where values remain constant throughout program execution.
Constant ArraysArrays with elements that remain constant during program execution.
Constant PointersPointers holding the same address throughout program execution.
Constant StructuresUser-defined structures with constant contents during program execution.
Constant UnionsUnions with constant values during program execution.
Constant EnumsEnums assigning names to integral constants, providing symbolic names for values.

More About Types of Constants in C

Integer Constants

Integer constants represent whole numbers. They can be in various bases like decimal, octal, or hexadecimal.

Decimal Integer Constants:

Octal Integer Constants (starting with '0'):

Hexadecimal Integer Constants (starting with '0x' or '0X'):

Floating Point Constants / Real Constants

Floating-point constants represent numbers with a decimal point.

Character Constants

Character constants represent single characters enclosed in single quotes.

String Constants

String constants represent sequences of characters enclosed in double quotes.

Rules of Constructing Constants in C

In C, constants must adhere to certain rules to be correctly constructed. Here are some rules to keep in mind when defining constants in C:

Integer Constants

Integer constants represent whole numbers and can be constructed using various bases such as decimal, octal, or hexadecimal.

  • Decimal Integer Constants:

  • Octal Integer Constants (starting with '0'):

  • Hexadecimal Integer Constants (starting with '0x' or '0X'):

Real Constants / Floating Point Constants

Floating-point constants represent numbers with a decimal point and can include the use of scientific notation.

String and Character Constants

Character constants represent single characters enclosed in single quotes, while string constants represent sequences of characters enclosed in double quotes.

Backslash Character Constants

Backslash character constants represent special characters using escape sequences.

ConstantsMeaning
\nNewline
\tTab
\\Backslash
\"Double Quote
\'Single Quote
\bBackspace
\fForm Feed
\rCarriage Return
\vVertical Tab
\0Null Character

These rules and examples provide guidance on constructing different types of constants in C, including integer constants, real constants, character constants, and escape sequences.

Creation and Use of Constants in C

In C, constants are essential for maintaining fixed values that should not change during program execution. Constants are crucial for writing clean and maintainable code. There are two primary methods to create and use constants in C: using the 'const' keyword and using the '#define' preprocessor directive.

Use of the 'const' Keyword

The 'const' keyword is a fundamental way to create constants in C. It allows you to declare a variable as constant, ensuring that its value remains unchanged throughout the program. Here's how to use the 'const' keyword to create and use constants:

Here's an example demonstrating the use of the 'const' keyword to declare and use a constant:

In this example, we declare a constant 'pi' using the 'const' keyword, and then we use it to calculate the area of a circle.

Use of the '#define' Preprocessor Directive

The '#define' preprocessor directive is another method to create constants in C. It allows you to define symbolic names for values, making your code more readable and maintainable. Here's how to use the '#define' directive to create and use constants:

Here's an example demonstrating the use of the '#define' directive to define and use a constant:

In this example, we define a constant 'PI' using the '#define' directive and then use it to calculate the area of a circle.

Both methods, using the 'const' keyword and the '#define' directive, are effective ways to create and use constants in C. The choice between them depends on your coding style and the specific requirements of your program.

Practice Problems On Constants In C

Problem 1: Circle Area Calculation

Write a C program that calculates the area of a circle using a constant for Pi (π). Ask the user to input the radius of the circle and then display the area.

Solution:

Problem 2: Temperature Conversion

Create a C program that converts temperature from Celsius to Fahrenheit using a constant for the conversion factor. Prompt the user to enter a temperature in Celsius and display the equivalent temperature in Fahrenheit.

Solution:

Problem 3: Speed Calculation

Develop a C program to calculate the speed of light in a specified medium (e.g., air) using a constant for the speed of light in a vacuum. Take input for the refractive index of the medium and calculate the speed accordingly.

Solution:

FAQs

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

A. In C programming, the terms "floating-point constants" and "real constants" are often used interchangeably. Both refer to constants that represent numbers with a decimal point, such as 3.1415926. These constants can be of type float or double. While there's no strict difference between the two terms, it's important to note that the choice of data type (float or double) determines the precision and range of the constant. float constants are less precise but occupy less memory, while double constants provide higher precision but require more memory.

Q. Are double quotes a prerequisite when representing a string constant?

A. Yes, double quotes (" ") are necessary when representing a string constant in C. String constants are sequences of characters enclosed within double quotes. For example, "Hello, World!" is a string constant. Single quotes (' ') are used for character constants, where only a single character is enclosed, like 'A' or '1'.

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

A. In C, hexadecimal constants can be written using either lowercase letters (a to f) or uppercase letters (A to F) for the hexadecimal digits representing values from 10 to 15. Both lowercase and uppercase letters are accepted and produce the same result. For example, 0xA and 0xa both represent the hexadecimal value 10. The choice of using lowercase or uppercase letters is a matter of coding style and personal preference. It's a good practice to be consistent within your codebase or follow the coding style guidelines of your project or team.

Conclusion

  • The variables whose value cannot be changed throughout the execution of the program are known as constant.
  • Constants are used when programs involving constants are requried.
  • Constants of primary variables are known as primary constants and constants of secondary variables are known as Secondary constants.
  • A variable can be set as constant using the const keyword or using the #define preprocessor.