In this step, you will learn how to perform arithmetic operations on large integers using the long long
data type in C. We'll extend the previous example to demonstrate basic mathematical operations.
Open the previous file and modify it to include arithmetic operations:
cd ~/project
nano long_integer.c
Replace the previous content with the following code:
#include <stdio.h>
int main() {
// Declare long long variables for arithmetic operations
long long num1 = 9876543210LL;
long long num2 = 1234567890LL;
// Addition
long long sum = num1 + num2;
printf("Addition: %lld + %lld = %lld\n", num1, num2, sum);
// Subtraction
long long difference = num1 - num2;
printf("Subtraction: %lld - %lld = %lld\n", num1, num2, difference);
// Multiplication
long long product = num1 * num2;
printf("Multiplication: %lld * %lld = %lld\n", num1, num2, product);
return 0;
}
Let's break down the code:
- We declare two
long long
variables num1
and num2
- Perform addition, subtraction, and multiplication
- Use
%lld
format specifier to print large integer results
- The
LL
suffix ensures the numbers are treated as long long literals
Compile and run the program:
gcc long_integer.c -o long_integer
./long_integer
Example output:
Addition: 9876543210 + 1234567890 = 11111111100
Subtraction: 9876543210 - 1234567890 = 8641975320
Multiplication: 9876543210 * 1234567890 = 12193263111263526900