OS Lab 2
OS Lab 2
OS Lab 2
XAVIER’S COLLEGE
Maitighar, Kathmandu
(Affiliated to Tribhuvan University)
LAB REPORT ON
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
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
Command Action:
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:
1. nano fibonacci.c
2. Write source code:
#include<stdio.h>
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;
}
Command Action:
Question 5.) WAP to find the area of the square using user defined power function from
Command:
1. nano square.h
2. Write source code:
#ifndef SQUARE_H
#define SQUARE_H
#endif
3. Ctrl + U
4. Ctrl + X
5. nano square.c
6. Write Source code:
#include "square.h"
Question 6.) Pass by value two user defined integers to perform addition and return the
Command:
1. nano PassByValue.c
2. Write source code:
#include <stdio.h>
Command:
1. nano PassByReference.c
2. Write source code:
#include<stdio.h>
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];
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>
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.