Variables और Data Types
Variable क्या है?
एक variable memory में एक named location है जो एक value store करती है। इसे एक labeled box की तरह सोचें — आप box को एक नाम देते हैं, specify करते हैं कि इसमें किस type की चीज़ रखी जा सकती है, और फिर उसमें value डालते हैं।
// Variable = type + naam + value int age = 20; // ↑ ↑ ↑ // type naam value
Declaration और Initialization
Declaration – Memory Reserve करना
int age; // Declare — अभी value नहीं दी float price; // float के लिए memory reserve char grade; // character के लिए memory reserve
Initialization – Value देना
int age = 20; // Declare + Initialize एक साथ float price = 99.50f; // Float value char grade = 'A'; // Character (single quotes!)
बिना initialize किए variable use करने पर garbage value मिलती है। हमेशा variable को use करने से पहले initialize करें!
C के Data Types
1. int – Integer (पूर्ण संख्या)
Whole numbers store करता है (कोई decimal नहीं)। Size: आमतौर पर 4 bytes।
int marks = 95; int temperature = -5; // Negative integers भी हो सकते हैं int year = 2025;
2. float – Decimal Number (Single Precision)
Decimal numbers store करता है। Size: 4 bytes। Precision: ~6-7 decimal digits।
3. double – Decimal Number (Double Precision)
float से ज़्यादा precise। Size: 8 bytes। जब high accuracy चाहिए तो double use करें।
4. char – Single Character
एक single character store करता है। Size: 1 byte। Character को single quotes में लिखते हैं।
Sizes और Ranges
| Data Type | Size | Range | Format Specifier |
|---|---|---|---|
char | 1 byte | -128 से 127 | %c |
int | 4 bytes | -2 अरब से +2 अरब | %d |
float | 4 bytes | ±3.4×10³⁸ (6-7 digits) | %f |
double | 8 bytes | ±1.7×10³⁰⁸ (15-16 digits) | %lf |
Complete Programs
#include <stdio.h> int main() { int rollNo = 101; float percentage = 89.5f; char grade = 'A'; double fees = 45000.75; printf("Roll No: %d\n", rollNo); printf("Percentage: %.1f%%\n", percentage); printf("Grade: %c\n", grade); printf("Fees: %.2lf\n", fees); return 0; }
Variable के Naming Rules
- Letter (a-z, A-Z) या underscore (_) से शुरू होना चाहिए
- Letters, digits (0-9), और underscores contain कर सकता है
- Digit से शुरू नहीं हो सकता:
2age❌ - C keywords use नहीं कर सकते:
int,float,for❌ - Case-sensitive है:
ageऔरAgeअलग-अलग variables हैं
सारांश
- Variable एक named memory location है जो value hold करती है
- C में variable declare करते समय हमेशा data type specify करें
- Main types:
int(पूर्ण संख्या),float/double(दशमलव),char(character) - Format specifiers:
%d(int),%f(float),%lf(double),%c(char) - Garbage value से बचने के लिए variables को हमेशा initialize करें
एक C program लिखें जो आपका नाम का initial (char), age (int), CGPA (float), और total fees (double) store करे और print करे।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C में variable क्या है?
int age;, जो एक integer रखने को memory आरक्षित करता है जिसे आप बाद में पढ़ या update कर सकते हैं।C में बुनियादी data types क्या हैं?
int, दशमलव संख्याओं के लिए float और double, और एकल characters के लिए char। C इनके size और range समायोजित करने को short, long, signed और unsigned जैसे modifiers भी देता है।C में float और double में क्या अंतर है?
double ज़्यादा memory (आमतौर पर 8 bytes) इस्तेमाल करता है और float (आमतौर पर 4 bytes) से लगभग दोगुनी सटीकता रखता है। ज़्यादा सटीक दशमलव चाहिए तो double, और memory बचाना ज़्यादा मायने रखे तो float इस्तेमाल करें।C में variable कैसे declare और initialise करते हैं?
int count = 0;। एक line में declare और initialise करना अच्छी आदत है क्योंकि uninitialised variable अप्रत्याशित garbage value रखता है।C में variables नाम रखने के नियम क्या हैं?
int या return जैसा C keyword नहीं हो सकता। नाम case-sensitive हैं, तो Age और age अलग variables हैं।