C Programming - Beginner to Brilliance / Basics of C Programming

Variables & Data Types in C

๐Ÿ“ฆ What is a Variable?

A variable is a named storage location in memory that holds a value which your program can read or change during its execution.

๐Ÿ  Real-life Analogy:

Imagine you have labeled containers in your kitchen:

  • One labeled โ€œSugarโ€ with 2kg inside

  • One labeled โ€œSaltโ€ with 1kg

In C:

  • The label is the variable name (e.g., sugar)

  • The container is the memory space

  • The content is the value (e.g., 2)


โœ๏ธ Declaring a Variable

To use a variable in C, you need to declare it like this:

data_type variable_name;

You can also initialize (assign a value) at the same time:

int age = 25;

๐Ÿงพ Examples with Explanation:

int marks = 85;        // 'marks' is an integer variable storing 85
float weight = 67.8;   // 'weight' is a float variable storing 67.8
char grade = 'A';      // 'grade' is a character variable storing 'A'

Each line:

  • Creates a box in memory

  • Labels it with the variable name

  • Stores a value inside it


๐Ÿงฌ What is a Data Type?

A data type tells the computer what kind of data is being stored in a variable โ€” like whether itโ€™s:

  • A whole number?

  • A decimal?

  • A character?

Just like we use different containers for water, grains, or milk, C uses different data types to store different kinds of information.

๐Ÿงพ Format Specifiers

In C, format specifiers are used in functions like printf() and scanf() to tell the compiler what type of data you're working with.


๐Ÿ“Œ Common Format Specifiers:

Specifier

Data Type

Example Value

Example printf()

%d

int

10

printf("Value = %d", 10);

%f

float/double

3.14

printf("Pi = %f", 3.14);

%.2f

float with 2 decimals

3.1415

printf("Pi = %.2f", 3.1415);

%c

char

'A'

printf("Char = %c", 'A');

%s

string

"Hello"

printf("Text = %s", "Hello");

%u

unsigned int

15

printf("Unsigned = %u", 15);

%ld

long int

100000L

printf("Long = %ld", 100000L);

%lf

double

3.1415

printf("Double = %lf", 3.1415);

%x / %X

Hexadecimal

255

printf("Hex = %x", 255); โ†’ ff

%p

pointer address

&x

printf("Address = %p", &x);


๐Ÿ’ก Example with multiple specifiers:

int age = 25;
float height = 5.9;
char grade = 'A';
printf("Age: %d, Height: %.1f, Grade: %c", age, height, grade);
Age: 25, Height: 5.9, Grade: A

โš ๏ธ Notes:

  • Use correct specifier for correct data type โ€” mismatches can cause wrong output or crash.

  • %f is used for both float and double in printf().

  • Always match order of arguments with the format string.


๐ŸŽ“ Types of Data Types in C

๐Ÿ”น Basic (Primary) Data Types

These are the most commonly used types.

Data Type

Description

Size (Typical*)

Format Specifier

int

Integer (whole number)

2 or 4 bytes

%d

float

Decimal (single precision)

4 bytes

%f

double

Decimal (double precision)

8 bytes

%lf

char

Single character

1 byte

%c

void

No value

0 bytes

โ€”

๐Ÿ“Œ Note: Size may vary based on the system (32-bit vs 64-bit)


๐Ÿ”น Derived Data Types

These are made using basic types:

  • Arrays

  • Pointers

  • Functions

  • Structures


๐Ÿ”น Enumeration (enum)

An enum is a user-defined data type in C that consists of named integer constants. It is used to assign names to a set of integral values, making code easier to read and maintain.

๐Ÿง  Syntax:

enum enum_name {
    VALUE1,
    VALUE2,
    VALUE3,
    ...
}

By default, VALUE1 = 0, VALUE2 = 1, and so on. You can also manually assign values.


๐Ÿ’ก Example 1 โ€“ Default Values:

#include <stdio.h>

enum Weekday {
    MONDAY,   // 0
    TUESDAY,  // 1
    WEDNESDAY,// 2
    THURSDAY, // 3
    FRIDAY    // 4
};

int main() {
    enum Weekday today = WEDNESDAY;
    printf("Day number: %d\\n", today);  // Output: 2
    return 0;
}

๐Ÿ’ก Example 2 โ€“ Custom Values:

enum Status {
    SUCCESS = 1,
    FAILURE = 0,
    PENDING = -1
};

You can now use SUCCESS, FAILURE, PENDING as named constants in your program.


๐Ÿ”„ Behind the Scenes:

An enum is essentially an int, so you can:

  • Assign enum values to int

  • Use in switch statements

  • Compare using logical operators


๐Ÿ“ฅ Memory Size of Data Types

You can check memory size using the sizeof() function:

#include <stdio.h>

int main() {
    printf("Size of int: %lu bytes\\n", sizeof(int));
    printf("Size of float: %lu bytes\\n", sizeof(float));
    printf("Size of double: %lu bytes\\n", sizeof(double));
    printf("Size of char: %lu bytes\\n", sizeof(char));
    return 0;
}

๐Ÿ–จ๏ธ Sample Output (on 64-bit system):

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

๐Ÿ“‹ Integer Types (Variants of int)

C allows various integer types depending on memory and range:

Type

Range (approximate)

Size

short int

-32,768 to 32,767

2 bytes

unsigned int

0 to 4,294,967,295

4 bytes

long int

-2.1 billion to 2.1 billion

4/8 bytes

long long int

-9 quintillion to +9 quintillion

8 bytes

๐Ÿงช Example:

unsigned int age = 30;  // Cannot store negative values

๐Ÿ”ก Character Type (char)

  • A char stores a single character in single quotes like 'A'.

  • It is internally stored as an integer (ASCII value).

Example:

char grade = 'A';       // 'A' = 65 in ASCII
printf("%d", grade);    // Output: 65

๐Ÿงฎ Floating-Point Types (float, double)

Used for storing decimal values.

float pi = 3.14;
double price = 99.999999;

Type

Precision

Size

float

6 digits

4 bytes

double

15 digits

8 bytes


๐Ÿงช Mixing Data Types

If you mix types in operations, type promotion happens automatically:

int a = 5;
float b = 2.0;

float result = a + b;  // a is promoted to float

โš ๏ธ Common Mistakes

Mistake

Correction

Declaring without data type

โŒ num; โ†’ โœ… int num;

Wrong type assignment

โŒ char ch = "A"; โ†’ โœ… char ch = 'A';

Using a variable without declaring it

โŒ x = 10; before int x;