0% found this document useful (0 votes)
21 views7 pages

Os Exp2

The document outlines an experiment focused on creating static and dynamic link libraries for complex number operations, including code examples for calculating exponential functions and factorials. It explains the theories behind static and dynamic linking, highlighting their advantages and disadvantages. The conclusion emphasizes the importance of modular programming and the practical experience gained in managing dependencies and linking libraries in software development.

Uploaded by

Harshav Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Os Exp2

The document outlines an experiment focused on creating static and dynamic link libraries for complex number operations, including code examples for calculating exponential functions and factorials. It explains the theories behind static and dynamic linking, highlighting their advantages and disadvantages. The conclusion emphasizes the importance of modular programming and the practical experience gained in managing dependencies and linking libraries in software development.

Uploaded by

Harshav Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Name Harshav Shah

UID no. 2023300207

Experiment 2

AIM : Write a program for creating a static/dynamic link library for complex
number operations and then test this library through linux linker.

Static Linking
Thoery: Static linking is when a program includes all the necessary code, including
libraries, directly into the final executable during compilation. This means
the program doesn’t need to rely on external libraries when it runs, making
it easier to move between different systems. It also runs faster and avoids
dependency issues. However, the downside is that the executable file
becomes larger because the code from libraries is included in each
program.
Code: 1. expo.c → It is the exponential function which help us to calculate e^x and x^y

#include <stdio.h>
#include <math.h>

double calculate(double x, double y, int choice) {


​ switch(choice) {
​ case 1:
​ return exp(x);
​ case 2:
​ return pow(x, y);
​ default:
​ printf("Invalid choice\n");
​ return -1;
​ }
}

2. fact.c →It is the factorial function used to calculate the factorial of a number

#include <stdio.h>

long long factorial(int n) {


​ long long fact = 1;
​ for (int i = 1; i <= n; i++) {
​ fact *= i;
​ }
​ return fact;
}

3. lib_mylib.h

long long factorial(int n);


double calculate(double x, double y, int choice);

4. driver.c

#include <stdio.h>
#include "lib_mylib.h"

int main() {
​ int n;
​ printf("\nEnter a number to calculate factorial: ");
​ scanf("%d", &n);
​ printf("Factorial of %d is: %lld\n", n, factorial(n));

​ double x, y;
​ int choice;
​ printf("\nEnter value for x: ");
​ scanf("%lf", &x);

​ printf("Enter value for y (only needed for x^y calculation): ");


​ scanf("%lf", &y);

​ printf("Choose the operation:\n");


​ printf("1. e^x\n");
​ printf("2. x^y\n");
​ printf("Enter your choice (1 or 2): ");
​ scanf("%d", &choice);

​ double result = calculate(x, y, choice);

​ if (result != -1) {
​ printf("Result: %.2f\n", result);
​ }

​ return 0;
}
OUTPUT:

Dynamic Linking
Theory: Dynamic linking is when a program uses external libraries during runtime
instead of including them in the program at compile time. This means the
program doesn’t have all the code built into it; instead, it refers to shared
libraries that are loaded when the program runs. This makes the program
smaller and allows multiple programs to use the same library, saving
memory. However, the program relies on the correct version of the library
being available when it runs, and if the library changes, the program might
need to be updated. Dynamic linking is useful for saving space and making
updates easier.
Code: 1.expo.c → To calculate e^x and x^y

#include <stdio.h>
#include <math.h>

double calculate(double x, double y, int choice) {


​ switch(choice) {
​ case 1:
​ return exp(x);
​ case 2:
​ return pow(x, y);
​ default:
​ printf("Invalid choice\n");
​ return -1;
​ }
}

2. fact.c → To calculate the factorial of a number

#include <stdio.h>

long long factorial(int n) {


​ long long fact = 1;
​ for (int i = 1; i <= n; i++) {
​ fact *= i;
​ }
​ return fact;
}

3. lib_mylib.h

long long factorial(int n);


double calculate(double x, double y, int choice);

4. driver.c

#include <stdio.h>
#include "lib_mylib.h"

int main() {
​ int n;

​ printf("\nEnter a number to calculate factorial: ");


​ scanf("%d", &n);
​ printf("Factorial of %d is: %lld\n", n, factorial(n));

​ double x, y;
​ int choice;
​ printf("\nEnter value for x: ");
​ scanf("%lf", &x);

​ printf("Enter value for y (only needed for x^y calculation): ");


​ scanf("%lf", &y);

​ printf("Choose the operation:\n");


​ printf("1. e^x\n");
​ printf("2. x^y\n");
​ printf("Enter your choice (1 or 2): ");
​ scanf("%d", &choice);

​ double result = calculate(x, y, choice);

​ if (result != -1) {
​ printf("Result: %.2f\n", result);
​ }

​ return 0;
}
Output:
Conclusion: The creation of static and dynamic link libraries for complex number
operations demonstrates the power of modular programming, where
commonly used functionalities can be packaged into reusable libraries. By
testing these libraries with the Linux linker, we gain hands-on experience in
linking compiled code with external libraries, understanding the differences
between static and dynamic linking, and learning how to manage
dependencies efficiently in software development. This process enhances
the portability, maintainability, and flexibility of programs.

You might also like