Variables in C Language – Declaration, Initialization and Naming Rules

What is a Variable in C Language?

A variable is a named memory location used to store data in a C program. The value stored in a variable can be changed during program execution. Every variable has a name and a data type that determines the kind of value it can store. The name of a variable is called an identifier. The data type of a variable determines the type of data it can store and the amount of memory required by it. However, the actual memory size depends on the compiler and the computer system. For example, an integer variable named i can store an integer value such as 10, -25 or 100. The size of an integer variable is not necessarily 2 bytes on every computer.

Variable C Language में एक Named Memory Location होता है जिसका उपयोग Program में Data Store करने के लिए किया जाता है। Variable में Store किए गए मान को Program Execution के दौरान बदला जा सकता है। प्रत्येक Variable का एक नाम तथा Data Type होता है जो यह निर्धारित करता है कि उसमें किस प्रकार का मान Store किया जा सकता है। Variable के नाम को Identifier कहा जाता है। Variable का Data Type यह निर्धारित करता है कि उसमें किस प्रकार का Data Store किया जा सकता है तथा उसके लिए कितनी Memory की आवश्यकता होगी। हालाँकि, Memory का वास्तविक आकार Compiler तथा Computer System पर निर्भर करता है। उदाहरण के लिए, i नाम का Integer Variable 10, -25 या 100 जैसे पूर्णांक मान Store कर सकता है। प्रत्येक Computer में Integer Variable का आकार 2 Bytes होना आवश्यक नहीं है।

Rules for Naming Variables in C Language

The following rules should be followed while creating variable names in C language:

C Language में Variable का नाम निर्धारित करते समय निम्नलिखित नियमों का पालन करना चाहिए:

  1. A variable name can contain English letters, digits and underscores (_).

    Variable के नाम में English Letters, Digits तथा Underscore (_) का प्रयोग किया जा सकता है।

  2. A variable name must begin with a letter or an underscore. It cannot begin with a digit. However, beginners should preferably start variable names with a letter.

    Variable के नाम की शुरुआत Letter या Underscore से हो सकती है। इसकी शुरुआत Digit से नहीं हो सकती। हालाँकि, Beginners को Variable का नाम Letter से शुरू करना चाहिए।

  3. A variable name cannot contain blank spaces or special symbols such as @, #, % and -.

    Variable के नाम में Blank Space या @, #, % तथा - जैसे Special Symbols का प्रयोग नहीं किया जा सकता।

  4. A variable name cannot be a keyword of C language.

    Variable का नाम C Language के किसी Keyword के समान नहीं होना चाहिए।

  5. Variable names are case-sensitive. For example, total, Total and TOTAL are treated as different identifiers.

    Variable के नाम Case-Sensitive होते हैं। उदाहरण के लिए total, Total तथा TOTAL को अलग-अलग Identifiers माना जाता है।

  6. Variable names should be meaningful and related to the data they represent.

    Variable के नाम Meaningful होने चाहिए तथा जिस Data को वे Store करते हैं उससे संबंधित होने चाहिए।

  7. Modern C does not impose a general eight-character limit on variable names. However, compiler and implementation limits may apply.

    Modern C में Variable के नाम के लिए सामान्य रूप से आठ अक्षरों की सीमा निर्धारित नहीं है। हालाँकि, Compiler तथा Implementation के अनुसार कुछ सीमाएँ हो सकती हैं।

Examples of Valid and Invalid Variable Names

Valid variable namesInvalid variable names
age2age
student_namestudent name
marks1float
total_markstotal-marks
_counttotal@marks

Note: Although _count is a valid identifier in this example, names beginning with underscores are subject to additional reserved-name rules in C. Beginners should preferably avoid them.

ध्यान दें: इस उदाहरण में _count एक Valid Identifier है, लेकिन Underscore से शुरू होने वाले नामों के लिए C में कुछ अतिरिक्त Reserved-Name Rules होते हैं। Beginners को ऐसे नामों से बचना चाहिए।

Case Sensitivity in C Language

C language is a case-sensitive programming language. It means that uppercase and lowercase letters are treated differently.

C Language एक Case-Sensitive Programming Language है। इसका अर्थ है कि इसमें Capital Letters तथा Small Letters को अलग-अलग माना जाता है।

Example:

int si;
int SI;
int Si;
int sI;

All four variables are different because their names differ in letter case.

उपरोक्त चारों Variables अलग-अलग हैं क्योंकि इनके नामों में छोटे तथा बड़े अक्षरों का अंतर है।

Declaration of Variables in C Language

