
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- 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 - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs 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
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- 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 - Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
Lookup Tables in C
Lookup tables (popularly known by the abbreviation LUT) in C are arrays populated with certain pre-computed values. Lookup tables help avoid performing a lot of calculations in a program. Instead of lengthy nested if-else statements or switch statements, one can use Lookup tables to enhance the efficiency of a C program.
Example 1
Let us have a look at a simple application of a lookup table. In the following code, we compute the square of a given integer.
#include <stdio.h> int square(int x){ return x*x; } int main(){ int num[5] = {1, 2, 3, 4, 5}; for (int i = 0; i <= 4; i++){ printf("No: %d \tSquare(%d): %d\n", i+1, i+1, square(i+1)); } return 0; }
Output
When you run this code, it will produce the following output −
No: 1 Square(1): 1 No: 2 Square(2): 4 No: 3 Square(3): 9 No: 4 Square(4): 16 No: 5 Square(5): 25
Example 2
While the above program works satisfactorily, it involves frequent calls to the square() function, for each value of the array index.
Instead, we can declare an array to store the squares of numbers and access the computed square directly from the index.
#include <stdio.h> int main(){ int squares[5] = {1, 4, 9, 16, 25}; for (int i = 0; i <= 4; i++){ printf("No: %d \tSquare(%d): %d\n", i+1, i+1, squares[i]); } return 0; }
Output
Run the code and check its output −
No: 1 Square(1): 1 No: 2 Square(2): 4 No: 3 Square(3): 9 No: 4 Square(4): 16 No: 5 Square(5): 25
Example 3
In the example below, we fetch the name of the element corresponding to its atomic number.
# include <stdio.h> int main(){ int num = 3; switch (num){ case 1: puts("Hydrogen"); break; case 2: puts("Helium"); break; case 3: puts("Lithium"); break; case 4: puts("Beryllium"); break; case 5: puts("Boron"); break; default: puts("error: unknown element!"); } return 0; }
Output
It will produce the following output −
Lithium
Example 4
Instead of a lengthy switch statements with a case for each element, we use a lookup table (an array populated with the names of all the elements) to simplify the program −
#include <stdio.h> static const char *table[] = { "Hydrogen", "Helium", "Lithium", "Beryllium", "Boron" }; int main(){ int num = 3; if (num >= 1 && num <= 5){ printf("Name of the element with atomic number %d is %s", num, table[num-1]); } else { puts("error: Atomic number not in the lookup table!"); } return 0; }
Output
Run the code and check its output −
Name of the element with atomic number 3 is Lithium
Lookup Tables in 7-Segment LED Display
Lookup tables are extensively used in the design of embedded systems, as they enhance the performance of the application.
In many devices, the seven-segment LED display is used to show visual output. Eight segments of the unit are illuminated based on a sequence of binary digits. We use a lookup table to convert numbers ranging from 0 to 9 to seven-segment signals to drive a display.
Example
The 7-segment binary code for the number is stored as an array element. The hexadecimal code is then converted to binary code that will drive the 7-segment display.
#include <stdio.h> int main(){ // Array to represent numbers 0-9 in 7-segment display binary encoding int const nums[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; static int bin[8]; int i = 0, num = 7, rem; printf("The binary equivalent of 7 is: "); for (i = 7; i >= 0; i--){ rem = num % 2; bin[i] = rem; num /= 2; } for (i = 0; i <= 7; i++){ printf("%d", bin[i]); if (i == 3) printf(" "); } return 0; }
Output
Run the code and check its output −
The binary equivalent of 7 is: 0000 0111
The least significant bits represent segments "a", "b", "c" and "d". The most significant bits are "e", "f", "g" and "h". The segments "a", "b" and "c" illuminate to display 7, leaving the other segments off.