C - Type System in C

 

Definition

The type system in C defines the rules for declaring variables, functions, and expressions, specifying:

  • What kind of data can be stored

  • How much memory is allocated

  • What operations are permitted on that data


Data Types in C (Type System Classification)

1. Basic (Primitive) Data Types

Used to store simple values.

Data Type Description
int Integer values
float Single-precision decimal
double Double-precision decimal
char Single character

Example:

int a;
float b;
char c;

2. Derived Data Types

Derived from basic data types.

  • Arrays

  • Pointers

  • Structures

  • Unions

Example:

int arr[10];
int *p;

3. User-Defined Data Types

Created by programmers.

  • struct

  • union

  • enum

  • typedef

Example:

typedef int marks;

4. Void Type

  • Represents no value

  • Used for functions that do not return anything

Example:

void display();

Importance of Type System

  • Ensures type safety

  • Helps in error detection

  • Determines memory allocation

  • Improves code readability and reliability


Type Conversion in C

Definition

Type conversion is the process of converting a value from one data type to another.

C supports two types of type conversion:

  1. Implicit (Automatic)

  2. Explicit (Type Casting)


1. Implicit Type Conversion

  • Done automatically by the compiler

  • Occurs when different data types are used in an expression

  • Smaller data type is converted into larger data type

Example:

int a = 10;
float b = a;

Here, int is automatically converted to float.


Usual Type Promotion Order

char → int → float → double

Example:

int a = 5;
float b = 2.5;
float c = a + b;   // a converted to float

2. Explicit Type Conversion (Type Casting)

  • Done manually by the programmer

  • Used when forced conversion is required

Syntax:

(type) expression

Example:

int a = 10, b = 3;
float result = (float)a / b;

Without casting, result would be 3.


Type Conversion in Expressions

Example:

int a = 5;
double b = 2.0;
double c = a * b;

Here, a is converted to double.


Type Conversion in Assignment

Example:

float f = 10.75;
int x = f;

Fractional part is truncated → x = 10


Problems Due to Improper Type Conversion

  • Loss of precision

  • Unexpected results

  • Overflow or underflow

  • Logical errors


Difference Between Implicit and Explicit Conversion

Implicit Conversion Explicit Conversion
Automatic Manual
Done by compiler Done by programmer
Safer Risk of data loss
No casting syntax Uses cast operator