Data Types & Variables in C

Introduction

In C programming, data types define the type of data a variable can hold, such as integers, floating numbers, or characters. Variables are named memory locations used to store data that can be manipulated during program execution. Understanding data types and variables is essential for writing efficient, error-free programs.


Key Points

1. Data Types

  • int → Stores integer numbers (e.g., 5, -10)
  • float → Stores decimal numbers (e.g., 3.14, -0.5)
  • double → Stores double-precision decimal numbers
  • char → Stores single characters (e.g., ‘A’, ‘b’)
  • void → Represents no data, often used in functions that do not return a value

2. Variables

  • Named memory locations for storing data
  • Must follow rules:
    • Start with a letter or underscore
    • Cannot contain spaces or special characters
    • Case-sensitive (Varvar)
  • Declared before use: int age; float salary; char grade;

3. Constants

  • Fixed values that do not change during program execution
  • Can be declared using:
    • const keyword → const int MAX = 100;
    • #define preprocessor → #define PI 3.14

4. Type Conversion

  • Automatic (implicit) conversion occurs when assigning smaller data type to larger one
  • Manual (explicit) conversion uses type casting: float f = (float)10 / 3;

5. Importance

  • Helps in memory optimization
  • Prevents errors due to incompatible data operations
  • Fundamental for writing correct C programs

Example Code

#include <stdio.h>
int main() {
    int age = 20;
    float salary = 45000.50;
    char grade = 'A';
    printf("Age: %d, Salary: %.2f, Grade: %c", age, salary, grade);
    return 0;
}
Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *