EKT 120 – Introduction to Computer Programming Laboratory Module
LAB 1
INTRODUCTION TO LINUX ENVIRONMENT AND C
COMPILER
ANSWER
School of Computer and Communication Engineering
Universiti Malaysia Perlis
1
EKT 120 – Introduction to Computer Programming 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 - 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
2
EKT 120 – Introduction to Computer Programming Laboratory Module
Note: after compiling using the above command, if there is no error
a default binary file, “a.out” is created. This is an executable file.
1.5.4 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.
1.6 Steps to Execute or Run the Program.
1.6.1 In the Terminal window, at the command prompt, run the binary file.
1.6.2 To run the default output (or binary) file, type: ./a.out
Note: the “./” to specify the binary file is in the current directory.
OR
To run the specific output (or binary) file, type: ./welcome.out
When you run the file, the output should be:
Welcome to UniMAP
CONGRATULATIONS…!!!! You are now able to write and run your first program
successfully.
3
EKT 120 – Introduction to Computer Programming 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. In general, C statement is case sensitive Ans: True
b. The name of the primary function of a C program must be „main‟ Ans: True
c. main ( ) { } is a complete and correct C program. Ans: True
2.2 Find error(s), if any, in each statement:
a. void main (void);
void main (void)
b. printf (“Do we need parentheses here?”);
c. Printf(“Is there any wrong here?”);
printf(“Is there anything wrong here?”);
d. printf(“Where do we need a blank space”)
printf(“Where do we need a blank space”);
2.3 Type, compile and run this program, correct any error(s) you may find and rewrite
the program.
main( )
{printf (“There is no class tomorrow” ); }
No error but can also be written as
main( )
{printf (“There is no class tomorrow”); }
2.4 Determine and correct any error(s) you may find in the following command.
ma in() PRINTF *, („What is wrong?‟);
main()
{
printf (“What is wrong?”);
}
4
EKT 120 – Introduction to Computer Programming Laboratory Module
2.5 Type the following program.
#include <stdio.h>
void main (void)
{
int dA;
float fB;
A = 2;
B = 3.0;
printf(“Learning Basic Structure of C Language”);
printf(“dA + fB = %f”, dA+fB);
}
a. Compile the above program and write down the comment given on the screen
after compiling.
refer to screen..
b. Compile the above program to specify the output (binary) to a filename
“basic.out” and then run the output file. Write the command that you use to
complete the above instruction
To compile: gcc -o basic.out „yourfilename’.c or
gcc „yourfilename‟.c -o basic.out
To run: ./basic.out
c. Write down the output of the program.
Learning Basic Structure of C LanguageA + B = 5.000000
d. Format the floating number into 8 numbers and 2 floating point. Write down the
formatting format you use.
printf(“\nA + B = %10.2f”, dA+fB);
e. Correct the program so that the output looks presentable. Describe the changes
you have done and the output of the program.
i. Write down the changes done to the program:
#include <stdio.h>
void main (void)
{
int dA;
float fB;
dA = 2;
fB = 3.0;
printf(“\nLearning Basic Structure of C Language”);
printf(“\nA + B = %10.2f\n”, dA+fB);
}
5
EKT 120 – Introduction to Computer Programming Laboratory Module
ii. What is the output of the program?
Learning Basic Structure of C Language
A+B= 5.00
f. Change the first “void” to “int”, then compile the program. Write down any
message after compiling.
refer to screen..
g. Explain the program in your own word and describe the command from top until
end of the program.