0% found this document useful (0 votes)
19 views

Lab 2 OS

Uploaded by

Yatra Nepal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lab 2 OS

Uploaded by

Yatra Nepal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab Number: 02

Lab Title: Using GCC complier of Linux CLI compile and run different programs in C

programming language.

Objective: To understand basic commands to compile and run a C program in Linus using

GCC.

Command Used: Linux Commands

IDE/Platform: Terminal (Linux)

Theory:

GCC (GNU Compiler Collection) is a compiler system produced by the GNU Project
supporting various programming languages. Originally developed to compile the C
programming language, GCC has expanded to support a wide range of languages including
C++, Objective-C, Fortran, Ada, Go, and more.
Experimentation

Questions:

1. Create a directory path "FullNameRollNumber"

Create a separate file for each of the following program:

Command:

mkdir NirajanPaudel023
cd NirajanPaudel023

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

Command:

nano os.c
gcc -o os os.c
./os

Command Action:

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


Command:

nano fib.c
gcc -o fib fib.c
./fib

Command Action:

4. WAP to check if integer entered by user is prime or not.

Command:

nano prime.c
gcc -o prime prime.c
./prime
Command Action:

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:
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:
7. Pass by reference to swap two integers.

Command:

1. nano passbyref.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 passbyref.c
6. ./a.out

Command Action:

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

Command:

1. nano stddetails.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 –o stddetails stddetails.c
6. ./stddetails

Command Action:
9. Display the file name and a number passed on to the main function

Command:

1. nano file.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 –o file file.c
6. ./file 1292
Command Action:
Discussion:

When providing instructions in the command terminal, the directory has to be correct
otherwise, the command will not deliver the proper action.

Conclusion:

Hence, we got basic information on GCC compiler and commands to compile and run a C
program command line terminal using GCC compiler in Linux operating system.

You might also like