Lab 1 Introduction To Linux Environment and C Compiler
Lab 1 Introduction To Linux Environment and C Compiler
Laboratory Module
LAB 1
INTRODUCTION TO LINUX ENVIRONMENT AND C
COMPILER
Laboratory Module
1. GETTING STARTED:
1.1 Steps to Create New Folder:
1.1.1 Click Places icon > Home Folder > Go to File menu > Create Folder
1.1.2 Rename the New Folder as your Matrix No.
1.2 Steps to Start the Text Editor:
1.2.1 Click Applications icon > Accessories > Text Editor (you are now in
gedit editor).
1.2.2 Once the gedit text editor started, automatically it will view an untitled
program.
1.3 Steps to Create New Program in gedit:
1.3.1 Type the following program:
// Program Example 1
// This program displays message "Welcome to UniMAP
#include <stdio.h>
int main()
{
printf("\nWelcome to UniMAP\n");
return 0;
}
1.3.2 Once finished, save the program file or also known as the source file.
1.4 Steps to Save the Source File:
1.4.1 Go to File menu, select Save as option.
1.4.2 Save the file as welcome.c. Type welcome.c in the box besides the
Name location.
OR
if you want to save in a specified folder, select the folder in the save as
dialog box.
(e.g. /home/student/foldername), double click the folder icon and then
type welcome.c in the box besides the Name location. Click Save button.
Notes : The filename must be in one word with extension .c, for
example Program.c or pgm_1.c.
1.5 Steps to Compile a Program:
1.5.1 Click Applications icon > Accessories > Terminal
1.5.2 Use the following commands to check the source file location:
pwd
- command to check the current directory
ls
- to list down the files in the current directory.
cd directory - to change directory
1.5.3
If you have found your file, use the command gcc to compile the
program welcome.c
gcc welcome.c
Laboratory Module
You can also compile the program and specify the output file (the
executable file) name together with the compile command. This is to
specify or named your binary file related to your program.
gcc -o welcome.out welcome.c
OR
gcc welcome.c -o welcome.out
Note: use the o parameter to specify the output file, followed the
filename that you want to specify.
./a.out
./welcome.out
Laboratory Module
2. TASKS
Now you have learned to compile a simple C program, try to answer the following
questions:
2.1 True or False:
a.
Ans:
b.
c.
Ans:
b.
c.
d.
2.3 Type, compile and run this program, correct any error(s) you may find and rewrite
the program.
main(
);
2.4 Determine and correct any error(s) you may find in the following command.
ma
Laboratory Module
d. Format the floating number into 8 numbers and 2 floating point. Write down the
formatting format you use.
e. Correct the program so that the output looks presentable. Describe the changes
you have done and the output of the program.
i.
Laboratory Module
f.
Change the first void to int, then compile the program. Write down any
message after compiling.
g. Explain the program in your own word and describe the command from top until
end of the program.