Data Types in C Language – BKNMU Junagadh | BCA Sem 1 Notes



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

BKNMU Junagadh NEP 2020 C Data Types Memory Allocation Format Specifiers
In simple words: A Data Type in C specifies the type of data that a variable can store. It tells the C compiler three crucial things: how much memory space to allocate for that variable, what kind of values can be stored inside it (e.g., integers, decimals, or characters), and what operations can be performed on that data.
📖 Four Major Classifications of C Data Types

C language classifies data types into four distinct categories based on their origin and complexity:

int, float, char

1. Primary Types

Predefined, fundamental types built directly into the C compiler. Used for basic data storage.

Array, Pointer, Function

2. Derived Types

Types constructed by combining or modifying primary data types to create advanced structures.

struct, union, enum

3. User-Defined Types

Types defined by the programmer using existing primary or derived types to suit specific needs.

void

4. Void Type

Specifies that no value or no data is available. Often used in functions to indicate "no return".

🎨 Memory Allocation at a Glance (Standard 32-bit System)

Typical Memory Size Allocated for Common Primary Types

char: 1 Byte int: 4 Bytes float: 4 Bytes double: 8 Bytes

(Note: Size of `int` can be 2 Bytes on older 16-bit systems or 4/8 Bytes on modern 64-bit systems.)

🔍 Comprehensive C Data Type Hierarchy
Primary (Built-in) Data Types
  • 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`.
Derived Data Types
  • 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.
User-Defined Data Types
  • 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.
1️⃣ In-Depth: Primary Data Types, Sizes, and Ranges

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 TypeSize
(Bytes)
Description / ApplicationFormat
Specifier
Approximate Range
(Values)
char1Stores single characters, symbols, or very small integers. Represents ASCII values.`%c`-128 to 127
unsigned char1Same as `char` but stores only positive values or extended ASCII characters.`%c`0 to 255
int4Stores 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 int4Same as `int` but stores only non-negative integers (zero and positive).`%u`0 to 4,294,967,295
short int2Used when smaller memory footprint is needed for integers. Saves memory.`%hd`-32,768 to 32,767
long int4 or 8Used when integers exceed the standard range of `int`. size is platform-dependent.`%ld`Same as `int` or larger
float4Stores single-precision floating-point numbers. Precision up to 6-7 decimal places.`%f`1.2E-38 to 3.4E+38
double8Stores double-precision floating-point numbers. Precision up to 15-16 decimal places.`%lf`2.3E-308 to 1.7E+308
long double10 or 12Stores extended-precision floating-point numbers for maximum accuracy.`%Lf`3.4E-4932 to 1.1E+4932
ℹ️ Format Specifiers: Notice the Format Specifier column in the table! This symbol (like %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!
🔍 Type Modifiers & The Void Data Type

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.
💻 Code Sample — Data Type Usage & sizeof() Operator

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:

#include <stdio.h> int main() { // Variable Declaration & Initialization char grade = 'A'; int age = 20; float weight = 65.5; double balance = 50000.75; // Printing values using format specifiers printf("Grade: %c\n", grade); printf("Age: %d\n", age); printf("Weight: %.1f\n", weight); // .1f restricts decimal output printf("Balance: %.2lf\n\n", balance); // Printing actual size in bytes on this compiler printf("Size of char: %lu bytes\n", sizeof(char)); printf("Size of int: %lu bytes\n", sizeof(int)); printf("Size of float: %lu bytes\n", sizeof(float)); printf("Size of double: %lu bytes\n", sizeof(double)); return 0; }
💡 Exam Tip on Integer Size: The exact size of 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).
🔄 Rules for Selecting Appropriate Data Types
1

Analyze Data Values

Determine the type of data being stored: is it a whole number, decimal number, single character, or no data?

2

Evaluate Required Range

Estimate the minimum and maximum values the data will hold. If negative numbers are impossible, use unsigned modifiers.

3

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.

⚠️ Type Overflow Error: If you try to store a number outside the data type's allowable range (e.g., storing `40000` in a 2-byte signed `short int`), C will not cause an error but will "wrap around" or store garbage data! This is called **Type Overflow**.
📝 Quick Revision — Key Exam Points
  • 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, long modify 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.

Post a Comment

Thanks for comment.

Previous Post Next Post