Introduction to Microsoft Visual C++
Microsoft Visual C++ (often abbreviated as MSVC or VC++) is a commercial integrated
development environment (IDE) product engineered by Microsoft for the C and C++ programming
languages Visual C++ provides a po!erful and fle"ible development environment for creating Microsoft
#indo!s based applications
$he Visual C++ Integrated Development Environment (IDE)
Programming Process
$he first step in the process of building a program is to !rite the Source File or Code % source
file is any collection of C++ statements or declarations !ritten by a programmer #hen you invo&e the
compiler' the preprocessor runs first to create the compiler input $he Compiler creates an ob(ect file
that contains machine code' lin&er directives' e"ternal references' and function and data names
generated from the source files )inally' the Linker combines all of the ob(ect code from libraries and
other ob(ect files' resolves the named resources' and creates an Executable file that the computer can
directly e"ecute to perform various functions or operations
Tpes of Errors
Sntax Errors * % synta" error occurs in code !hen a statement is constructed incorrectly
E"amples of synta" errors include an incorrectly typed &ey!ord' omission of re+uired
punctuation' or an incorrect construct
!un"Time Errors , %n error that occurs !hile the application is running and the statement
attempts an operation that is impossible to carry out such as division by -ero or accessing a file
that does not e"ist
Logic Errors * $he third type of error is the logic error .ogic errors occur !hen your code does
not perform the !ay that you intended $hese errors can be especially difficult to find because an
application can run !ithout performing any invalid operations and still produce incorrect results
$he !ay to verify that your application does not have logic errors is to run test data through the
program and analy-e the results .ogic errors' li&e run*time errors' can occur !hen the user is
interacting !ith the application
Source
)ile
C++
Compiler
C++
.in&er
/b(ect
)ile
E"ecutable
)ile
.in& Compile E"ecute0
#rite
1rogram
#orking $it% Microsoft Visual C++
Launc%ing Microsoft Visual C++ &'(
2 $o run Microsoft Visual C++' clic&ing the follo!ing item from the tas&bar3
Start Programs Microsoft Visual Studio 6.0 Microsoft Visual C++ 6.0
Pro)ect #orkspace
In Microsoft Visual C++' the 1ro(ect #or&space is a container for your development pro(ects and
it is used to vie! and access the pro(ect4s various elements #hen you create a ne! pro(ect' a
!or&space file is created !ith a 5ds!6 e"tension
To create a ne$ Pro)ect #orkspace *#in+, Console -pplication.
/' /n the menu bar' clic&3 File New $he 5New form !ill be displayed as sho!n
belo!
,' Clic& Win32 Console !!lication from the pro(ects tab
+' $ype the pro(ect name under the Pro"ect name entry
0' $ype the location of the pro(ect under the #ocation entry
1' Clic& the 2k button $he 5Win32 Console Application6 form !ill be displayed as sho!n
belo!
&' Clic& sim!le a!!lication option button
3' Clic& the Finis$ button $he creation of the ne! pro(ect !ill be confirmed by the 5New
Project Inormation6 form as sho!n belo!
4' Clic& the 2k button and the ne! pro(ect !ill be created
To sa5e a Pro)ect #orkspace
/' #hile the pro(ect is still open' clic& the Sa5e -ll icon and all pro(ect related
files and resources !ill be saved
To close a Pro)ect #orkspace
/' /n the menu bar' clic&3 File Close Wor%s!ace and all pro(ect related files and
resources !ill be closed
To open an existing Pro)ect #orkspace
/' 7un the Microsoft Visual C++ program
,' /n the menu bar' clic&3 File &!en Wor%s!ace and 5!pen Wor"space6 form !ill be
displayed as sho!n belo!
+' /n the 5!pen Wor"space6 form' select the !or&space file of the pro(ect you !ant to
open
0' Clic& the 2k button and the pro(ect !ill be opened
To execute a Pro)ect #orkspace
/' #hile the pro(ect is still open' clic& the Execute Program icon or press the
shortcut &ey (ctrl+F1) and the confirmation form !ill be displayed as sho!n belo!
,' Clic& the 6es button on the displayed form and the pro(ect !ill undergo the
5Compiling6 and 5#in"ing6 process as sho!n belo! If there is no synta" error
encountered during these processes' Microsoft Visual C++ !ill create an e"ecutable
program or file of the pro(ect and e"ecute it
If there is a synta" error' the rest of the processes !ill be stopped and the defective
code !ill be reported $he pro(ect source code must be revised by the programmer
until all errors are eliminated
Creating a simple Visual C++ program
/' Create a ne! #in89 Console %pplication pro(ect !or&space and name it 5Sample :26 (see3
To create a ne$ Pro)ect #orkspace)
,' /n the 5Class$iew6 tab' double clic& the follo!ing se+uence (-7C) as sho!n belo!
+' $he initial source code of the pro(ect !ill be displayed as sho!n as Code % belo! 7etype the
source code as sho!n on Code ; belo!
Code %
Code ;
0' E"ecute the program (see3 To execute a Pro)ect #orkspace) If everything goes !ell !ithout
an error' you should see the program output as sho!n belo!
1' 1ress any &ey to terminate the program
-
C
7
// Sample 01.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
main(int argc, char arg!"#$
%
printf ("&estern 'nstitute of (echnology )n"$*
printf ("+y first ,-.. program. )n)n)n"$*
return 0*
/
// Sample 01.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int main(int argc, char arg!"#$
%
return 0*
/
-natom of a Visual C++ program
Comments " is a se+uence of single line characters that begins !ith t!o for!ard slashes (//) and is
ignored by the compiler % comment is usually used to document program source code and can be
!ritten at any part of the code
Synta"3
// (his line 0ill 1e ignored...
8eader Files , <eader files commonly contain for!ard declarations of classes' subroutines' variables'
and other identifiers
#include * directive tells the preprocessor to treat the contents of a specified file as if those
contents had appeared in the source program at the point !here the directive appears
Synta"3
#include "filename"
stdafx.h , a header file generated by Microsoft Visual Studio IDE that describes both standard
system and pro(ect specific include files that are used fre+uently but are changed infre+uently
$he %)= in stdaf"h stands for 5%pplication )rame!or& e=tensions6 $his header file is included
by default
stdio.h * !hich stands for >standard input?output header>' is the header in the C standard
library that contains macro definitions' constants' and declarations of functions and types used for
various standard input and output operations
Program 7od , part of the code !here program statements that are to be e"ecuted are !ritten
main , a special function that mar&s the beginning and end of program e"ecution % C or C++
program must have one function named main $he program statements must be enclosed
bet!een a pair of curly braces %/
Synta"3
main(int argc, char arg!" #$
%
program2statements
/
argc * %n integer specifying ho! many arguments are passed to the program from the
command line ;ecause the program name is considered an argument' argc is at least 2
argv * %n array of null*terminated strings It can be declared as an array of pointers to
char (char @argvA B) $he first string *argvA:B. is the program name' and each follo!ing
string is an argument passed to the program from the command line $he last pointer
*argvAargcB. is CD..
9ote3 Dnless you are creating program that accepts arguments or data from the
command line' the values of argc and argv can be ignored
// Sample 01.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
main(int argc, char arg!"#$
%
printf ("&estern 'nstitute of (echnology )n"$*
printf ("+y first ,-.. program. )n)n)n"$*
return 0*
/
Comments
<eader
)iles
1rogram
;ody
printf , declared in header file stdioh' this function displays formatted strings to the screen
#ithout the statement #include "stdio.h"' printf !ill not be recogni-ed by the compiler and
an error !ill be reported
Synta"3
printf(string$*
E"ample3
printf ("&estern 'nstitute of (echnology )n"$*
string , a se+uence of characters enclosed in double +uotation mar&
9e$ line *\n. , used in string outputs' puts the cursor on the ne"t line of its current
position
Semicolon*;. : indicates the end of a function or instruction statement
return 0; , this statement terminates the e"ecution of a function and returns control to the
calling function E"ecuting this statement inside the main function !ill terminate the !hole
program
Synta"3
return 0*
-cti5it (/; String 2utput
Ma&e a program in Microsoft Visual C++ that !ill display the output as sho!n belo! 7eplace the
string 5"3our complete name here#6 !ith your o!n full name and the string 5"3our course
here#6 !ith the course you are enrolled in Save your pro(ect as 5%ctivity :26 in your designated folder
&estern 'nstitute of (echnology
4rogramming in +icrosoft ,isual -..
"3our complete name here#
"3our course here#
Variables
In computer programming' a 5ariable is a facility for storing data $he current value of the
variable is the data actually stored in the variable Depending on the programming language in +uestion'
the data stored in the variable can be intentionally altered during the program run' thus causing its value
to change
)undamental types of C++ variables are divided into t!o categories3 Integral and )loating
Integral types are capable of handling !hole numbers Floating types are capable of specifying values
that may have fractional parts
Tpe <escription Si=e !ange
Integral variables
char Character or small integer 2 byte *29E to 29F
short Short Integer 9 bytes *89FGE to 89FGF
int Integer H bytes *92HFHE8GHE to 92HFHE8GHF
long .ong integer H bytes *92HFHE8GHE to 92HFHE8GHF
)loating variables
float )loating point number H bytes +?* 8He +?* 8E (IF digits)
double Double precision floating point number E bytes +?* 2Fe +?* 8:E (I2J digits)
<eclaration of Variables
;efore a variable can be used in the program' it must be declared first $o declare a variable' the
variable type must be !ritten first' follo!ed by a space' then by a variable name or series of variable
names separated by commas' and then terminated by a semicolon
Synta"3
(ype 5ame61st, 5ame67nd,.. 5ame6nth*
E"ample3
int &idth* integer type variable named &idth
float 4erimeter* float type variable named 4erimeter
long 8, 3, 9* three long type variables named 8, 3 and 9
!ules of Variable 9aming
Variable names can consist of letters' digits' or underscores
Variable names cannot start !ith a digit or an underscore
Variable names should consist of 82 or fe!er characters to ensure portability
Variable names are case sensitive Capital letter 8 is entirely different from small letter x
Key!ords such as >int> and >float> cannot be used as variable names
Scope of Variables
% variable can be either of global or local scope % >lobal variable is a variable declared in the
main body of the source code' outside all functions It can be referred from any!here in the code' even
inside functions % Local variable is a variable declared !ithin the body of a function or a bloc& and it can
only be referred inside a function or a bloc& !here it !as declared Dsing it outside the bloc& !here it
!as declared !ill result into an error
#include "stdafx.h"
#include "stdio.h"
char Sides*
short ,alue61, ,alue67*
main(int argc, char arg!"#$
%
int :ase, ;eight*
float <rea*
return 0*
/
Llobal Variables
.ocal Variables
Initiali=ation of Variables
#hen declaring a regular local variable' its value is by default undetermined ;ut you may !ant a
variable to store a concrete value at the same moment that it is declared In order to do that' you can
initiali-e the variable $o initiali-e a variable' append an e+ual sign to the variable name during its
declaration and follo! it !ith the initial value
Synta"3
(ype 5ame61st=!alue61, 5ame67nd=!alue67,.. 5ame6nth=!alue6n*
E"ample3
int ;eight=>?* Variable &idth is e@ual to >?
float Arade=7.7?, <!e=B0.?* Variable Arade is e+ual to 99J and
<!e is e+ual to E:J
long <=>00, :, -=C00000* Variable < is e+ual to 8::' - is
e+ual to F::':::' and : remains
undetermined
<isplaing 5ariable 5alues using printf function
%side from displaying strings' printf can also be used to display the formatted values of one or
more variables
Synta"3
printf(Dfs61st fs67nd... fs6nthE, 5ame61st, 5ame67nd,...5ame6nth$*
Format specifications * al!ays begin !ith a percent sign (?) and are read left to right #hen
printf encounters the first format specification (if any)' it converts the value of the first argument
after format and outputs it accordingly $he second format specification causes the second
argument to be converted and output' and so on
Synta"3
F"0idth#".precision#type
width , (/ptional) $he number that specifies the minimum number of characters
output
precision , (/ptional) $he precision specifies the number of digits to be printed
after the decimal point $he last printed digit is rounded.
Type * 7e+uired character that determines !hether the associated argument is
interpreted as a character' a string' or a number
Format Tpe Variable Tpe 2utput Format
c int Specifies a single*byte character
d or i int Signed decimal integer
8 or 8 int Dppercase or .o!ercase <e"adecimal
f dou1le
Signed value having one or more decimal
digits after the decimal point
s string Specifies a single*byte,character string
2st Variable
9nd Variable
nth Variable
)ormat specification of
2st Variable
)ormat specification of
9nd Variable
)ormat specification of
nth Variable
E"ample3
/utput3
-ssigning 5alues to 5ariables using scanf function
scanf , declared in header file stdioh' this function reads data from the &eyboard entry and
!rites the data into a target variable specified by a pointer(M) $he type of variable must
correspond to the type specification used in scanf
& * % pointer indicates the address of a variable from the memory It is usually used to direct the
entered data from scanf function into a target variable
Synta"3
scanf(DGormatSpecificationE, H,aria1le5ame$*
E"ample3
#include "stdafx.h"
#include "stdio.h"
int main(int argc, char arg!"#$
%
char <ge=70*
int &idth=1?0, ;eight=I>0, ,alue=CJ, 8=BBBBB*
float +oney=J.?*
dou1le pi=>.1I1?J7K?>?*
printf(";e is Fi years old.)n", <ge$*
printf(";e has F0.7f pesos in his pocLet.)n)n", +oney$*
printf("(he rectangle is Fi 1y Fi units.)n", &idth, ;eight$*
printf("Fi in decimal is F8 in hexadecimal.)n)n", ,alue, ,alue$*
printf("&idth Mffects: ==F>i==F?i==FCi==FJi==)n", 8, 8, 8, 8$*
printf("4recision Mffects: F0.>f F0.Kf F0.Jf F0.17f)n)n", pi, pi, pi, pi$*
return 0*
/
;e is 70 years old.
;e has J.?0 pesos in his pocLet.
(he rectangle is 1?0 1y I>0 units.
CJ in decimal is IG in hexadecimal.
&idth Mffects: ==BBBBB==BBBBB== BBBBB== BBBBB==
4recision Mffects: >.1I7 >.1I1?J> >.1I1?J7K?I >.1I1?J7K?>?00
4ress any Ley to continue
$arget Variable 1ointer )ormat specification of the $arget Variable
#include "stdafx.h"
#include "stdio.h"
int main(int argc, char arg!"#$
%
int <ge*
float ;eight*
printf(";o0 old are youN "$*
scanf("Fi", H<ge$*
printf(";o0 tall are you (feet$N "$*
scanf("Ff", H;eight$*
printf(")n)n3ou are F0.7f ft. tall and Fi years old.)n)n", ;eight, <ge$*
return 0*
/
<efined Constants
Nou can define your o!n names for constants that you use very often !ithout having to resort to
memory*consuming variables' simply by using the Odefine preprocessor directive
Synta"3
#define identifier !alue
E"ample3
#define 4' >.1I1?J
-rit%metic 2perators
In any language' there are arithmetic operators that are used to perform arithmetic operations
$hese are the follo!ing Microsoft Visual C++ arithmetic operators3
2perator 9ame 2perator Example !esult *-@/(A 7@0.
%ssignment = 8=<* 8=10
%ddition . 8=<.:* 8=1I
Subtraction 2 8=<2:* 8=K
Dnary Minus 2 8=2<* 8=210
Multiplication 8=<:* 8=I0
Division / 8=</:* 8=7.?
Modulus
(remainder)
F
8=<F:* 8=7
Cote3 % and ; must be integral variables
Increment
(plus 2)
..
8=..<* 8=11, <=11
8=<..* 8=10, <=11
Decrement
(minus 2)
22
8=22<* 8=J, <=J
8=<22* 8=10, <=J
In an e"pression !hich involves several operators' the order in !hich the operators are evaluated
depends on the priority given to them $he priority given to the operators is called precedence $he
operators such as ' /' F have higher precedence than the operators .' 2 $he operators ' / and F
have same precedence and operators . and 2 have same precedence )or e"ample' in the statement
!"#$P "#$ is evaluated before and the result of "#$ is added to
If the e"pression contains parentheses' then the natural order of e"ecution of the operators is
overridden by the parentheses $he parentheses are given more priority than the any of the operators
1arentheses force the operations to have higher precedence If the e"pression contains nested
parentheses' then the innermost parentheses are evaluated first )or e"ample' in the statement
%!"&#$P !" is evaluated before and the result of !" is multiplied to $
E"ample3
#include "stdafx.h"
#include "stdio.h"
#define 4' >.1I1?J
int main(int argc, char arg!"#$
%
float O, ;, S, ,*
printf(")n-3P'5DMO)n)nOadius: "$*
scanf("Ff", HO$*
printf(";eight: "$*
scanf("Ff", H;$*
S=74'O;.74'OO*
,=4'OO;*
printf(")nSurface <rea: F0.If
[email protected]", S$*
printf(")n,olume: F0.If cu.units)n)n", ,$*
return 0*
/