Constants in C Language – Types, Examples and Uses

What is a Constant in C Language?

Constants are fixed values used in a C program. Their values are not intended to change during program execution. Constants are also closely related to literals, which are fixed values written directly in the source code. In C language, constants can be represented using integer, floating-point, character and string literals. The const keyword and #define preprocessor directive can also be used to define values that should not be changed during program execution.

C Language में Constants का उपयोग निश्चित मानों को दर्शाने के लिए किया जाता है। Program Execution के दौरान इन मानों को बदलने का उद्देश्य नहीं होता है। Constants का संबंध Literals से भी होता है, जो Source Code में सीधे लिखे गए निश्चित मान होते हैं। C Language में Constants को Integer, Floating-Point, Character तथा String Literals के रूप में लिखा जा सकता है। Program Execution के दौरान जिन मानों को बदलना नहीं चाहिए, उन्हें Define करने के लिए const Keyword तथा #define Preprocessor Directive का भी उपयोग किया जा सकता है।

Types of Constants in C Language

The commonly used categories of constants (literals) in C language are:

C Language में सामान्यतः Constants (Literals) की निम्नलिखित श्रेणियाँ होती हैं:

  1. Integer Constants

  2. Floating-Point Constants

  3. Character Constants

  4. String Literals

1. Integer Constants

Integer constants are whole-number values without a fractional part. They may represent positive, negative or zero values.

Integer Constants वे पूर्णांक मान होते हैं जिनमें दशमलव भाग नहीं होता है। ये धनात्मक, ऋणात्मक या शून्य मान हो सकते हैं।

Integer constants can be written in three commonly used number systems:

Integer Constants को सामान्यतः तीन Number Systems में लिखा जा सकता है:

(a) Decimal Integer Constants

Decimal integer constants use digits from 0 to 9. They are written using the base-10 number system.

Decimal Integer Constants में 0 से 9 तक के अंकों का प्रयोग किया जाता है। इन्हें Base-10 Number System में लिखा जाता है।

Examples:

10
100
-25
5000

(b) Octal Integer Constants

Octal integer constants use digits from 0 to 7 and begin with the digit 0.

Octal Integer Constants में 0 से 7 तक के अंकों का प्रयोग किया जाता है तथा इनकी शुरुआत Digit 0 से होती है।

Examples:

010
040
077

Here, 040 represents decimal value 32, and 010 represents decimal value 8.

यहाँ 040 का Decimal मान 32 तथा 010 का Decimal मान 8 होता है।

(c) Hexadecimal Integer Constants

Hexadecimal integer constants use digits from 0 to 9 and letters A to F. They begin with 0x or 0X.

Hexadecimal Integer Constants में 0 से 9 तक के अंकों तथा A से F तक के अक्षरों का प्रयोग किया जाता है। इनकी शुरुआत 0x या 0X से होती है।

Examples:

0x10
0x40
0xFF

Here, 0x40 represents decimal value 64, and 0xFF represents decimal value 255.

यहाँ 0x40 का Decimal मान 64 तथा 0xFF का Decimal मान 255 होता है।

The letters O and o must not be confused with the digit 0. Octal and hexadecimal prefixes use the digit zero.

Letter O और o को Digit 0 से अलग समझना चाहिए। Octal तथा Hexadecimal Prefix में Digit Zero का उपयोग किया जाता है।

Integer Constant Suffixes

Integer constants may also use suffixes to indicate their type, such as U, L and LL.

Integer Constants के साथ उनके Type को दर्शाने के लिए U, L तथा LL जैसे Suffix का प्रयोग किया जा सकता है।

Examples:

100U
500L
1000LL
250UL

Here, U represents unsigned, L represents long and LL represents long long.

यहाँ U Unsigned, L Long तथा LL Long Long को दर्शाता है।

2. Floating-Point Constants (Real Constants)

Floating-point constants represent numbers that contain a fractional part or are written using exponential notation. They are also known as real constants.

Floating-Point Constants ऐसे संख्यात्मक मान होते हैं जिनमें दशमलव भाग होता है या जिन्हें Exponential Notation में लिखा जाता है। इन्हें Real Constants भी कहा जाता है।

