- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Lookup Tables
![]() Share with a Friend |
C Programming - C Lookup Tables
C Lookup Tables
A lookup table in C is a data structure (usually an array or hash table) used to map input values to corresponding output values. It allows for efficient data retrieval, often replacing repetitive computations or decision-making logic with simple indexing operations. Lookup tables are widely used in programming for tasks like function approximation, data mapping, and optimization.
Features of Lookup Tables
- Efficiency: Accessing data through indexing is faster than computing results dynamically.
- Precomputed Data: Values are precomputed and stored, reducing runtime computation.
- Space-Time Tradeoff: Increases memory usage but decreases computational overhead.
Types of Lookup Tables
- One-Dimensional Lookup Table:
- A single array where an index directly corresponds to a value.
- Multi-Dimensional Lookup Table:
- A multi-dimensional array used for more complex mappings.
- Hash Tables:
- A more advanced form of lookup table using hash functions for data retrieval.
- Function Pointers Table:
- A table of function pointers used to call different functions dynamically.
Basic Example of a Lookup Table
Using an Array
C
#include <stdio.h>
// Lookup table for square values
int square_lookup[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};
int main() {
int index = 7;
printf("The square of %d is %d\n", index, square_lookup[index]);
return 0;
}
Output:
The square of 7 is 49
Applications of Lookup Tables
- Mathematical Computations:
- Storing precomputed values like sine, cosine, or logarithms.
- Data Mapping:
- Converting input values to specific outputs (e.g., ASCII character tables).
- Function Pointers:
- Dynamic function calls based on input.
- Encoding and Decoding:
- Converting data to another format (e.g., Base64 encoding).
- Graphics and Image Processing:
- Mapping colors or pixel intensities.
Advanced Examples
Function Pointer Table
C
#include <stdio.h>
// Functions
void function1() { printf("Function 1 called\n"); }
void function2() { printf("Function 2 called\n"); }
void function3() { printf("Function 3 called\n"); }
int main() {
// Lookup table of function pointers
void (*function_table[3])() = {function1, function2, function3};
// Call a function using the table
int choice = 2; // 0-based index
if (choice >= 0 && choice < 3) {
function_table[choice]();
} else {
printf("Invalid choice\n");
}
return 0;
}
Output:
Function 3 called
Character Mapping Table
C
#include <stdio.h>
// Simple ASCII to uppercase mapping
char uppercase_lookup[256];
void initialize_table() {
for (int i = 0; i < 256; i++) {
if (i >= 'a' && i <= 'z') {
uppercase_lookup[i] = i - 32; // Convert to uppercase
} else {
uppercase_lookup[i] = i; // Keep as is
}
}
}
int main() {
initialize_table();
char input = 'g';
printf("Uppercase of '%c' is '%c'\n", input, uppercase_lookup[(unsigned char)input]);
return 0;
}
Output:
Uppercase of 'g' is 'G'
Advantages of Lookup Tables
- Fast Access: Data retrieval is instantaneous through indexing.
- Simplified Logic: Eliminates complex decision-making logic.
- Optimized Performance: Reduces runtime computation overhead.
Disadvantages of Lookup Tables
- Memory Usage: Requires additional memory to store precomputed values.
- Static Nature: Values are fixed unless explicitly updated.
- Limited Flexibility: Dynamic computation is not possible with static tables.
Conclusion
Lookup tables are a powerful tool for optimizing performance by replacing complex calculations or logic with fast data retrieval. By precomputing and storing values, they enhance efficiency, especially in real-time systems and applications requiring high-speed data access.
