Variables and Data Types in C

What is a Variable?

A variable is a name given to a memory location that stores a value. This value can change during the execution of a program.

Syntax:

datatype variableName = value;

Example:

int age = 20;
float height = 5.8;
char grade = 'A';

Rules for Naming Variables

What are Data Types?

Data types tell the compiler what kind of data a variable can hold. C has several types:

1. int (Integer)

Stores whole numbers without decimals.

int num = 25;

2. float (Floating Point)

Stores decimal numbers (single precision).

float temp = 36.6;

3. double (Double Precision)

Stores larger decimal numbers with more precision than float.

double distance = 1234.56789;

4. char (Character)

Stores a single character enclosed in single quotes.

char letter = 'B';

5. void (Empty Type)

Represents no value. Commonly used in functions to indicate no return value.

void printHello() {
  printf("Hello");
}

6. short and long (Integer Variants)

Used to store smaller or larger integers.

short a = 100;
long b = 1000000;

Complete Example

#include <stdio.h>

int main() {
    int age = 20;
    float marks = 87.5;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Marks: %.1f\n", marks);
    printf("Grade: %c\n", grade);

    return 0;
}
Variables and Data Types in C

Variables and Data Types in C

What is a Variable?

A variable is a name given to a memory location that stores a value. This value can change during the execution of a program.

Syntax:

datatype variableName = value;

Example:

int age = 20;
float height = 5.8;
char grade = 'A';

Rules for Naming Variables

What are Data Types?

Data types tell the compiler what kind of data a variable can hold. C has several types:

1. int (Integer)

Stores whole numbers without decimals.

int num = 25;

2. float (Floating Point)

Stores decimal numbers (single precision).

float temp = 36.6;

3. double (Double Precision)

Stores larger decimal numbers with more precision than float.

double distance = 1234.56789;

4. char (Character)

Stores a single character enclosed in single quotes.

char letter = 'B';

5. void (Empty Type)

Represents no value. Commonly used in functions to indicate no return value.

void printHello() {
  printf("Hello");
}

6. short and long (Integer Variants)

Used to store smaller or larger integers.

short a = 100;
long b = 1000000;

Complete Example

#include <stdio.h>

int main() {
    int age = 20;
    float marks = 87.5;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Marks: %.1f\n", marks);
    printf("Grade: %c\n", grade);

    return 0;
}