You are on page 1of 4

Birla Institute of Technology & Science Pilani,

Hyderabad Campus

Computer Programming (CS F111)

1st Semester 2022-23

Lab 1

C PROGRAM STRUCTURE AND EXECUTION FLOW

In this lab class, you will be introduced to the programming language called
C. Let’s write your 1st C program.
1) Opening the terminal: A terminal takes commands from the user as
input and executes them. You will use it all the time for executing
instructions. The first step is to open the terminal in your UNIX system.
After that, you can check your current directory by typing the following
command:

pwd
/home/lab/

2) Creating the C file: The next step is to create a new file in your current
directory. Type the following command. It creates a new C file from the
command line and opens it in gedit editor. Note the extension of “.c”.
This indicates that it is C file.

gedit firstprog.c

3) Write the program in the C file: Write the program (given at the end
of this list) in your editor.
4) Save and quit the file: Enter Ctrl+S and quit the file.

5) Compile the file: From the same directory where you saved the file,
you have to compile the program that you just wrote. For that, you need
something called a “compiler”. A compiler converts the instructions in
C to machine level language so that the computer can understand it.
For this lab, you will use the gcc compiler. gcc stands for GNU
Compiler Collection. The following command asks gcc compiler to
convert firstprog.c, which is a program written in C to
firstprog.out, a program in machine level language.

Machine
gccC
Level
Program
Program

gcc firstprog.c -o firstprog.out

 gcc – Invokes the GNU C compiler


 firstprog.c – Your C program file name (this contains your
source code)
 -o firstprog.out – Instructs C compiler to create the
executable with the name firstprog.out. If you don’t specify
“-o” and the name of the executable file, C compiler will create
an output executable with the name a.out.

You can also use an online gcc compiler:


https://www.onlinegdb.com/. It is useful in cases
when your system does not have a compiler
already installed.
6) Execute the file: Finally, execute firstprog.out (if no errors are
displayed in the above step). This step will execute firstprog.out to
display the final output.

./firstprog.out
Hello World!!!!!

#include<stdio.h> /* Include Header Files */


int main() /* main() is the entry point of
the program */
{
printf ("Hello World!!!!!");
return 0;
}

Note:
1. main() is the starting point to the program.
2. Each instruction in a C program is written as a separate statement,
ending with a semicolon.
3. Any command written as xx()is a function.
4. #include<stdio.h> includes predefined functions, like printf().
It should be added at the begining of every C program that you write. As
you write more complex programs, you will need to add more header
files, but that is a discussion for the future :)
5. Every function returns some value at the end. Here, our main function
returns 0 indicating that the program completed successfully.
6. The statements that appear enclosed in “/* */” are called comments.
The compiler ignores anything written between them.

You might also like