0% found this document useful (0 votes)
9 views34 pages

Lab Manual 1

Uploaded by

shr78138
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)
9 views34 pages

Lab Manual 1

Uploaded by

shr78138
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/ 34

Fundamentals of Programming

Lab Manual 1
Getting Started with Linux Environment and C
Programming

SCHOOL OF INTERDISCIPLINARY ENGINEERING AND SCIENCESNATIONAL UNIVERSITY OF


SCIENCES AND TECHNOLOGY (NUST)
Objectives:
1. The purpose of this lab is to familiarize students with how to create a virtual
image of Ubuntu inside your Windows operating system using Virtual Box.
2. Familiarize students with basic Linux commands.
3. Install the C compiler in Linux OS.
4. Write and compile C program

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:

Click Next and Then Click on Finish:

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:

Choose the Try and Install Ubuntu Option:

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.

2. Basic Listing: The primary function of the ls command is to display a


straightforward list of files and directories in the current working directory.

3. No Frills Output: When used without any options, ls provides a minimalist


output, showing only the names of files and directories without additional
details.

4. Customizable Display: Users can enhance the command's output by


incorporating various options, allowing them to tailor the information
displayed, such as file sizes, permissions, and timestamps.

5. Terminal Navigation Tool: Essential for navigating and exploring the


contents of directories within a terminal or command-line environment.

6. Foundation for Advanced Commands: The simplicity of ls makes it a


foundational command upon which more complex and powerful directory-
related commands can be built.

7. Quick Overview: Provides a quick and concise overview of the directory's


contents, aiding users in identifying and locating files and directories.

18
➢ The use of mkdir Command:

1. Basic Directory Creation: The command mkdir directory_name is used to


create a new folder with the specified name in the current working directory.
For example, mkdir my_folder creates a directory named "my_folder."

2. Recursive Path Creation: The option -p in mkdir -p /path/to/folder/name


allows for the creation of directories along the specified path, even if
intermediate directories don't exist. In this example, the command creates the
directory "name" inside the "folder," then "to," and finally inside the "path."
It ensures the complete path is created, resulting in four new directories.

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.

4. Efficient Directory Structure Setup: Useful for efficiently setting up


complex directory structures with a single command, streamlining the process
of creating nested directories.

19
5. Flexible Path Specification: Allows flexibility in specifying the path,
enabling users to create directories at any desired location within the file
system.

6. Time-saving: Especially beneficial when dealing with deep directory


hierarchies, as it eliminates the need to create each intermediate directory
manually.

Syntax: mkdir directory_name

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:

1. Home Directory Navigation: The basic cd command without any arguments


instantly moves the user back to their home directory, providing a quick way
to return to the default working location.

2. Parent Directory Access: Using cd .. moves up one level in the directory


hierarchy, allowing users to navigate to the parent directory of the current
location.

3. Specific Folder Navigation: The command cd folder name is employed to


move into a specific directory named "folder name," enabling precise
navigation to desired locations within the file system.

4. Absolute Path Navigation: By specifying an absolute path like cd


/some/other/path, users can directly switch to a particular directory, regardless
of their current location. It's crucial to input the path accurately, and tab
completion can assist in avoiding errors.

5. Tab Completion Efficiency: Mention of tab completion emphasizes the


efficiency of the cd command, as users can type a portion of a directory or file
name and press the Tab key to auto-complete, reducing the risk of errors and
speeding up navigation.

6. Fundamental File System Navigation: Serving as a fundamental command,


cd is essential for users to traverse and explore the file system, facilitating
effective command-line navigation and manipulation of directories.

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

➢ The Use of Mv Command:

The syntax of mv is similar to cp.


1. Basic Syntax:
mv [options] source destination

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

5. Moving and Renaming Directories:

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

➢ Creating a C file in a directory:

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:

• filename.c: Specifies the name of the file to be created or updated. In


this case, it has a .c extension, indicating a C source file.

3. Creating a New File:

• 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

➢ Open this file in Editor.

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.

3. Opening an Existing File:

If the file filename.c exists in the current directory, vim will open it for editing.

25
vim filename.c

4. Creating a New File:

If the specified file doesn't exist, vim will create a new empty file with the provided
name.

vim newfile.c

5. Entering Command Mode:

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.

7. Exiting Insert Mode:

To exit insert mode and return to command mode, press Esc.

8. Saving Changes:

To save changes to the file, enter command mode (Esc) and type: w followed by
Enter.

:w

9. Saving and Quitting:

To save changes and quit vim, use:wq in command mode.

:wq

10.Discarding Changes and Quitting:

To discard changes and quit vim, use :q! in command mode.

26
:q!

27
Here we go now we can write our first Program:

➢ Compile this program by using the given command:

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:

gcc filename.c -o output_filename

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.

gcc filename.c -o my_program

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.

5. Default Output Filename:

If you omit the -o option, the compiled program will be named a.out by default.

gcc filename.c

6. Compiling Multiple Source Files:

You can compile multiple source files into a single executable by listing them after
the gcc command.

gcc file1.c file2.c -o my_program

7. Debug Information:

To include debugging information in the compiled executable, you can use the -g
option.

gcc filename.c -o my_program -g

8. Compile Only:

If you want to compile the source code without linking (producing an object file),
you can use the -c option.

gcc filename.c -c -o object_file.o

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.

4. Running the Program:

Execute the program by typing ./filename in the terminal and pressing Enter.

./my_program

Basic Structure of C Program:

#include <stdio.h>

int main() {
// Print the string "Hello, World!" to
the console
printf("Hello, World!\n");

// Return 0 to indicate successful


execution
return 0;

31
}
Here's an explanation of each part of the code:

1. #include <stdio.h>:

• This line includes the standard input-output library (stdio.h). This


library is necessary for using functions like printf to perform input and
output operations.

2. int main() { ... }:

• The main function is the entry point of every C program. Execution of


the program starts from here. The opening curly brace { marks the
beginning of the function body, and the closing curly brace } marks the
end.

3. printf("Hello, World!\n");:

• The printf function is used to print formatted output to the console. In


this case, it prints the string "Hello, World!" followed by a newline
character (\n). The \n is used to move the cursor to the next line.

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

You might also like