Nano Technology

Data Types and Variables in C


1. Introduction to Data Types in C

In C programming, data types specify the type of data that a variable can store. They tell the compiler how much memory to allocate for a variable and what kind of operations can be performed on it.

Choosing the correct data type is important for efficient memory usage and correct program execution.

Dennis Ritchie

2. What are Variables in C?

A variable is a named memory location used to store data values during program execution. The value stored in a variable can change while the program is running.

int count;
float temperature;

Here, count and temperature are variables of type int and float.

3. Classification of Data Types in C

C data types are broadly classified into the following categories:

  • Basic (Primary) Data Types
  • Derived Data Types
  • User-Defined Data Types

4. Basic (Primary) Data Types

Basic data types are the fundamental data types provided by C.

Data Type Description Size (Typical)
int Stores whole numbers 2 or 4 bytes
float Stores decimal numbers 4 bytes
double Stores double-precision decimal numbers 8 bytes
char Stores a single character 1 byte

5. Derived Data Types

Derived data types are formed using basic data types. They allow programmers to store multiple values or complex data.

  • Arrays
  • Pointers
  • Functions
int marks[5];     // Array
int *ptr;         // Pointer

6. User-Defined Data Types

User-defined data types allow programmers to create their own data types according to program requirements.

  • struct
  • union
  • enum
  • typedef
struct student {
    int roll;
    float marks;
};

7. Declaration of Variables

Variable declaration tells the compiler the name and type of the variable.

int age;
float salary;
char grade;

A variable must be declared before it is used in a program.

8. Initialization of Variables

Initialization means assigning an initial value to a variable at the time of declaration.

int age = 20;
float pi = 3.14;
char ch = 'A';

Proper initialization helps avoid unexpected results during program execution.

9. Rules for Naming Variables

  • Variable names must begin with a letter or underscore
  • They can contain letters, digits, and underscores
  • They should not be C keywords
  • They are case-sensitive

Examples of valid variable names: total, _count, sum1
Invalid variable names: 1sum, float, total marks

10. Size and Range of Data Types

The size and range of data types depend on the compiler and system architecture. However, approximate values are:

  • int: −32,768 to 32,767 (2 bytes)
  • char: −128 to 127 or 0 to 255
  • float: 6 decimal precision
  • double: 15 decimal precision

11. Common Errors Related to Data Types and Variables

  • Using undeclared variables
  • Assigning wrong data types
  • Overflow due to insufficient data type size
  • Uninitialized variables

12. Summary

  • Data types define the type and size of data stored in variables.
  • Variables are memory locations used to store values.
  • C supports basic, derived, and user-defined data types.
  • Proper declaration and initialization are essential.
Note: A strong understanding of data types and variables is essential before learning operators, control statements, arrays, and pointers.