Examples:

3.14
25.50
-10.75
2.5e3

Here, 2.5e3 represents (2.5 \times 10^3), which is equal to 2500.

यहाँ 2.5e3 का अर्थ (2.5 \times 10^3) है, जिसका मान 2500 होता है।

Floating-point constants can have the following suffixes:

Floating-Point Constants के साथ निम्नलिखित Suffix का प्रयोग किया जा सकता है:

  • No suffix – The constant has type double.

  • f or F – The constant has type float.

  • l or L – The constant has type long double.

  • बिना Suffix के Constant का Type double होता है।

  • f या F लगाने पर Constant का Type float होता है।

  • l या L लगाने पर Constant का Type long double होता है।

Examples:

3.14
3.14f
3.14F
3.14L

3. Character Constants

A character constant is a single character or an escape sequence enclosed in single quotation marks. In C language, an ordinary character constant has type int.

Character Constant एक Single Character या Escape Sequence होता है जिसे Single Quotation Marks (' ') के बीच लिखा जाता है। C Language में सामान्य Character Constant का Type int होता है।

Examples:

'A'
'B'
'5'
'#'
'\n'
'\t'

Here, 'A' represents the character A, '5' represents the character digit 5, '\n' represents a newline and '\t' represents a horizontal tab.

यहाँ 'A' Character A को, '5' Digit 5 के Character को, '\n' Newline को तथा '\t' Horizontal Tab को दर्शाता है।

Character constants are represented using the character encoding supported by the C implementation. In ASCII-based systems, their corresponding ASCII values are commonly used.

Character Constants को C Implementation द्वारा Supported Character Encoding के अनुसार Represent किया जाता है। ASCII-Based Systems में इनके Corresponding ASCII Values का उपयोग सामान्यतः किया जाता है।

Example:

const char grade = 'A';
const char ch = '1';
const char symbol = '#';

4. String Literals

A string literal is a sequence of characters enclosed in double quotation marks. In C language, a string literal is stored as an array of characters with an additional null character '\0' at the end.

String Literal Characters का एक समूह होता है जिसे Double Quotation Marks (" ") के बीच लिखा जाता है। C Language में String Literal को Characters के Array के रूप में Store किया जाता है तथा इसके अंत में एक अतिरिक्त Null Character '\0' होता है।

Examples:

"India"
"Hello World"
"C Programming"
"12345"

A string can be stored in a character array. The %s format specifier is used with functions such as printf() to display a string.

String को Character Array में Store किया जा सकता है। printf() जैसे Functions में String को Display करने के लिए %s Format Specifier का उपयोग किया जाता है।

Example:

const char country[] = "India is a great country.";

In this example, country is a character array initialized with a string. The array includes the terminating null character.

इस उदाहरण में country एक Character Array है जिसे String से Initialize किया गया है। इस Array में अंत का Null Character भी शामिल होता है।

Difference Between 5, '5' and "5"

The values 5, '5' and "5" look similar, but they represent different types of constants.

5, '5' तथा "5" देखने में समान लगते हैं, लेकिन ये अलग-अलग प्रकार के Constants को दर्शाते हैं।

ExpressionTypeMeaning
5Integer constantNumeric value five
'5'Character constantCharacter digit five
"5"String literalString containing the character five and '\0'

For example:

int a = 5;
char b = '5';
const char c[] = "5";

Here, a stores the integer value 5, b stores the character digit 5, and c stores a string containing the character 5 followed by the null character.

यहाँ a में Integer Value 5, b में Character Digit 5 तथा c में Character 5 के बाद Null Character वाली String Store होती है।

Defining Constants Using const Keyword

The const keyword is used to declare an object whose value cannot be modified through that identifier after initialization.

const Keyword का उपयोग ऐसे Object को Declare करने के लिए किया जाता है जिसकी Value को Initialization के बाद उस Identifier के माध्यम से Modify नहीं किया जा सकता है।

Syntax

const data_type constant_name = value;

Examples

const int MAX = 100;
const float PI = 3.14f;
const char GRADE = 'A';

In the above example, MAX, PI and GRADE are const-qualified variables. Their values cannot be changed through these identifiers.

उपरोक्त उदाहरण में MAX, PI तथा GRADE Const-Qualified Variables हैं। इनके मानों को इन Identifiers के माध्यम से बदला नहीं जा सकता है।

Important: In C language, a const variable is not necessarily a compile-time constant expression. This is an important difference between C and C++.

महत्वपूर्ण: C Language में const Variable का उपयोग हमेशा Compile-Time Constant Expression के रूप में नहीं किया जा सकता है। यह C तथा C++ के बीच एक महत्वपूर्ण अंतर है।

Defining Constants Using #define Directive

The #define preprocessor directive is used to define symbolic constants and macros. It replaces the defined name with the specified replacement text before compilation.

#define Preprocessor Directive का उपयोग Symbolic Constants तथा Macros को Define करने के लिए किया जाता है। Compilation से पहले Preprocessor Defined Name को दिए गए Replacement Text से बदल देता है।

Syntax

#define constant_name value

Examples

#define MAX 100
#define PI 3.14
#define COUNTRY "India"

In the above example, MAX, PI and COUNTRY are macro names that are replaced by their respective values during preprocessing.

उपरोक्त उदाहरण में MAX, PI तथा COUNTRY Macro Names हैं जिन्हें Preprocessing के दौरान उनके निर्धारित मानों से Replace किया जाता है।

Unlike a const object, a macro does not create a variable or reserve memory for a separate object.

const Object के विपरीत Macro किसी Variable का निर्माण नहीं करता तथा इसके लिए अलग से Memory Reserve नहीं होती है।

Example Program Using Constants in C

The following program demonstrates the use of integer, floating-point and character constants, along with a const variable.

निम्नलिखित प्रोग्राम में Integer, Floating-Point तथा Character Constants के साथ const Variable का उपयोग समझाया गया है।

#include <stdio.h>
#define MAX_MARKS 100
int main(void)
{
const float PI = 3.14f;
int marks = 85;
char grade = 'A';
printf("Maximum Marks = %d\n", MAX_MARKS);
printf("PI = %.2f\n", PI);
printf("Marks = %d\n", marks);
printf("Grade = %c\n", grade);
return 0;
}

Output

Maximum Marks = 100
PI = 3.14
Marks = 85
Grade = A

In this program, 100, 3.14f, 85 and 'A' are literals. PI is a const-qualified variable, and MAX_MARKS is a macro defined using the #define directive.

इस प्रोग्राम में 100, 3.14f, 85 तथा 'A' Literals हैं। PI एक Const-Qualified Variable है तथा MAX_MARKS को #define Directive की सहायता से Define किया गया Macro है।

Important Points about Constants in C

  • Constants represent fixed values.

  • Literals are values written directly in source code.

  • Integer constants can be decimal, octal or hexadecimal.

  • Floating-point constants represent real numeric values.

  • Character constants are written inside single quotation marks.

  • String literals are written inside double quotation marks.

  • A string literal is terminated by the null character '\0'.

  • The const keyword declares objects that cannot be modified through the declared identifier.

  • The #define directive creates macros through preprocessor replacement.

  • 5, '5' and "5" represent different types of values.

  • Constants निश्चित मानों को दर्शाते हैं।

  • Literals वे Values हैं जिन्हें Source Code में सीधे लिखा जाता है।

  • Integer Constants को Decimal, Octal या Hexadecimal रूप में लिखा जा सकता है।

  • Floating-Point Constants वास्तविक संख्यात्मक मानों को दर्शाते हैं।

  • Character Constants को Single Quotation Marks में लिखा जाता है।

  • String Literals को Double Quotation Marks में लिखा जाता है।

  • String Literal के अंत में Null Character '\0' होता है।

  • const Keyword ऐसे Objects को Declare करता है जिन्हें निर्धारित Identifier के माध्यम से Modify नहीं किया जा सकता है।

  • #define Directive Preprocessor Replacement के माध्यम से Macros बनाता है।

  • 5, '5' तथा "5" अलग-अलग प्रकार के मानों को दर्शाते हैं।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025