Chapter 3: Variables, Data Types, and Constants

1. Basic Data Types in C

C has a few basic (primitive) data types. Here are the most important ones beginners use:

Data Type What it stores Typical Size (bytes) Range (approximate) Example Value
int Whole numbers (no decimal) 4 bytes -2,147,483,648 to +2,147,483,647 25, -100, 0
float Decimal numbers (floating point) 4 bytes About 6 decimal places of precision 3.14, 99.99, -0.5
double More accurate decimal numbers 8 bytes About 15 decimal places of precision 3.141592653589793
char Single character 1 byte -128 to +127 (ASCII) ‘A’, ‘5’, ‘$’

Quick Tip:

  • Use int for ages, counts, scores
  • Use float or double for money, temperature, distance
  • Use double when you need more accuracy (most people use double instead of float)

2. Declaring and Initializing Variables

Declaration = Telling the computer: “I need a box of this type.” Initialization = Putting a value in the box right away.

Syntax:

C

Rules for Variable Names (Identifiers):

  • Start with letter or underscore (_)
  • Can contain letters, digits, underscore
  • Case-sensitive (Age and age are different)
  • Cannot be a keyword (like int, if, for, etc.)

Examples:

C

3. Format Specifiers (How to Print Variables)

When you use printf(), you need to tell it what type of variable you are printing. These are called format specifiers.

Data Type Format Specifier Example in printf
int %d or %i printf(“Age: %d\n”, age);
float %f printf(“Price: %f\n”, price);
double %lf or %f printf(“Pi: %.10lf\n”, pi);
char %c printf(“Grade: %c\n”, grade);

Real Example – Printing Variables

C

Output:

text

4. Constants and Literals

Constants = Values that cannot be changed after they are set. There are two main ways to make constants:

A. Using const keyword (modern way – recommended)

C

If you try to change it → compiler error!

B. Using #define (old style – preprocessor)

C

No semicolon at the end!

Literals = The actual values you write directly in code Examples of literals:

  • Integer literal: 25, -100
  • Floating-point literal: 3.14, 99.99f (f = float)
  • Character literal: ‘A’, ‘5’, ‘$’
  • String literal: “Hello World”

Example with Constants

C

5. Keywords and Identifiers

Keywords = Special reserved words in C (you cannot use them as variable names)

There are 32 keywords in standard C (some common ones):

  • int, float, double, char
  • if, else, for, while, return
  • const, void, switch, case, break
  • static, extern, typedef, etc.

Identifiers = Names you give to variables, functions, etc.

  • Examples: age, total_marks, myName, _count
  • Cannot be keywords

Full Practical Example – Putting Everything Together

C

Output:

text

Today’s Homework

  1. Copy the last example and run it.
  2. Change values (your own name, marks, roll number) and run again.
  3. Create a new program that stores:
    • Your age (int)
    • Your height in cm (float)
    • Your favorite letter (char)
    • A constant for your city name (using #define or const)
    • Print everything nicely using printf.
  4. Try to use a keyword as variable name (like int int = 10;) – see the error!

You may also like...