- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - 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
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.