C PROGRAMMING • Beginner

C Variables

A variable is a named memory location that stores data during program execution. In C, every variable must be declared with a type before use — C is a statically-typed language. This makes C programs efficient because memory is allocated at compile time.

Declaring a Variable

// Syntax: data_type variable_name;
int age;
float price;
char grade;
double salary;

Initializing a Variable

// Declaration + initialization
int age = 22;
float price = 99.99;
char grade = 'A';
double salary = 45000.50;

// Multiple declarations
int a = 10, b = 20, c = 30;

Without Initialization

int x;              // Uninitialized
printf("%d", x);    // ⚠ Undefined behavior — garbage value

int y = 0;          // Always initialize to avoid bugs
⚠ Common Mistake

Using an uninitialized variable in C causes undefined behavior — it may print a random garbage value or crash. Always initialize variables, especially in C.

The 4 Basic Types in C

1. int — Integers

int age = 22;
int temperature = -5;
int marks = 500;

printf("%d\n", age);       // 22

2. float — Decimal Numbers (small)

float price = 99.99f;
float temp = 36.6f;

printf("%.2f\n", price);   // 99.99

3. double — Decimal Numbers (large)

double pi = 3.141592653589793;
double distance = 1234567.89;

printf("%.10f\n", pi);     // 3.1415926536

4. char — Single Character

char grade = 'A';
char symbol = '$';

printf("%c\n", grade);     // A
printf("%d\n", grade);     // 65 (ASCII code)

Format Specifiers

Used with printf and scanf:

Data Type       Format Specifier
---------       ----------------
int             %d
float           %f
double          %lf
char            %c
string          %s
long            %ld
unsigned int    %u

Variable Naming Rules

  1. Can contain letters, digits, and underscores.
  2. Must begin with a letter or underscore (not a digit).
  3. Case-sensitive — age ≠ Age.
  4. Cannot use C keywords — int, return, if, etc.
  5. Convention: use snake_case — student_name, total_marks.
  6. Constants use UPPER_SNAKE_CASE — MAX_SIZE.

Constants — const keyword

Values that cannot be changed after initialization.

const double PI = 3.14159;
const int MAX = 100;

// PI = 3.14;   // ❌ Compile error

Scope of Variables

Local Variables

Declared inside a function or block. Accessible only within that block.

void demo() {
    int x = 10;    // local
    printf("%d", x);
}
// printf("%d", x);   // ❌ Error — x not accessible here

Global Variables

Declared outside all functions. Accessible from any function.

int counter = 0;   // global

void increment() {
    counter++;
}

int main() {
    increment();
    printf("%d", counter);   // 1
    return 0;
}

Static Variables

Retains value between function calls.

void counter() {
    static int count = 0;   // initialized once
    count++;
    printf("Called %d times\n", count);
}

int main() {
    counter();   // Called 1 times
    counter();   // Called 2 times
    counter();   // Called 3 times
    return 0;
}

Type Conversion

Implicit (Automatic)

int x = 10;
float y = x;    // int → float (safe)
printf("%f", y);   // 10.000000

Explicit (Type Casting)

float pi = 3.99;
int x = (int) pi;    // 3 (decimal truncated)
printf("%d", x);

Real-World Example — Student Record

#include <stdio.h>

int main() {
    // Global info
    const char* academy = "AI Developers Academy";
    int total_students = 50;

    // Student data
    char name[] = "Ankit";
    int age = 22;
    float percentage = 89.5;
    char grade = 'A';

    printf("Academy: %s\n", academy);
    printf("Total Students: %d\n", total_students);
    printf("---\n");
    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Percentage: %.1f%%\n", percentage);
    printf("Grade: %c\n", grade);

    return 0;
}

Output

Academy: AI Developers Academy
Total Students: 50
---
Name: Ankit
Age: 22
Percentage: 89.5%
Grade: A
💡 Academy Tip

In C, use %f for float and %lf for double when using scanf. But printf treats both as %f — this is a common gotcha.

📝 Try Yourself

Write a C program that declares: an int (your age), a float (your height in meters), a char (your grade), and a string (your name). Print them all with proper format specifiers. Then try converting a float to int and print the truncated value.

Quick Revision

  • Variables are named memory locations.
  • C requires type declaration before use.
  • Basic types: int, float, double, char.
  • Format specifiers: %d, %f, %lf, %c, %s.
  • Use const for constants.
  • Local — inside function; global — everywhere; static — retains value.
  • Uninitialized local variables cause undefined behavior.
  • snake_case for variables, UPPER_CASE for constants.

Interview Questions

Q: What is the difference between float and double?

A: float is 4 bytes with ~6-7 decimal digits precision. double is 8 bytes with ~15-16 digits precision. Use double when accuracy matters.

Q: Difference between local and global variables?

A: Local variables are declared inside a function, accessible only there, and go out of scope when the function ends. Global variables are declared outside all functions, accessible everywhere, and exist for the program's lifetime.

Q: What is a static variable in C?

A: A variable declared with static inside a function retains its value between function calls. It's initialized only once. Static global variables are accessible only within the same file.

Q: What happens if you don't initialize a variable in C?

A: Local uninitialized variables contain garbage values (whatever was in memory). Using them causes undefined behavior. Global and static variables are automatically initialized to 0.

Q: Why is sizeof operator useful?

A: sizeof returns the size in bytes of a variable or type. Useful for memory allocation and portability. Example: sizeof(int) is typically 4 on most modern systems.

Want to Master C Programming?

Join our C Programming course in Varanasi — practical classes and career support at our Sarnath centre.

Ask on WhatsApp
Chat with us