⚡0 XP🔥0

Variables, Data Types and Constants

Lesson 2 of 13 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs C

Declare variables, choose the right data type (int, float, double, char), use sizeof, format specifiers, constants and type casting.

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
▶ Try it YourselfC
Ctrl + Enter to run
Output
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, Marks and MARKS are 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:

▶ Try it YourselfC
Ctrl + Enter to run
Output
char      : 1 byte
int       : 4 bytes
float     : 4 bytes
double    : 8 bytes
long long : 8 bytes
Note

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.

▶ Try it YourselfC
Ctrl + Enter to run
Output
A has ASCII code 65
Next letter: B
Lowercase: a

float vs double - precision#

▶ Try it YourselfC
Ctrl + Enter to run
Output
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
▶ Try it YourselfC
Ctrl + Enter to run
Output
Price: 1499.00
GST (18%): 269.82
Total: 1768.82
Tip

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:

▶ Try it YourselfC
Ctrl + Enter to run
Output
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 / int is integer division - cast or use a decimal literal to get decimals.
  • Use const or #define for values that never change.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Student details

Create variables for a student and print them exactly like this:

Roll no: 42
Grade: A
Percentage: 87.50

Use an int, a char and a float (printed with 2 decimal places using %.2f).

Expected output:
Roll no: 42
Grade: A
Percentage: 87.50
2

Average of three numbers

The marks are 78, 85 and 90 (integers). Print their average with decimals: Average = 84.33. Watch out - (78 + 85 + 90) / 3 gives an integer! Use a cast or divide by 3.0.

Expected output:
Average = 84.33

Quick Quiz

Q1.

Which format specifier prints a float or double with printf?

Q2.

What is printed by printf("%d", 7 / 2);?

Q3.

Which variable name is invalid in C?

Q4.

How many bytes does a char take?

Q5.

What does const float PI = 3.14f; mean?