Vous êtes sur la page 1sur 6

EKT150 Introduction to Computer Programming

Laboratory Module

LAB 1 INTRODUCTION TO LINUX ENVIRONMENT AND C COMPILER

School of Computer and Communication Engineering Universiti Malaysia Perlis

EKT150 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 ls cd directory - command to check the current directory - to list down the files in the current 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
2

EKT150 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: 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. ./welcome.out

EKT150 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 Add statements in the program written in 1.3.1 to display the following output:
Welcome to UniMAP My name is Jane Doe. This is my first C program.

2.2 True or False: a. b.


c. In general, C statement is case sensitive Ans: The name of the primary function of a C program must be main Ans: main ( ) { } is a complete and correct C program. Ans:

2.3 Find error(s), if any, in each statement: a. b. void main (void); printf (Do we need parentheses here?); Printf(Is there any error here?); printf(Where do we need a blank space) ma in() PRINTF *, (What is wrong?);

c.
d.

e.

EKT150 Introduction to Computer Programming

Laboratory Module

2.5 Type the following program. #include <stdio.h> void main (void) { int A; float B; A = 2; B = 3.0; printf(Learning Basic Structure of C Language); printf(A + B = %f,A+B); }

a.

Compile the above program and write down the comment given on the screen after compiling.

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.

c. Write down the output of the program.

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. Write down the changes done to the program:

EKT150 Introduction to Computer Programming

Laboratory Module

ii. What is the output of the program?

f.

Change the first void to int, then compile the program. Write down any message after compiling.

g. Explain the program in your own words and describe the commands from top
until end of the program.

Vous aimerez peut-être aussi