You are on page 1of 6

Tutorial 2

BETC 1313 Programming Fundamental


1) GCC compiler
GCC is one of many GNU compiler collection, which is used to compile the C
language code into an executable file. There are four discrete steps that gcc uses to
produce an executable file. They are:
 Preprocessing the C source file using the cpp program,
 Compiling the processed C source into assembler using the cc1back-end,
 Assembling the asm file into an object file using as, and finally
 Linking the object file with other archives/libraries to produce an executable
using the collect2 program, which is essentially a front-end to ld for simple
programs
In short, gcc compiler will translate a high-level programming language into a file
which can be executable by the machine.

2) Compiling C by using GCC


A C language code can be compiled by using the following command:
gcc <c file name> -o <executable file name>
Consider the file helloworld.c below:

a) Open the xcode application, choose create a new Xcode project


b) Choose Command Line Tool and click Next

c) A window asked to Choose options for your new project will appear. Write
your Product Name as helloworld and make sure Language use is C. Click
Next
d) You need to choose folder. Select Documents as your folder.

e) Copy the provided code above. Save your file. It will save as main.c.

f) Open a terminal. Make sure your current directory is in


/Users/comsys04/Documents/helloworld/helloworld

g) Type the following command to go to the current directory


cd Documents/helloworld/helloworld
Observe your current directory is now changed.

h) Type the following command to compile the code:


gcc main.c -o hello
Observe the message in the terminal to ensure that the compilation is
successfully completed.

i) Execute the executable file hello by type:


./hello
3) Exercise 1

a) In a new working directory, create a new file myfile.c and copy the
following code:

b) Compile and execute the code and observe the output.

c) Fix the errors in the code and recompile the code.

d) Observe the output and ensure that all the errors are fixed.

e) Can you tell what type of errors you’ve detected in the code before?

f) What happens if the executable file name is change to betc2018

4) Exercise 2

a) In a new working directory, create a new file exercise2.c and copy the
following code:
b) Compile and execute the code and observe the output.

c) Change the code into the following coding:

d) Compile and execute the code and observe the output.

e) Explain the differences.

5) Exercise 3

a) Create a new file exercise3.c and copy the following code:

b) Compile and execute the code and observe the output.

c) Does it give the correct output? If no, fixed the program to give the correct
one.
d) Explain the observation?

You might also like