You are on page 1of 14

Programming in C

CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Contact Details

Email : dani.khattak@gmail.com, zilehuma11@gmail.com

General Guidelines for Lab usage


It is very important that you observe the following rules for the lab
usage. These rules are very important in order to maintain an
environment in the lab that is conducive to learning.
Please dont make noise in the lab. The lab is being used by a lot
of students at the same time. Please make sure that your
behavior does not become a source of inconvenience for
anybody else.
Please make sure that you have seated yourself in the lab before
it starts. Attendance is compulsory for all the students and
nobody will be allowed to enter the lab after 15 minutes.
In case you want to leave the lab, raise your hand and seek the
permission from the Instructors present in the lab. Only one
student is allowed outside the lab at a given time

Please turn off your cell phones during the lab. You are not
allowed to use your cell phone during the lab.
Use of internet is not allowed during lab hours unless explicitly
instructed by the instructor
Please follow the deadlines for your lab/assignment submissions.
No grades would be awarded in case of late submission.
Please make sure that you delete your code from the lab PC after
the lab is over, so that it cannot be misused by someone else.
Plagiarism would not be tolerated under any circumstances. You
will be given a zero in such cases and your case would be
forwarded to Disciplinary Committee (DC) for further action.

CASE Institute

Page 1 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

C Programming Environment
To program in a language, we need software called an IDE (Integrated
Development Environment). An IDE consists of an editor to edit code
and a compiler/linker to convert your code to executable machine code
and a debugger to find errors in a program. Additionally, it may have a
help system and other useful features.
The layout of C Programs:
The general form of a C program is as follows (don't worry about what
everything means at the moment - things will be explained later).
preprocessor directives
main()
{
local variables to function main ;
statements associated with function main ;
}
Listing 1.1
First Program - Hello World

#include<stdio.h>
As part of compilation, the C compiler runs a program called the C
preprocessor. The preprocessor is able to add and remove code from
your source file. In this case, the directive #include tells the
preprocessor to include code from the file stdio.h. This file contains
declarations for functions that the program needs to use. A declaration
for the printf function is in this file.
void main()
This statement declares the main function. C program can contain
many functions but must always have one main function. A function is
a self-contained module of code that can accomplish some task. The
"void" specifies there is no return type of main.

CASE Institute

Page 2 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

{
This opening bracket denotes the start of the main function.
printf("Hello World");
Printf is a function from a standard C library that is used to print strings
to the standard output, normally your screen. The compiler links code
from these standard libraries to the code you have written to produce
the final executable.
}
This closing bracket denotes the end of the main function.
Escape Sequences
Character combinations consisting of a backslash (\) followed by a
letter or by a combination of digits are called "escape sequences." To
represent a newline character, single quotation mark, or certain other
characters in a character constant, you must use escape sequences.
An escape sequence is regarded as a single character and is therefore
valid as a character constant. Escape sequences are used to format
our output.
The following escape sequences can be used to print out special
characters.
Escape
Sequence

Description

\n

Newline

\t

Horizontal
tab

\\

Backslash

\'

Single quote

\"

Double quote

\r

Carriage
return

Listing 1.2
CASE Institute

Page 3 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

This programs shows the use of Newline Escape Sequence (\n)

Output

This
is
a
test
She said, How are you?

Listing 1.3
This programs shows the use of Horizontal tab Escape Sequence (\t)

Output
This is a test

She said, How are you?

The gap between the sentences is due to Horizontal tab escape


sequence.
Listing 1.4
This programs shows the use of Newline Escape Sequence (\n) with
Double quote escape sequence (\)

CASE Institute

Page 4 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Output:
This is a test
She said, How are you?

The purpose of the above programs is to give you an idea that escape
sequences can be used in any combination to format your output.
Introduction to Microsoft Visual Studio 2010
How to create a small program in Visual studio 2010.
Step 1:
First of all click on Start Menu All Programs Microsoft Visual Studio
2010 Microsoft Visual Studio 2010
This would launch the following window

Step 2:
Go to File Menu New Project
This would launch the following window. Remember this step is very
important so you have to be careful. Click on the Windows Console
Application and then click on the project name and write Lab1_007.
Change the roll number according to your own roll number. Please
note that you cant create your projects in C drive. Use any
other drive except C. Then click OK.

CASE Institute

Page 5 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Step 3:
After clicking the OK in the step 2. You will see the following window.
click on Next. After that you will see another window.

CASE Institute

Page 6 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Step 4:
Now make sure that console application is selected and Empty project
is checked then click finish.

CASE Institute

Page 7 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Step 5:
Now the project is created. We add a source file by right clicking on
source files add new item

Step 6:
The following window appears. In this window write the name of your
source file and make sure c++ file is selected. Now click add.

CASE Institute

Page 8 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Step 7:
The following windows appear. Now rename the source file
Lab1_15.cpp to Lab1_15.c
Because we want to create a program in C.

CASE Institute

Page 9 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Step 8:
Write the following program in the window.

CASE Institute

Page 10 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Step 9:
Now go to Build Menu and click Build Solution ,and after that a go
to Debug menu and click on Start Debugging. You will see Hello
World!!! on a new black screen
Debugging
Debugging is the name given to the process of removing bugs (errors)
from computer programs.
What a Debugger Can Do?

Step through the program one statement at a time, either "over"


or "into" functions.
Run the program up to a certain point (either to the cursor or to a
"breakpoint") and then stop.
Show the values (or contents) of variables at each point during
the execution of the program.

In this lab we will use step over only to execute your code line by line.
Listing 1.5

CASE Institute

Page 11 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

First compile the above program and then Execute it, all the three
statements will be printed on the screen. Now again compile the file
and execute one statement at a time by clicking on Debug Step
Over (or F10). You will see that on the output screen (black screen) the
statements will appear one by one.
Comments:
Single line comments.
Multi line comments.
A more sophisticated program.

CASE Institute

Page 12 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Your tasks
Write programs that display the output shown in the following
diagrams.(use single printf statement).
Task 1
Create file (house.c)

Task 2
Create file (tree.c)

CASE Institute

Page 13 of 14

Programming in C
CS1001A
Lab 03: Programming, Debugging, Microsoft
Visual C++

Task 3
Make 1 shape of your choice.
Lab Task Submission Format:

Write the following things at the top of code file by using multi
line comments.
/* Name: xyz
Roll no: 15
Course: Programming in C (Lab 1). */

The format for the name of C file should be Lab 3_15.c (write
your roll number in place of 15 here)
Submit only the .c files and not the whole project.

****************************GOOD
LUCK********************************

CASE Institute

Page 14 of 14

You might also like