
Jasmine Grover Content Strategy Manager
Content Strategy Manager
In programming, a constant is just a label given to a variable whose values don't change. Constants are similar to variables in the context of the C programming language, but they differ in that they maintain a single value during program execution.
- This suggests that once a constant's value is established, it is fixed in its given state throughout the program.
- The 'const' keyword is used to specify a constant in the C language.
- The const keyword, referred to as a const type qualifier, is used at the beginning of the variable declaration to designate that variable as a constant.
Table of Content |
Key Terms: Variables, Integer constant, Floating point constant, Real constant, String and character constant
Read More: Tree Topology
Use of the Constants in C
[Click Here for Sample Questions]
In the context of programming, a constant is a specified storage location in a program that keeps a single value constant over its entire runtime. Among other data kinds, these constants can also be character, floating-point, string, double, and integer.
- Constants are divided into two groups in C programming: primary constants and secondary constants.
- Character, real, and integer constants are considered primary constants, while structures, arrays, pointers, unions, and related kinds are considered secondary constants.
Also Read:
What are Literals in C?
[Click Here for Sample Questions]
Literals are values that we assign to variables, and they retain their constancy throughout a program's execution. In most cases, we can use the terms "literal" and "constant" interchangeably. For example, in the expression "const int = 9;", an entire expression is a form of constant expression, and we specifically call the value 9 a constant integer literal.
Types of Constants in C
[Click Here for Sample Questions]
The types of constraints in C are mentioned below in the tabular format:
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
[Click Here for Sample Questions]
Types of constants used in C are mentioned below.
Integer Constants
It could be an octal integer, a decimal integer, or a hexadecimal integer. It represents a decimal integer directly as a plain integer value. For octal integer values, we indicate them by prefixing with 'o,' and for hexadecimal integer values, we use '0x' as a prefix.
- In programming, integer constants can also be of an unsigned or long type.
- Unsigned constant values are marked with 'u' as a suffix and long integer constant values with 'l' as a suffix.
- Furthermore, an unsigned long integer constant is indicated by 'ul' as a suffix.
Constant | Example |
---|---|
Decimal Constant | 10, 20, 450, etc. |
Real or Floating-point Constant | 10.3, 20.2, 450.6, etc. |
Octal Constant | 021, 033, 046 etc. |
Hexadecimal Constant | 0x2a, 0x7b, 0xaa etc. |
Character Constant | 'a', 'b', 'x' etc. |
String Constant | "c", "c program", "c in javatpoint" etc. |
Floating Point Constants / Real Constants
This kind of constant should have integral and fractional components.
- An exponential component is sometimes included in a floating-point constant.
- When a floating-point constant is expressed in exponential form, the suffix 'E' or 'e' must be used to indicate the value of the constant.
Character Constants
Character constants are symbols enclosed within single quotation marks. These character quotations have a maximum length of just one character.
Example,
‘B’
‘5’
‘+’
String Constants
String constants are composed of a variety of special symbols, digits, characters, and escape sequences, all enclosed within double quotation marks.
The definition of a string constant occurs in a single line:
"You go there"
It can be defined with the use of constant multiple lines.
"You \
go \
there"
The definition can also occur using white spaces.
"You" "go" "there"
Rules of Constructing Constants in C
[Click Here for Sample Questions]
Rules to construct these constants in a given program:
Integer Constants
It is required to contain at least one digit. The presence of a decimal point is strictly prohibited. Blank spaces and commas are not permissible.
- An integer constant can have either a positive or negative sign.
- In the absence of a sign in front of the constant, it is considered positive by default.
- This type of constant is valid within the range of -32768 to 32767.
Real Constants
This constant type should contain at least one digit. It should not contain a decimal point.
- It can be either positive or negative.
- It cannot include any spaces or commas.
- If there is no sign in front of the constant, we consider it positive.
String and Character Constants
This category of constant can be a solitary digit, an individual alphabet, or even a lone special symbol, all of which remain enclosed within single quotation marks.
- Double quotation marks encompass string constants.
- The maximum length for this kind of constant is just one character.
Backslash Character Constants
These represent certain character types with unique significance within the C language.
- These constant types require a preceding backslash symbol to enable the program to utilize these distinct functions.
- Below is a comprehensive compilation of all special characters employed in the C language, along with their respective functions:
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
[Click Here for Sample Questions]
In C programming constants can be created by two concepts.
- By using the ‘#define’ preprocessor
- By using the ‘const’ keyword.
Use #define to Define Constants in C
When creating constants using define directives, it's essential to define them at the outset of the program. This is necessary because all preprocessor directives must be written before any global declarations.
Syntax of Constant in C using #define
#define const_name value
Example
// C Program to define a constant using #define#include <stdio.h>#define pi 3.14 int main(){ printf("The value of pi: %.2f", pi); return 0;}
Use of the ‘const’ Keyword
The 'const' keyword is employed to generate a constant of any data type within a program. To create such a constant, the variable declaration must be preceded by the 'const' keyword. Below is the standard syntax we adhere to when employing the 'const' keyword:
Syntax of Constant in C using #const
const <data_type> <var_name> = <value>;
Example
#include <stdio.h>
int main(){ const int STUDENT_ID = 27; const int COURSE_CODE = 502; printf("Student ID: %d is taking the class %d\n", STUDENT_ID, COURSE_CODE);
return 0;}
# OutputStudent ID: 27 is taking the class 502
Read More:
Things To Remember
- Constants are fixed, unchanging values in C.
- They can be categorized as literal constants (hardcoded values) or symbolic constants (user-defined with names).
- Symbolic constants are typically defined using #define or const.
- Naming conventions for symbolic constants often involve uppercase letters and underscores.
- Constants can be used in expressions, just like variables.
- Constants can also be used as function parameters to indicate immutability.
- The 'const' keyword is employed to generate a constant of any data type within a program.
Sample Questions
Ques. What is a constant in C programming? (1 mark)
A) A variable whose value can change during program execution.
B) A named memory location is used for storing data temporarily.
C) A value that remains the same throughout the program's execution.
D) A variable declared within a loop.
Ans. C) A value that remains the same throughout the program's execution.
Explanation: A constant in C programming is a value that remains the same throughout the program's execution. It does not change during runtime.
Ques. Which of the following is a valid way to declare an integer constant in C? (1 mark)
A) int constant = 42;
B) const integer = 42;
C) constant int = 42;
D) None of the above.
Ans. D) None of the above.
Explanation: None of the above. To declare an integer constant in C, you would use the 'const' keyword like this: const int constant = 42;
Ques. What does an octal integer constant start with in C? (1 mark)
A) It starts with '0o'.
B) It starts with '0x'.
C) It starts with '0b'.
D) It starts with '0'.
Ans. D) It starts with '0'.
Explanation: An octal integer constant in C starts with '0'. For example, '012' is an octal integer.
Ques. Which type of constant in C can contain both an integral and fractional part? (1 mark)
A) Integer constant.
B) Floating-point constant.
C) Character constant.
D) Octal constant.
Ans. B) Floating-point constant.
Explanation: A floating-point constant in C can contain both an integral and fractional part. For example, '3.14' is a floating-point constant.
Ques. When using the 'const' keyword to create a constant, where should the declaration typically occur in the C program? (1 mark)
A) It can be declared anywhere in the program.
B) It must be declared within a loop.
C) It must be declared before the 'main' function.
D) It must be declared at the end of the program.
Ans. C) It must be declared before the 'main' function.
Explanation: When using the 'const' keyword to create a constant in C, it is typically declared before the 'main' function or any other global declarations. This ensures that it is available throughout the program.
Ques. Which of the following is used to create constants using preprocessor directives in C? (1 mark)
A) #const
B) #define
C) #include
D) #constdef
Ans. B) #define
Explanation: To create constants using preprocessor directives in C, you use the '#define' directive. For example, #define MY_CONSTANT 10;
Ques. What is the maximum length of a character constant in C? (1 mark)
A) 1 character
B) 10 characters
C) 255 characters
D) There is no maximum length.
Ans. A) 1 character
Explanation: The maximum length of a character constant in C is 1 character. Character constants are typically enclosed in single quotation marks and represent a single character.
Ques. Are double quotes a prerequisite when representing a string constant? (2 marks)
Ans. Yes, in C programming, double quotes are a prerequisite when representing a string constant. String constants are enclosed within double quotation marks. For example:
char *myString = "Hello, World!";
Using double quotes is essential to differentiate string constants from character constants, which are enclosed in single quotation marks.
Ques. Shall we use small caps or uppercase letters for writing the hexadecimal constant? (2 marks)
Ans. In C programming, hexadecimal constants can be written using either small caps (lowercase letters) or uppercase letters. Both formats are valid, and the choice between them is a matter of personal preference or coding style.
For example, the hexadecimal constant for the decimal value 15 (0xF) can be written as either "0xF" (uppercase) or "0xf" (lowercase), and both representations are correct:
int hex1 = 0xF; // Uppercaseint hex2 = 0xf; // Lowercase
It's important to note that C is case-sensitive, so while the letter case of hexadecimal constants doesn't matter, the case of variable names, function names, and keywords in C does matter.
For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates
Check-Out:
Comments