BCA Sem 1 | Unit 1 | Computer Concepts & C Programming
Data Types in C Language — BKNMU Junagadh
Complete Guide to Primary, Derived, User-Defined & Void Types with Size, Range & Examples
C language classifies data types into four distinct categories based on their origin and complexity:
1. Primary Types
Predefined, fundamental types built directly into the C compiler. Used for basic data storage.
2. Derived Types
Types constructed by combining or modifying primary data types to create advanced structures.
3. User-Defined Types
Types defined by the programmer using existing primary or derived types to suit specific needs.
4. Void Type
Specifies that no value or no data is available. Often used in functions to indicate "no return".
Typical Memory Size Allocated for Common Primary Types
(Note: Size of `int` can be 2 Bytes on older 16-bit systems or 4/8 Bytes on modern 64-bit systems.)
Integer Type: Storing whole numbers (e.g., 10, -500). Examples: `int`, `short`, `long`.Floating-Point Type: Storing numbers with decimal parts. Examples: `float`, `double`, `long double`.Character Type: Storing single characters. Example: `char`.
Array: A collection of elements of the same data type.Pointer: A variable that stores the memory address of another variable.Function: A block of code constructed to perform a specific task.
Structure (struct): Groups variables of different data types under one name.Union (union): Similar to struct but shares the same memory location for all members.Enumeration (enum): Assigns names to integer constants.Typedef: Creates an alias (nickname) for an existing data type.
Here is the detailed technical breakdown of primary data types including their default memory size, format specifiers used in printf(), and allowable value ranges on a standard 32-bit architecture:
| Data Type | Size (Bytes) | Description / Application | Format Specifier | Approximate Range (Values) |
|---|---|---|---|---|
char | 1 | Stores single characters, symbols, or very small integers. Represents ASCII values. | `%c` | -128 to 127 |
unsigned char | 1 | Same as `char` but stores only positive values or extended ASCII characters. | `%c` | 0 to 255 |
int | 4 | Stores whole numbers (integers). The default type for non-decimal numbers. Size may vary. | `%d` / `%i` | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 | Same as `int` but stores only non-negative integers (zero and positive). | `%u` | 0 to 4,294,967,295 |
short int | 2 | Used when smaller memory footprint is needed for integers. Saves memory. | `%hd` | -32,768 to 32,767 |
long int | 4 or 8 | Used when integers exceed the standard range of `int`. size is platform-dependent. | `%ld` | Same as `int` or larger |
float | 4 | Stores single-precision floating-point numbers. Precision up to 6-7 decimal places. | `%f` | 1.2E-38 to 3.4E+38 |
double | 8 | Stores double-precision floating-point numbers. Precision up to 15-16 decimal places. | `%lf` | 2.3E-308 to 1.7E+308 |
long double | 10 or 12 | Stores extended-precision floating-point numbers for maximum accuracy. | `%Lf` | 3.4E-4932 to 1.1E+4932 |
%d or %f) must be used inside printf() and scanf() functions to tell C how to correctly read from or print data to the user screen!
C provides keywords called **Type Modifiers** to alter the memory size and range of basic data types:
signed: Variables can store both positive and negative numbers (default for `int`).unsigned: Variables can store only zero and positive numbers, effectively doubling the positive range.short: Requests the compiler to reduce memory allocation size (usually for `int`).long: Requests the compiler to increase memory allocation size (for `int` and `double`).
The Void Data Type (void):
- Represents **no value** or **empty set of values**.
- It cannot be used to declare variables (e.g.,
void x;causes an error). - Used in functions to indicate "No return value" (e.g.,
void myFunction() { ... }). - Used in function parameters to indicate "Takes no arguments".
- Used in pointers (
void*) to create "generic pointers" that can point to any data type.
Observe how different data types are declared, initialized, and printed in a C program, and how the sizeof() operator is used to verify their size:
int is not fixed by C standard; it depends entirely on the computer architecture (16-bit, 32-bit, or 64-bit). In exams, always verify with sizeof(int) if available, or state the assumption (typically 4 Bytes for modern systems).
Analyze Data Values
Determine the type of data being stored: is it a whole number, decimal number, single character, or no data?
Evaluate Required Range
Estimate the minimum and maximum values the data will hold. If negative numbers are impossible, use unsigned modifiers.
Consider Precision and Memory
Use `double` for high-precision math; use `float` otherwise to save memory. Use `short int` over `long int` when range is low.
- Definition: Specifies variable memory size, value type, and allowed operations.
- 4 Categories: Primary, Derived, User-Defined, and Void.
- Primary Types:
char(1B),int(4B),float(4B),double(8B) [Sizes assume 32-bit system]. - Modifiers:
signed,unsigned,short,longmodify range/size. - Void Data Type: Represents emptiness, used for function return types, generic pointers.
- Format Specifiers:
%c,%d,%f,%lf, etc., are essential for I/O operations. - sizeof Operator: Returns the actual memory size of data type or variable on current machine.