Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
LAB 1: STRUCTURE OF A C LANGAUGE PROGRAM
MCT-142L: Computer Programming-I Lab
OBJECTIVE:
This lab will introduce basic structure of a typical C Language Program. At the end of this lab, you should be
able to:
• Familiarize with C Language elements e.g., directives, comments, main function, etc.
• Realize the importance of the C Language Syntax
• Learn about the different Escape Sequences available in C Language
APPARATUS:
• Laptop\PC with following tools installed
o Visual Studio Code with C/C++ and Code Runner Extensions
o C/C++ mingw-w64 tools for Windows 10
A TYPICAL C LANGUAGE PROGRAM
Let’s consider a simple C Language program (see Example 1.1).
Example 1.1: A Typical C Language Program
1 /* Example_1_1.c: A Typical C Language Program
2 -----------------------------------------------------------------------
3 This example program describes basic structure of a C Language Program.
4 -----------------------------------------------------------------------
5 Written by Shujat Ali (
[email protected]) on 07-Sep-2021.
6 IDE: Visual Studio Code 1.60.0
7 C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */
8
9 #include<stdio.h>
10
11 int main()
12 {
13 printf("Hello World!");
14
15 return 0;
16 }
17
18 // End of Program
Program Output Hello World!
1|Page Computer Programming-I Lab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
• Lines 1-7: multi-line comments. It is a good program practices to add comments to your program.
• Line 9: is the C directive. This tells the compiler that during compilation you should investigate this
file for symbols not defined within the program. e.g. printf
• Line 11: declares the beginning of the body of the main part of the program. The main part of the
program is treated as any other function in C program. Every C program must have a main function.
It's the entry point to the program. This is what gets invoked when you run the program. Within the
curly brackets you write the code for the application you are developing.
• Line 12: { marks the start of main function.
• Line 13: is the start of the body of main function. printf is built-in C function for displaying messages
on computer screen. It is defined in C Language Standard Input/Output library i.e. stdio. Every valid
C Language Program statement ends with a semi-colon (;)
• Line 15: specifies what main function will return after executing. Any line of code in the function
after this return statement will not be executed.
• Line 16: } marks the end of main function.
• Line 18: single-line comment
Task 1.1: Understanding the C language Syntax
Write the Example 1.1 in a C Language source file in Visual Studio Code
and answers the following questions.
1. Remove the # in line 9 i.e. just write include<stdio.h>. Compile it and write down what message
you receive in the TERMINAL tab.
2. Now write back the # sign and remove semicolon at the end of the line 13 i.e.
printf("Hello World!"), run the program and write down what you get in TERMINAL tab.
3. Now write back the semicolon remove any of the bracket { or } (line 12 or 16), compile it and
write down what you observe in TERMINAL tab.
4. Put back the bracket and now change printf to Printf (line 13), run the program and write
down what you observe in TERMINAL tab.
2|Page Computer Programming-I Lab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
5. Write back printf (line 13) and now remove #include<stdio.h>, run the program and write
down what you observe in TERMINAL tab.
Write back the line 9, save and run the program. It must run successfully and display “Hello World!” in
TERMINAL Tab. From above 5 observations, you must have learnt that while writing a C Language program,
we need to follow some set of rules otherwise program will not run. These set of rules are called the Syntax
of C Language. We will talk about it later.
6. Now modify the Example 1.1 program such that it displays “I am learning Computer
Programming” in the TERMINAL tab when we run it. Write down the code snippet that will
accomplish the task.
7. What if we write the new message in two lines instead of single line. i.e.
printf("I am learning");
printf("Computer Programming");
Guess what will be displayed in TERMINAL tab. Run the program and check if you guess it
right.
8. Try to modify the 1st line with printf a little bit i.e.
printf("I am learning\n");
printf("Computer Programming");
Run the program and write down what you observe in TERMINAL tab.
The additional characters \n in printf("I am learning\n"); is called the escape sequence in C Language.
3|Page Computer Programming-I Lab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
ESCAPE SEQUENCE IN C
An Escape Sequence in C
Escape
language is a sequence of Meaning Description
Sequence
characters that doesn't \a Alarm or Beep Makes a sound from computer
represent itself when used \b Backspace Backs up one character position
inside string literal or \f Form Feed Advances to the next page
character. It is composed of \n New Line Takes the cursor to beginning of next line
two or more characters \r Carriage Return Moves to left margin
starting with backslash \. For \t Tab (Horizontal) Takes the cursor to the next tab stop
example: \n represents new \v Vertical Tab Performs a vertical tab
line. \\ Backslash Displays a backlash (\)
\' Single Quote Displays a single quotation mark (‘)
Table to the right shows the
\" Double Quote Displays a double quotation mark (“)
list of escape sequences
\? Question Mark Displays a question mark (?)
available in C Language.
\nnn Octal number
You can use any of these to
\xhh Hexadecimal number
format the output of a C
\0 Null Displays a null character
program.
Task 1.2: Practicing Escape Sequence
Add a new Task_1_2.c file in the current workspace in Visual Studio Code
and write a program that will display on following details shown on the
left column.
Name: “Shujat Ali” Your Name
Registration Number: 2024-MC-XX Your Reg. No.
Subject: Computer Programming-I (MCT-142)
A Blank line
I have understood the lab 1.
(Add two tabs at the start
Learning ‘C’ Language is fun. of the line)
Task 1.3: Practicing Escape Sequence
Repeat above task using just ONE printf statement.
4|Page Computer Programming-I Lab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
Task 1.4: Practicing Escape Sequence
Write a C program that will display a diamond (shown below:
____
/\__/\
/_/ \_\
\ \__/ /
\/__\/
Task 1.5: Practicing Escape Sequence
Repeat above task using just ONE printf statement.
5|Page Computer Programming-I Lab