Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Department of Mechatronics and Control Engineering

University of Engineering and Technology Lahore

LAB 1: STRUCTURE OF A C LANGAUGE PROGRAM


MCT-242L: Computer Programming-I Lab (Fall-2022)
Registration No. 2022-MC-07

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 (engrshujatali@gmail.com) 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
#include<stdio.h>
9
10
int main()
11
{
12
printf("Hello World!");
13
14 return 0;
15 }
16
17 // End of Program
18

1|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Program Output Hello World!

 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: Write the Example 1.1 in a C Language source file in Visual Studio Code (with
workspace name Lab_1) and answers the following questions. (4 points)
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.

PS C:\c> cd "c:\c\" ; if ($?) { gcc anas.c -o bilal } ; if ($?) { .\anas }

anas.c:1:11: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token

1 | include <stdio.h>

| ^

PS C:\c>

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.

PS C:\c> cd "c:\c\" ; if ($?) { gcc anas.c -o anas } ; if ($?) { .\anas }

anas.c: In function 'main':

anas.c:5:27: error: expected ';' before 'return'

5| printf("Hello World!")

| ^

2|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

| ;

6|

7| return 0;

| ~~~~~~

PS C:\c>

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.

PS C:\c> cd "c:\c\" ; if ($?) { gcc anas.c -o anas } ; if ($?) { .\anas }

Anas.c: In function 'main':

Anas.c:5:5: error: expected declaration specifiers before 'printf'

5| printf("Hello World!");

| ^~~~~~

Anas.c:7:5: error: expected declaration specifiers before 'return'

7| return 0;

| ^~~~~~

anas.c:8:1: error: expected declaration specifiers before '}' token

8|}

|^

Anas.c:9: error: expected '{' at end of input

PS C:\c>

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.
PS C:\c> cd "c:\c\" ; if ($?) { gcc anas.c -o anas } ; if ($?) { .\anas }
anas.c: In function 'main':
anas.c:5:5: warning: implicit declaration of function 'Printf'; did you mean 'printf'? [-Wimplicit-
function-declaration]
5 | Printf("Hello World!");
| ^~~~~~

3|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

| printf
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/
bin/ld.exe: C:\Users\Anas\AppData\Local\Temp\ccipSZsk.o:bilal.c:(.text+0x18): undefined
reference to `Printf'
collect2.exe: error: ld returned 1 exit status
PS C:\c>

5. Write back printf (line 13) and now remove #include<stdio.h>, run the program and write
down what you observe in TERMINAL tab.
PS C:\c> cd "c:\c\" ; if ($?) { gcc anas.c -o anas} ; if ($?) { .\anas }
Anas.c: In function 'main':
anas.c:5:5: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
5 | printf("Hello World!");
| ^~~~~~
Anas.c:1:1: note: include '<stdio.h>' or provide a declaration of 'printf'
+++ |+#include <stdio.h>
1|
Anas.c:5:5: warning: incompatible implicit declaration of built-in function 'printf' [-Wbuiltin-
declaration-mismatch]
5 | printf("Hello World!");
| ^~~~~~
anas.c:5:5: note: include '<stdio.h>' or provide a declaration of 'printf'
Hello World!
PS C:\c>

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.

PS C:\c> cd "c:\c\" ; if ($?) { gcc anas.c -o anas } ; if ($?) { .\anas}

I am learning Computer Programming

PS C:\c>

4|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

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.

PS C:\c> cd "c:\c\" ; if ($?) { gcc anas.c -o anas } ; if ($?) { .\anas}

I am learning Computer Programming

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.

PS C:\c> cd "c:\c\" ; if ($?) { gcc anas.c -o anas } ; if ($?) { .\anas }

I am learning

Computer Programming

The additional characters \n in printf("I am learning\n"); is called the escape sequence in C Language.

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 \. \t Tab (Horizontal) Takes the cursor to the next tab stop
For example: \n represents \v Vertical Tab Performs a vertical tab
new 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.

5|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Task 1-2: Add a new Task_1_2.c file in the current workspace (Lab_1) in Visual Studio Code
and write a program that will display on TERMINAL tab on successful compiling. (4 points)
Name: “abc” (You must display your name)

Registration Number: 2021-MC-XX (You must display your roll number)

Subject: Computer Programming-I (MCT-242)

(A blank line)

I have understood the lab 1.

Learning ‘C’ Language is fun. (Add two tabs at the start of the line)

Answer: # include<stdio.h> int main() { printf("Name: Anas Gulzar\nRoll Number:


2022-MC-07\nSubject: Computer Programming-1 (MCt-242)\n\nI have understand the lab 1.\n\t\
tLearning 'C' Language is fun. "); return 0; }

Task 1-3: Add a new Task_1_3.c file in the current workspace (Lab_1) in Visual Studio Code
and write a program that will display a diamond (shown below) on TERMINAL tab on
successful compiling. (2 points)
____
/\__/\
/_/ \_\
\ \__/ /
\/__\/
Answer: # include<stdio.h> int main() { printf(" ___\n /\\_/\\\n/_/ \\_\\\n\\ \\
_/ /\n \\/_\\/ "); return 0; }

6|Page Computer Programming-I Lab

You might also like