OS Lab 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

ST.

XAVIER’S COLLEGE
Maitighar, Kathmandu
(Affiliated to Tribhuvan University)

LAB REPORT ON

OPERATING SYSTEMS (CSC264)

SUBMITTED BY: SUBMITTED TO:

Name: Er. Anuj Shrestha


Faculty/Semester: (NEC Registration No.: 5495)
Roll no.: Lecturer
Dept. of Computer Science
Lab Number: 2

Title: Using GCC complier of Linux CLI compile and run different programs in C
programming language.
Objective: To learn to compile and execute programs in C using terminal commands in
Linux with GCC compiler
Command Used: Linux CLI Commands

IDE/Platform: Terminal of Linux

Theory:

GCC, which stands for GNU Compiler Collection, is a widely used open-source compiler system for
programming languages like C, C++, and Fortran. It's a suite of compilers developed by the GNU
Project, originally started by Richard Stallman.
GCC provides compilers for several programming languages, including:
 C Compiler (gcc): Used to compile C programs.
 C++ Compiler (g++): Used to compile C++ programs.
 Fortran Compiler (gfortran): Used to compile Fortran programs.
 Objective-C Compiler (gcc): Used to compile Objective-C programs.
 Objective-C++ Compiler (g++): Used to compile Objective-C++ programs.
Experimentation

Question 1.) Create a directory path "FullNameRollNumber"

Command: mkdir JenishShrestha014

Command Action:

Question 2.) WAP to display a message "Operating Systems are Best"

Command:

1. cd JenishShrestha014
2. nano message.c
3. Write source code:
#include<stdio.h>
int main (){
printf("Operating Sytems are Best");
return 0;
}
4. Ctrl + U
5. Ctrl + X
6. gcc message.c
7. ./a.out

Command Action:

Question 3.) WAP to display fibonanci sequence up to nth term.


Command:

1. nano fibonacci.c
2. Write source code:
#include<stdio.h>

int fibonacci(int n){


if(n<=1){
return n;
}
else {
return fibonacci(n-1) + fibonacci(n-2);
}
}

int main (){


int n;
printf("Enter the number of terms for Finbonacci
series: ");
scanf("%d", &n);
for (int i=0; i<n; i++){
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
3. Ctrl + U
4. Ctrl + X
5. gcc fibonacci.c
6. ./a.out
Command Action:
Question 4.) WAP to check if integer entered by user is prime or not.

Command:

1. nano prime.c
2. Write source code:
#include<stdio.h>
#include<stdbool.h>
bool isPrime(int n){
if(n<=1)
return false;
for (int i = 2; i< n/2; i++){
if(n%i ==0)
return false;
}
return true;
}

int main (){


int num;
printf("Enter a number for prime:");
scanf("%d", &num);
if (isPrime(num))
printf("\nThe number %d is prime.", num);
else
printf("\nThe number %d is composite.",
num);
return 0;
}
3. Ctrl + U
4. Ctrl + X
5. gcc prime.c
6. ./a.out

Command Action:

Question 5.) WAP to find the area of the square using user defined power function from

a user defined header file.

Command:

1. nano square.h
2. Write source code:
#ifndef SQUARE_H
#define SQUARE_H

int power(int base, int exponent);

#endif
3. Ctrl + U
4. Ctrl + X
5. nano square.c
6. Write Source code:
#include "square.h"

int power(int base, int exponent) {


int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
7. Ctrl + U
8. Ctrl + X
9. nano area.c
10. Write source code as:
#include<stdio.h>
#include "square.h"

int main (){


int side;
printf("Enter the side of the square:");
scanf("%d", &side);
int area = power(side, 2);
printf("Area of the square = %d\n", area);
return 0;
}
11. gcc area.c square.c –o area
12. ./area
Command Action:

Question 6.) Pass by value two user defined integers to perform addition and return the

result in the main function

Command:

1. nano PassByValue.c
2. Write source code:
#include <stdio.h>

int add(int a, int b){


return a+b;
}

int main (){


int x, y;
printf("Enter two digits for addition:");
scanf("%d %d", &x, &y);
int result = add(x,y);
printf("The addition of the given numbers is: %d\
n", result);
return 0;
}
3. Ctrl + U
4. Ctrl + X
5. gcc PassByValue.c
6. ./a.out
Command Action:

Question 7.) Pass by reference to swap two integers

Command:

1. nano PassByReference.c
2. Write source code:
#include<stdio.h>

void swap (int *x, int*y){


int temp;
temp= *x;
*x=*y;
*y=temp;
}

int main(){
int x, y;
printf("Enter value for x: ");
scanf("%d", &x);
printf("Enter value for y: ");
scanf("%d", &y);
printf("Before swapping x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping x = %d, y = %d\n", x, y);
return 0;
}
3. Ctrl + U
4. Ctrl + X
5. gcc PassByReference.c
6. ./a.out
Command Action:

Question 8.) Use struct type to get the name, roll, faculty, phone number of 2 students
and display these details

Command:

1. nano StudentDetails.c
2. Write source code:
#include<stdio.h>
#include<stdlib.h>

struct Student_info {
char name[50];
int roll;
char faculty[30];
long int contact;
} S[2];

int main (){


for (int i=0; i<2; i++){
printf("Enter the details of student %d: ",i+1);
printf("Name: ");
scanf(" %[^\n]", S[i].name);
printf("Roll No.: ");
scanf(" %d", &S[i].roll);
printf("Faculty: ");
scanf(" %s", S[i].faculty);
printf("Phone No.: ");
scanf(" %ld", &S[i].contact);
}
printf("The details of the students are: ");
for (int i=0; i<2; i++){
printf("The details of student %d:\n ", i+1);
printf("Name: %s\n", S[i].name);
printf("Roll No.: %d\n", S[i].roll);
printf("Faculty: %s\n", S[i].faculty);
printf("Phone No.: %ld\n", S[i].contact);
printf("\n");
}
return 0;
}
3. Ctrl + U
4. Ctrl + X
5. gcc StudentDetails.c
6. ./a.out
Command Action:

Question 9.) Display the file name and a number passed on to the main function

Command:

1. nano FileInfor.c
2. Write source code:
#include <stdio.h>

int main(int argc, char *argv[]) {


if (argc < 2) {
printf("Usage: %s <number>\n", argv[0]);
return 1;
}
printf("File name: %s\n", argv[0]);
printf("Number passed: %s\n", argv[1]);

return 0;
}
3. Ctrl + U
4. Ctrl + X
5. gcc FileInfor.c –o Finfo
6. ./Finfo 12345
Command Action:
Discussion:
When compiling C programs using the gcc command in Linux, it’s necessary to know a few key
points. Firstly, you can specify the name of the output executable file using the –o options (like
gcc filename.c –o outputFileName) otherwise gcc will name the output file as a.out. Additionally,
for the user defined header files, you need to compile the program explicitly using gcc before
compiling ht emain program. This ensures the necessary dependencies being resolved and
included in the final executable.

Conclusion:

In summary, gcc is a powerful tool for compiling C programs in Linux, but it's important to specify
the output executable name if you don't want it to default to a.out. Also, be mindful of including any
user-generated header files and compiling them separately as needed. These practices ensure smooth
compilation and execution of C programs in a Linux environment using gcc. Hence, we got
familiarized with gcc and compiling and executing C programs in linux using it.

You might also like