Lab Manual 1
Lab Manual 1
Lab Manual 1
Getting Started with Linux Environment and C
Programming
Operating System:
An operating system is a program that acts as an interface between the computer
user and computer hardware and controls the execution of programs.
1
Create a virtual Image of Ubuntu:
First, you have to download Virtual Box and Ubuntu by using the following links.
➢ Virtual Box: https://www.virtualbox.org/wiki/Downloads
➢ Ubuntu: https://ubuntu.com/download/desktop
Once you are done with the download open Virtual Box
2
Click on the new button and after clicking the new button choose the given
Setting:
3
Click next and choose the memory of your Ubuntu Linux OS:
4
Click next and Select Create a virtual Hard disk Now:
5
Your Ubuntu has been created on your Virtual Machine:
6
Now go to Settings because we have to do some setting before Installing
Ubuntu:
Select the Video Memory 128 MB
7
Go to the Storage option and click on empty:
8
Choose the Downloaded Ubuntu File from your disk by clicking on disk
Option:
9
10
As you can see the file has been added now click ok:
11
After Clicking OK Start Your Ubuntu OS:
12
Choose Install Ubuntu Option:
13
14
15
Click On Install Now:
16
Once you are done with the Installation Start Ubuntu and Open the Terminal:
17
Familiarize students with basic Linux commands:
➢ The use of ls Command:
1. This command displays a simple list of files and directories in the current
directory.
18
➢ The use of mkdir Command:
3. Preventing Errors: The -p option helps prevent errors that might occur if
intermediate directories are missing. Without it, attempting to create a
directory inside a non-existent path would result in an error.
19
5. Flexible Path Specification: Allows flexibility in specifying the path,
enabling users to create directories at any desired location within the file
system.
Task:
Create three directories’ labs, codes, docs
Create subdirectory “test” in “docs” using mkdir –p
Create a file “hello. c” in “docs” using the Vim editor and type some text
20
➢ The use of cd Command:
Syntax: cd directory_name
21
➢ Use of cp Command:
1. Single File Copy: The basic cp command, such as cp file /path/to/folder, is
used to copy a specific file to the specified destination. It provides a simple
way to duplicate individual files within the file system.
2. Recursive Copy: The -r option in cp -r folder /path/to/folder enables
recursive copying, allowing the entire contents of the source folder to be
duplicated into the target folder. This is useful for copying directories and their
subdirectories.
3. Selective Copy by Extension: Using wildcards, as in cp *.extension
/path/to/folder, allows for the selective copying of files based on their
extension. For instance, copying all .doc files to a new folder using cp *.doc
/path/to/folder. The destination folder must exist before the command is
executed.
4. Wildcard-Based Copy: The command cp name* /path/to/folder utilizes
wildcards to copy files starting with a specific prefix, such as "name," to the
designated folder. For example, copying all files starting with "example" using
cp example* /path/to/folder. The destination folder must be present before
execution.
5. Flexible Copy Operations: The cp command provides flexibility in copying
both individual files and entire directory structures, along with options for
22
selective or wildcard-based copying, enhancing its utility for various file
manipulation tasks.
6. Folder Dependency: It's essential to note that in examples 3 and 4, where
specific files or wildcard patterns are used, the destination folder must exist
before executing the command to ensure a successful copy operation.
Task:
1. Copy hello.c from “docs” to codes
2. Arguments:
source: Specifies the file or directory to be moved.
destination: Specifies the target location or new name for the file/directory.
3. Moving Files:
When mv is used to move a file to a different location, it effectively cuts and pastes the file to the
specified destination.
mv file.txt /path/to/destination/
4. Renaming Files:
By using mv, you can also rename files by specifying the same location as the destination but with
a different name.
mv oldfile.txt newfile.txt
mv can be used to move entire directories along with their contents to a new location or to rename
directories.
mv /path/to/source_directory /path/to/destination_directory
23
6. Overwriting Files:
If a file with the same name already exists in the destination, mv will overwrite it without any
warning.
mv -f source.txt /path/to/destination/
Task:
1. Move hello.txt from “docs” to labs
The touch command in Unix-like operating systems is primarily used to update the
access and modification timestamps of a file. However, if the specified file does not
exist, touch creates an empty file with the given name. Here's an explanation of the
syntax and usage for creating a C file using the touch command:
1. Basic Syntax:
touch filename.c
2. Arguments:
• If the file filename.c does not exist in the current directory, touch will
create an empty file with that name.
24
touch filename.c
The vim command is a text editor available on Unix-like systems that allows users
to create, view, and edit files. Here's an explanation of the syntax and usage of the
vim command to open a C file:
1. Basic Syntax:
vim filename.c
2. Arguments:
filename.c: Specifies the name of the file to be opened or edited. In this case, it's
assumed to be a C source file.
If the file filename.c exists in the current directory, vim will open it for editing.
25
vim filename.c
If the specified file doesn't exist, vim will create a new empty file with the provided
name.
vim newfile.c
After opening a file, vim starts in command mode. To navigate and perform various
actions, you need to enter command mode by pressing Esc.
6. Inserting Text:
To start inserting or appending text in the file, press i in command mode. This puts
vim into insert mode.
8. Saving Changes:
To save changes to the file, enter command mode (Esc) and type: w followed by
Enter.
:w
:wq
26
:q!
27
Here we go now we can write our first Program:
The gcc command is a widely used compiler on Unix-like systems, and it's often
used to compile C programs. Here's an explanation of the syntax and usage for
compiling a C program using the gcc command:
1. Basic Syntax:
2. Arguments:
filename.c: Specifies the name of the C source file that you want to compile.
-o output_filename: Specifies the name of the output file or executable. This is the
compiled program that will be generated.
3. Compiling a C Program:
28
The gcc command is used to compile C programs. In this case, it takes filename.c as
input and produces an executable file specified by -o output_filename.
4. Output Filename:
The -o option is followed by the desired name of the output file or executable. In the
example, it's set to my_program, but you can choose any valid filename.
If you omit the -o option, the compiled program will be named a.out by default.
gcc filename.c
You can compile multiple source files into a single executable by listing them after
the gcc command.
7. Debug Information:
To include debugging information in the compiled executable, you can use the -g
option.
8. Compile Only:
If you want to compile the source code without linking (producing an object file),
you can use the -c option.
9. Checking Version:
To check the version of gcc installed on your system, you can use the -v option.
gcc -v
29
➢ Once the program compiles Successfully run the program to see the output:
After successfully compiling a C program using gcc, you can run the compiled
executable to see the output. Here's an explanation of the syntax and usage for
running a C program:
1. Basic Syntax:
./filename
2. Executable Filename:
filename: Replace this with the name of the compiled executable. If you used the -o
option with gcc, use the specified output filename.
./my_program
3. Current Directory:
30
The ./ before the filename indicates that the executable is in the current directory.
This is necessary to run executables that are not in a directory listed in the system's
PATH.
Execute the program by typing ./filename in the terminal and pressing Enter.
./my_program
#include <stdio.h>
int main() {
// Print the string "Hello, World!" to
the console
printf("Hello, World!\n");
31
}
Here's an explanation of each part of the code:
1. #include <stdio.h>:
3. printf("Hello, World!\n");:
4. return 0;:
• The return statement ends the main function and returns a value to the
operating system. A return value of 0 conventionally indicates
successful execution, while a non-zero value may indicate an error.
Task:
Write a c program in output provide your 5-liner introduction.
32
33