Tutorial Playlist
132 Lessons1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
Now Reading
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
When diving into the world of programming, understanding the basics is crucial to building a strong foundation. In C programming, one of these basic concepts is "constants."
This article will guide you through the notion of constants in C, their uses, and the different types with comprehensive examples.
By the end of it, you will have gained a robust understanding of constants in C programming language, equipping you to write more efficient and readable code.
Constants, as the term suggests, refer to fixed values that do not change during the execution of a program. They are also known as "literals" in C programming. Once defined, constants in C cannot be modified or reassigned, hence offering a level of safety and predictability in your code.
Constants in C play a pivotal role in writing efficient, readable, and safer code. They are commonly used when a value is repeated throughout the program and when it's crucial to prevent accidental modification of the value.
The role of constants in C is significant as they work on improving the efficiency, readability, and safety of your code. They are often used to define fixed values like the value of PI in mathematics, the number of days in a week, the maximum size of an array, etc.
By now, you must’ve understood what is a constant in C, however, knowing the types of constants in C examples is another imperative aspect of comprehending the right implementation of constants across programs. For instance, integer constants in C are used when the value is a whole number and it doesn't contain any decimal or fractional part. On the other hand, real constants are used when the value has a fractional part, such as the value of PI.
Moreover, having a sound knowledge of primary constants in C, like integer and character constants, along with more complex constants, like enumeration and macro constants, opens up more programming possibilities and gives you more tools to solve problems.
Before delving into the types of constants in C and their examples, let’s learn what are Literals.
In C programming, literals are the data given in a variable or constant. They are the fixed values that a program processes. Examples of literals are numbers (integer or real), characters ('a', 'b', 'c'), and strings ("abc", "hello").
There are several types of constants in C. These include:
Let’s take the subsequent sections to understand these types in more detail, along with well, fleshed out examples and explanations!
Integer constants in C refer to a sequence of digits. There are three types of integer constants: decimal (base 10), octal (base 8), and hexadecimal (base 16).
int decimal = 10; // Decimal constant |
Real constant in C comprise numbers that have a fractional part. They can be written in two forms: decimal form and exponential form.
float decimalForm = 12.345; // Decimal form |
Character constants in C are the single alphabet, digit, or special symbols enclosed within single quotes, like 'a', 'z', '1', '$', etc.
char charA = 'a'; |
String constants in C are a sequence of characters enclosed in double-quotes. A string constant in C automatically adds a null character '\0' at the end.
char string[] = "Hello, World!"; |
In C, we declare a constant using the const keyword followed by the data type and then the constant's name.
const data_type constant_name; |
Here are some examples of declaring constants in C:
const int LENGTH = 10; |
In C, constants can also be initialized using the #define preprocessor directive.
#define constant_name value |
Here are some examples of initializing constants in C:
#define LENGTH 10 |
Enumeration constants (also known as an "enum") in C is a user-defined data type that consists of integral constants. To define enums, we use the keyword enum.
Enumeration constants in C is a way of creating an integral constant with a meaningful name.
Here's the syntax for enumeration constants:
enum enum_name { const1, const2, ..., constn } var_name; |
enum boolean { false, true } isTrue; |
In this example, false is equivalent to 0, and true is equivalent to 1.
Macro constants in C are values that are defined using #define directive. These values cannot be changed during the program execution.
Macro constants are named constants. Instead of using const keyword, we use #define preprocessor to define a constant.
Here's the syntax for macro constants:
#define identifier value |
#define PI 3.14 |
In this example, we’ve defined two macro constants in C - one is by the name PI, and the other is by the name MAX_SIZE.
Using constants in C programming comes with a myriad of benefits that can significantly improve your code's efficiency, readability, and maintainability. Below we'll explore these advantages in greater detail:
By employing constants in your programs, you make your code more understandable. For instance, instead of hardcoding a value like 3.14 throughout your program, you could define a constant PI. Now, whenever PI is referenced, it's immediately clear to any programmer what that value represents. This increase in readability makes it easier for others (and your future self) to understand your code, thereby enhancing its maintainability.
The core attribute of a constant is that its value cannot change. This inherent immutability safeguards critical values in your program. If a particular value needs to stay the same throughout your program's execution (like the value of PI), defining it as a constant will prevent any accidental modifications, leading to fewer bugs and unexpected behaviours.
Constants don't occupy memory in the way variables do. For instance, when you define an integer constant, the C compiler does not allocate memory space for it, unlike an integer variable. Instead, the compiler replaces instances of the constant in your code with its value during compilation. This memory optimisation can be particularly beneficial in memory-constrained environments like embedded systems.
If you're working on a large project with multiple source files, using constants can help standardise certain values across these files. You can define constants in a header file and include that header in any source file that needs those constants. This practice ensures that the same values are used across the entire project, promoting consistency and reducing potential errors.
If you need to change the value of a constant (for example, if you used a constant for the maximum number of students allowed in a class and that number has changed), you only need to change it in one place. Once the constant is updated, all references to it throughout the program will automatically use the new value. This is significantly easier and safer than updating the value everywhere it's used in your code.
Utilising constants in C is a powerful practice that can improve your programs in numerous ways. As you continue to explore C programming, you'll likely find even more situations where constants can be beneficial
As you work with constants in C, it's essential to understand how to construct them properly. There are specific rules for constructing different types of constants: integer constants, real constants or floating-point constants, character constants, string constants, and backslash character constants.
An integer constant in C programming is a sequence of digits. In order to construct integer constants, there are certain rules users must comply with. These include:
Real constants, also referred to as floating-point constants, are numbers that have a fractional part. They can be implemented in two forms: decimal form and exponential form. The rules for constructing real constants are:
String and character constants have their own construction rules:
Backslash character constants are escape sequences that represent special characters. Some backslash character constants are:
Each of these backslash character constants represents a single character constant and can be used wherever a character constant is required.
Following these rules for constructing constants helps to ensure your C programs are syntactically correct, enhancing the program's efficiency and readability.
The const keyword is used in C to declare a constant. This keyword ensures that the variable's value declared using const cannot be changed.
const int LENGTH = 10; |
In this example, LENGTH is declared as a constant integer with a value of 10.
The #define preprocessor is another way of declaring a constant in C
#define LENGTH 10 |
In this example, LENGTH is defined as a constant with a value of 10.
After learning the theory, let's put your knowledge into practice. Here are some problems for you to solve.
1. Problem: Write a C program to calculate the circumference of a circle using a constant PI.
Hint: The formula for the circumference of a circle is 2PIr, where r is the radius of the circle. Declare PI as a constant using #define or const.
2. Problem: Write a C program to create an enumeration constant for the days of the week and print them.
Hint: Use enum keyword to create an enumeration for days of the week. In a loop, print each day.
3. Problem: Write a C program that defines the months of a year as macro constants and prints them.
Hint: Use the #define directive to declare each month as a macro constant. To print, simply use the printf function with the defined constants.
4. Problem: Write a C program that defines constants for a student's name, roll number, and grade they received in a subject. Print these details.
Hint: You can use the const keyword to declare the name and roll number as character constants and grade as an integer constant.
5. Problem: Write a C program that uses a constant integer to define the size of an array. Fill this array with the first n natural numbers and print them.
Hint: Use a constant integer to declare the size of an array. Use a loop to fill and another loop to print the array elements.
Remember, practice is the key to mastering any concept. Happy coding!
Constants in C, though seemingly simple, are critical building blocks in writing efficient and easy-to-understand code. As we've explored, there are various types of constants, each with its unique usage and benefits. They not only make your code safer and more readable but also contribute significantly to memory optimisation.
To unlock more power in C programming and dive deeper into its concepts, check out upGrad’s Master of Science in Computer Science Program, offered under Liverpool John Moores University. Not only will you gain practical knowledge, but you'll also earn an industry-recognised degree to boost your career.
Start your journey with upGrad today!
1. What are constants in C?
Constants in C are fixed values that do not change during the execution of a program.
2. What are the different types of constants in C?
There are several types of constants in C: integer constants, real or floating-point constants, character constants, string constants, enumeration constants, and macro constants.
3. How do I declare and initialise a constant in C?
Constants in C can be declared using the const keyword or initialised using the #define preprocessor directive.
4. What are the advantages of using constants in C?
Using constants in C improves code readability, prevents accidental modification of values, and reduces memory usage.
5. What are enumeration constants in C?
Enumeration constants (also known as an "enum") in C is a user-defined data type that consists of integral constants.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...