What is a variable?#
A variable is a named box in memory that holds a value. In C you must declare a variable with its data type before using it - the type tells the compiler how much memory to reserve and how to interpret the bits.
int age; // declaration
age = 20; // assignment
int marks = 95; // declaration + initialisation
Age: 20 Height: 5.8 feet Initial: R
Variable एक डिब्बे जैसा है जिसका एक नाम होता है। C में डिब्बा बनाने से पहले बताना पड़ता है कि उसमें किस प्रकार का data रहेगा - पूरा नंबर (int), दशमलव वाला नंबर (float/double) या एक अक्षर (char)। इसे ही data type कहते हैं।
Rules for naming variables#
- Use letters, digits and underscore
_only. - The first character must be a letter or
_(not a digit). - No spaces, no keywords (
int,return,while...). - C is case-sensitive:
marks,MarksandMARKSare three different variables.
Good names: total_marks, studentCount, rate. Bad names: 2nd, my marks, float.
Basic data types#
| Type | Stores | Typical size | Format specifier | Example |
|---|---|---|---|---|
int |
whole numbers | 4 bytes | %d |
int n = -25; |
float |
decimals (~6-7 digits precision) | 4 bytes | %f |
float p = 3.14f; |
double |
decimals (~15 digits precision) | 8 bytes | %lf (scanf), %f (printf) |
double g = 9.80665; |
char |
one character | 1 byte | %c |
char c = 'A'; |
long long |
big whole numbers | 8 bytes | %lld |
long long pop = 1400000000LL; |
unsigned int |
non-negative whole numbers | 4 bytes | %u |
unsigned int u = 40000u; |
Use sizeof to see how many bytes a type takes on the current system:
char : 1 byte int : 4 bytes float : 4 bytes double : 8 bytes long long : 8 bytes
sizeof returns a value of type size_t, which is printed with %zu.
char is really a small number#
A char stores the ASCII code of a character. 'A' is 65, 'a' is 97, '0' is 48.
A has ASCII code 65 Next letter: B Lowercase: a
float vs double - precision#
float : 0.333333343267441 double: 0.333333333333333
A float is accurate to about 7 digits, a double to about 15. Prefer double for scientific or money calculations; use float when memory matters (graphics, embedded).
Constants#
A value that must not change should be a constant. There are two ways:
#define MAX_STUDENTS 60 // macro: replaced by the preprocessor, no type
const double PI = 3.14159; // typed, read-only variable
Price: 1499.00 GST (18%): 269.82 Total: 1768.82
To print a literal % sign with printf, write %%.
Type conversion (casting)#
When you mix types, C converts automatically (implicit conversion) - usually to the "bigger" type. Integer-only arithmetic stays integer, which surprises beginners:
Integer division: 3 Wrong average : 3.000000 Right average : 3.500000 Also right : 3.50
(float)total is an explicit cast: it converts total to float before the division. In the "wrong" line the division already happened in integers, so the fraction was lost before the cast.
दो int को भाग देने पर C दशमलव वाला हिस्सा फेंक देता है: 7 / 2 = 3। सही जवाब (3.5) पाने के लिए भाग देने से पहले किसी एक number को float बना दीजिए - (float)total / count या total / 2.0।
Summary#
- Declare every variable with a type:
int,float,double,char... - Match the format specifier to the type:
%d,%f,%c,%lld,%zu. int / intis integer division - cast or use a decimal literal to get decimals.- Use
constor#definefor values that never change.