Variable declaration means specifying the data type and name of a variable before using it in a program. The data type tells the compiler what kind of data the variable is intended to store.

Variable Declaration का अर्थ है Program में Variable का उपयोग करने से पहले उसका Data Type तथा नाम निर्धारित करना। Data Type Compiler को बताता है कि Variable में किस प्रकार का Data Store किया जाना है।

Syntax

data_type variable_name;
data_type var1, var2, var3;

Examples

int a;
float p, r, t, si;
char ch;
double salary;

In the above example, a is an integer variable, p, r, t and si are floating-point variables, ch is a character variable and salary is a double-precision floating-point variable.

उपरोक्त उदाहरण में a एक Integer Variable है, p, r, t तथा si Floating-Point Variables हैं, ch एक Character Variable है तथा salary एक Double-Precision Floating-Point Variable है।

Initialization of Variables in C Language

Initialization means providing an initial value to a variable at the time of its definition.

Variable को Define करते समय उसे प्रारंभिक मान प्रदान करना Initialization कहलाता है।

Syntax

data_type variable_name = value;
data_type var1 = value1, var2 = value2;

Examples

int a = 100;
float p = 5000.0f, r = 2.5f, t = 4.0f;
char ch = 'A';
double salary = 25000.50;

In the above example, the variables are initialized with values at the time of their definition.

उपरोक्त उदाहरण में Variables को Define करते समय ही प्रारंभिक मान प्रदान किए गए हैं।

Example Program: Declaration and Initialization of Variables

The following program demonstrates variable declaration, initialization and displaying variable values.

निम्नलिखित प्रोग्राम में Variable Declaration, Initialization तथा Variable के मान को प्रदर्शित करने की प्रक्रिया समझाई गई है।

#include <stdio.h>
int main(void)
{
int age = 25;
float marks = 85.5f;
char grade = 'A';
printf("Age = %d\n", age);
printf("Marks = %.2f\n", marks);
printf("Grade = %c\n", grade);
return 0;
}

Output

Age = 25
Marks = 85.50
Grade = A

In this program, three variables named age, marks and grade are declared and initialized with values. The printf() function displays their values.

इस Program में age, marks तथा grade नाम के तीन Variables को Declare करके उनमें प्रारंभिक मान Store किए गए हैं। printf() Function की सहायता से इनके मान स्क्रीन पर प्रदर्शित किए जाते हैं।

Finding the Size of a Variable Using sizeof()

The sizeof operator is used to determine the memory size of a data type or variable in bytes. The actual size may vary depending on the compiler and computer system.

sizeof Operator का उपयोग किसी Data Type या Variable द्वारा उपयोग की जाने वाली Memory का आकार Bytes में ज्ञात करने के लिए किया जाता है। इसका वास्तविक आकार Compiler तथा Computer System के अनुसार अलग-अलग हो सकता है।

Example:

#include <stdio.h>
int main(void)
{
int i;
float f;
char ch;
double d;
printf("Size of int = %zu bytes\n", sizeof(i));
printf("Size of float = %zu bytes\n", sizeof(f));
printf("Size of char = %zu byte\n", sizeof(ch));
printf("Size of double = %zu bytes\n", sizeof(d));
return 0;
}

The output depends on the compiler and system. The sizeof(char) operator always returns 1 byte in C.

इस Program का Output Compiler तथा Computer System पर निर्भर करता है। C Language में sizeof(char) का मान हमेशा 1 Byte होता है।

Important Points about Variables

  • A variable is a named memory location used to store data.

  • Every variable has a data type and an identifier.

  • Variable names are case-sensitive.

  • A variable name cannot be a C keyword.

  • Variables should be initialized before their values are read.

  • The size of a variable depends on its data type and implementation.

  • The sizeof operator can be used to determine the size of a variable.

  • Variable and identifier are related terms, but they are not exactly the same.

  • Variable एक Named Memory Location होता है जिसका उपयोग Data Store करने के लिए किया जाता है।

  • प्रत्येक Variable का एक Data Type तथा Identifier होता है।

  • Variable के नाम Case-Sensitive होते हैं।

  • Variable का नाम C Language का Keyword नहीं हो सकता।

  • Variable की Value को Read करने से पहले उसे Initialize करना चाहिए।

  • Variable का आकार उसके Data Type तथा Implementation पर निर्भर करता है।

  • sizeof Operator की सहायता से Variable का आकार ज्ञात किया जा सकता है।

  • Variable तथा Identifier संबंधित शब्द हैं, लेकिन दोनों का अर्थ बिल्कुल समान नहीं है।

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