0% found this document useful (0 votes)
15 views17 pages

Lab1 v2

The document outlines a laboratory exercise for the Computer Interfacing Module at Singapore Polytechnic, focusing on Visual Studio and C/C++ programming. It introduces key concepts of interfacing, programming structures, and provides step-by-step instructions for creating and running C/C++ programs using Microsoft Visual Studio 2013. The document also covers essential programming functions, input/output operations, and the importance of using local and global variables.

Uploaded by

racomes304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

Lab1 v2

The document outlines a laboratory exercise for the Computer Interfacing Module at Singapore Polytechnic, focusing on Visual Studio and C/C++ programming. It introduces key concepts of interfacing, programming structures, and provides step-by-step instructions for creating and running C/C++ programs using Microsoft Visual Studio 2013. The document also covers essential programming functions, input/output operations, and the importance of using local and global variables.

Uploaded by

racomes304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

School of Electrical & Electronic Engineering

Singapore Polytechnic

Computer Interfacing
Laboratory 1: Visual Studio and C/C++ Programming

1. Introduction
The Computer Interfacing Module introduces the concepts of using the computer
to “talk” to other devices so as to obtain or to transmit information. We call this
processing “interfacing”.

To start on our journey towards monitoring & controlling other devices, we must
first learn how to write programs that allow such operations. In this laboratory
exercise, we will familiarise ourselves with our programming tools, i.e. Microsoft
Visual C++ (VC++), learn some useful functions of C/C++ and revise the
selection upon test statement and loop statement that we have learned in
previous semesters so that we can apply these to our forthcoming laboratory
exercises.

ANSI-C Standard
When it became evident that the C programming language was becoming a very
popular language available on a wide range of computers, a group of concerned
individuals met to propose a standard set of rules for the use of the C
programming language. The group represented all sectors of the software
industry and after many meetings, and many preliminary drafts, they finally wrote
an accepted standard for the C language. It has been adopted by the American
National Standards Institute (ANSI), and by the International
Standard Organization (ISO).

2. Objective
This group of laboratory exercise will help you to get familiar with Microsoft Visual
C++ and to revise C programming language. At the end of this laboratory, you
should be able to:

• Use the VC++ to write/edit/execute/debug a program.


• Write program that uses the various program structures

3. Requirements

• Personal Computer with Microsoft Windows operating system


• Microsoft Visual Studio 2013
• USB thumb drive

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 1 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

The reason for choosing C/C++ Programming Language in our laboratory sessions:
It is widely used in industry; most software is written in C/C++ including operating
systems.

The reasons for choosing Microsoft Visual Studio as our compiler and environment:

• User friendly, as you can use all the common Windows application
commands you are familiar with, e.g. Ctrl+C (Copy), Ctrl+V (Paste), Ctrl+X
(Cut), Ctrl+F (Find), Ctrl+Z (Undo), Ctrl+Y (Redo), etc.

• It is widely used.

C++ (an OOP language) is an extension of C. All good C programs are also C++
programs, but the reverse is not true. The program written in C can be compiled using
C++ compiler, however, a program written in C++ (with class, object and other OOP
components) cannot be compiled using C compiler.

Important
Remember to bring along your USB thumb drive for all the laboratory sessions.

4. Creating Your First C/C++ Program in Visual Studio 2013

4.1 Starting Visual Studio

1. After successfully login, select start button at the bottom left-hand corner of the
screen:

start → Programs → Microsoft Visual Studio 2013 → Microsoft Visual Studio


2013

The above steps will start the Microsoft Visual Studio 2013 IDE (Integrated
Development Environment)

Alternatively, you may use the shortcut (shown below) on the desktop to start
the Microsoft Visual Studio 2013.

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 2 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

The following procedures will direct you to create a project and a simple C++ program.

a) From the start button, launch Microsoft Visual Studio 2013.

b) Open new project (Visual C++ > Win32)


Select Win32 Console Application.
Name your project

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 3 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

c) Click “Next” then Select an empty project and uncheck “Security Development
Lifecycle (SDL)”.

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 4 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

d) Create/Add a C++ file.

e) Right click on the source, add, new item

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 5 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

g) Create the C++ file.

h) Add the “inpout32.lib”

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 6 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

Type in the inpout32.lib;

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 7 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 8 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

i) Build solution.

j) Check the result of the build process

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 9 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

k) Run the program.

l) You should see the following output.

Congratulation! You have successfully run your first C++ program using Microsoft Visual
Studio 2013.

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 10 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

4.2 C/C++ Programming Exercises


You are to create a number of C programs (projects) for the rest of the laboratory
session. These programs are designed to serve as a revision on C programming. You
are to type the programs into the Visual Studio environment and run them. What you
need to enhance is to know how to use standard C library functions and
functions provided by C++.

4.2.1 printf() Function


To output variable values to the screen, we may use the printf() function as
follows: (Create LAB1b.cpp and add it to the project LAB1b.)

Listing 1.2 Source code for lab1b.cpp

#include <stdio.h>

void main()
{
int my_variable;

my_variable = 10;

printf(“my_variable(decimal) = %d\n”, my_variable);


printf(“my_variable(hex) = %x\n”, my_variable);
}
Note: % is known as a format specifier.
%d is used to display integer values in decimal number system.
%x is used to display an integer value in hexadecimal number system.
%c is used to display a character.
%s is used to display a string of text.

