Loading...

Variables and Data Types

In C programming, variables are used to store data, and data types define the type of data a variable can hold.

Variables in C

A variable is a name given to a memory location that holds a value. It must be declared before use.

Variable Declaration

Variables are declared using a data type followed by the variable name. Example:

int age;
float salary;
char grade;

Variable Initialization

Variables can be initialized at the time of declaration.

int age = 25;
float salary = 50000.50;
char grade = 'A';

Data Types in C

Data types define the type and size of data stored in a variable. C provides the following data types:

1. Basic Data Types

  • int - Stores integers (e.g., 10, -5)
  • float - Stores decimal numbers (e.g., 3.14, -0.99)
  • double - Stores large floating-point numbers
  • char - Stores a single character (e.g., 'A', '9')

2. Derived Data Types

  • Array - Collection of similar data types
  • Pointer - Stores the address of a variable
  • Structure - Groups different data types

3. Enumeration Data Type

Enum is used to define a set of named integer constants.

enum Color {RED, GREEN, BLUE};

Example Program

The following program demonstrates the use of variables and data types:

#include <stdio.h>

int main() {
    int age = 25;
    float salary = 45000.50;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);

    return 0;
}

Explanation

  • int age = 25; - Declares an integer variable and initializes it.
  • float salary = 45000.50; - Declares a floating-point variable.
  • char grade = 'A'; - Declares a character variable.

If you have any questions, feel free to ask. Thank you for reading!

Thankyou!