STRUCTURED PROGRAMMING

3. Program structure

3.2. Data types

1. Classification of Data Types

Data types in C are broadly classified into three categories:

A. Primary (Primitive/Fundamental) Data Types

These are the basic built-in types provided by the C language.

Data Type Keyword Description Typical Size (bytes) Format Specifier
Integer int Stores whole numbers (positive or negative). 2 or 4 %d
Character char Stores a single character (letter/symbol). 1 %c
Float float Stores numbers with decimal points (single precision). 4 %f
Double double Stores large decimal numbers (double precision). 8 %lf
Void void Represents the absence of value (used in functions). 0 -

B. Derived Data Types

These are derived from the fundamental data types to handle complex data.

  • Arrays: A collection of elements of the same data type (e.g., a list of 10 integers).

  • Pointers: Variables that store the memory address of another variable.

  • Functions: A block of code that returns a value of a specific data type.

C. User-Defined Data Types

These allow the programmer to define their own data structures.

  • Structure (struct): A collection of variables of different data types grouped together under a single name (e.g., a "Student" structure containing name, age, and marks).

  • Union (union): Similar to structures, but all members share the same memory location.

  • Enumeration (enum): Consists of named integer constants (e.g., enum Week {Mon, Tue, Wed}).


2. Data Type Modifiers

Modifiers are used with basic data types (specifically int and char) to alter the size or range of data they can hold.

  • signed: The variable can hold both positive and negative numbers (default for int).

  • unsigned: The variable can hold only positive numbers (doubles the positive range).

  • short: Reduces the storage size (usually 2 bytes).

  • long: Increases the storage size (usually 4 or 8 bytes).

Example of Modifier Usage:

C
short int age = 20;          // Uses less memory
unsigned long int distance;  // Uses more memory, positive values only


3. Variable Declaration Syntax

In structured programming, you must declare a variable's data type before using it.

Syntax: data_type variable_name;

Examples:

C
int score = 95;
float pi = 3.142;
char grade = 'A';
double salary = 50000.50;

4. Choosing the Right Data Type

  • Use int for counters or counting items (e.g., number of students).

  • Use float for measurements needing precision (e.g., temperature, currency).

  • Use double for high-precision scientific calculations.

  • Use char for menu options (e.g., Press 'Y' for Yes).