4.2.2 Escape Sequences


• Escape sequences are characters preceded by a backlash “\”.
• Escape sequences are used when you want to output special characters.

e.g. The newline escape sequence is “\n”

Create LAB1c.cpp and add it to project LAB1c

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 11 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

Listing 1.3 Source code for lab1c.cpp


#include <stdio.h>

void main()
{
char sex;
float height;

sex = ‘M’;
height = 1.5;
printf(“Sex is %c\n”, sex);
printf(“Height is %f\n”, height);
}

4.2.3 Keyboard Input – scanf() function


Scanf() allows user to input data from the keyboard and assign to a variable, as
illustrated below:

Listing 1.4 Source code for LAB1d.cpp

#include <stdio.h>

void main()
{
char sex;
float height;

printf(“\nPlease enter the sex (M or F): ”);


scanf(“%c”, &sex);

printf(“\nPlease enter the height: ”);


scanf(“%f”, &height);

printf(“\nSex is %c”, sex);


printf(“\nHeight is %f”, height);
}

When your program execution reaches the line containing scanf(), it stops and
waits for the user to input a value from the keyboard.
Other basic standard input functions are:
getchar() reads a character from keyboard
gets() reads a string of text from keyboard
Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 12 of 17
School of Electrical & Electronic Engineering
Singapore Polytechnic

4.2.4 Displaying Several Variables


The printf() function can display more than one variable at a time:

Listing 1.5 Source code for LAB1e.cpp


#include <stdio.h>

void main()
{
int num1, num2, sum;

printf(“\nWhat is the first number? ”);


scanf(“%d”, &num1);

printf(“\nWhat is the second number? ”);


scanf(“%d”, &num2);

sum = num1 + num2;


printf(“\nSum of %d + %d is %d\n”, num1, num2, sum);
}

4.2.5 Selection Based Upon Tests

Listing 1.6 Source code for LAB1f.cpp

#include <stdio.h>

void main()
{
int temperature;

printf(“\nEnter temperature: “);


scanf(“%d”, &temperature);

If (temperature <= 0)
printf(“Too cold!\n”);
else
if (temperature >= 100)
printf(“Too hot!\n”);
else
printf(“Ok.\n”);
}

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 13 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

4.2.6 Iterations

Listing 1.7 Source code for LAB1g.cpp


//This program prints out numbers from 0 to 9

#include <stdio.h>

void main()
{
int i;

for (i = 0; i < 10; i++)


printf(“ %d”, i);
}

Listing 1.8 Source code for LAB1h.cpp

//This program allows user to enter the age

#include <stdio.h>

void main()
{
int age;

printf(“\nPlease enter your age: “);


scanf(“%d”, &age);

while ( (age <= 0)||(age > 100) )


{
printf(“\nInvalid age.\n”);
printf(“\nPlease re-enter age. “);
scanf(“%d”, &age);
}
}

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 14 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

Listing 1.9 Source code for LAB1i.cpp


//This program finds average of a set of numbers

#include <stdio.h>

void main()
{
int count;
float number, sum, average;

count = 0;
sum = 0;

do
{
printf(“\nEnter Number (0 to terminate): “);
scanf(“%f”, &number);
if (number != 0)
{
sum = sum + number;
count = count +1;
}
} while (number !=0) ;

average = sum/count;
printf(“\nThe average of the %d numbers is %.2f\n”, count, average);
}

What is the difference between while and do-while loop?


______________________________________________________________

______________________________________________________________

______________________________________________________________

5. Functions
All programs written so far consisted of a single long function called main(). We
will now learn to break up our program into several small functions (modules)
which will be called (invoked) from main().

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 15 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

A global variable is a variable that is declared outside any function. It is also


called public variable or external variable.

A local variable is a variable that is declared inside a a particular function. It is


also called private variable or internal variable.

It is better to declare all your variables as local variables within the function
that need to access them.

Listing 1.10 Source code for LAB1j.cpp


//This program reads two numbers from the keyboard and prints
//out the sum

#include <stdio.h>

int num1, num2, sum; //global variables


void printsum(void); //function prototype

void main()
{
printf(“\nEnter the first number: “);
scanf(“%d”, &num1);

printf(“\nEnter the second number: “);


scanf(“%d”, &num2);

sum = num1 + num2;


printsum();
}

void printsum(void)
{
printf(“\nThe sum is %d\n”, sum);
return;
}

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 16 of 17


School of Electrical & Electronic Engineering
Singapore Polytechnic

Listing 1.11 Source code for LAB1k.cpp


//This program calculates average of two numbers

#include <stdio.h>

float average(float num1, float num2):

void main()
{
float num1, num2, avg;
printf(“\nEnter first number: “);
scanf(“%f”, &num1);
printf(“\nEnter second number: “);
scanf(“%f”, &num2);
avg = average(num1, num2);
printf(“\nThe average is %f”, avg);
}

float average(float num1, float num2)


{
float local_avg;
local_avg = (num1 + num2)/2;
return(local_avg);
}

Lab 1: Visual Studio AND C/C++ Programming, ver.2 Page 17 of 17

You might also like