Title: C and Intro to Microsoft Visual Studio
1C and Intro to Microsoft Visual Studio
- The University of Chicago
2Computational Finance with C
- TA Christopher Merrill
- cmerrill_at_math.uchicago.edu
- Office Hours Tue 6pm 730pm, Eckhart Basement
Computer Lab
3Course Outline
- In the first quarter well deal with the basics
of writing C programs in the Microsoft Visual
Studio 8.0 integrated environment (compiler,
editor and debugger). Well use a small subset
of the standard C library called the Standard
Template Library (STL). The instruction level is
set for students with little or no C
experience. - The second quarter is devoted to OOP, Object
Oriented Programming. We will design classes for
yield curve computations and put together a bond
analysis application. We will also cover the
Universal Modeling Language (UML) and discuss
design patterns. Students with sufficient C
experience should join this quarter. - The third quarter studies the QuantLib
quantitative finance class library
(http//www.quantlib.org). We will use this
library to write pricing models for a variety of
exotic derivatives.
4Suggested Reading List
- C Design Patterns and Derivatives Pricing, Mark
Joshi (2004) - Introduction to C for Financial Engineers An
Object-oriented Approach, Daniel Duffy (2006) - Accelerated C Practical Programming by
Example, Andrew Koenig (1999) - Numerical Recipes in C, Press, Flannery et al
(1992)
5Why C? Pros and Cons
- C is a large, complex and controversial
language. Its also the most common language of
choice for implementing high performance
production systems at investment banks, hedge
funds and smaller proprietary trading and
brokerage firms. - Its strength is also its weakness backward
compatibility with C, a very mature language
(1970) which was originally designed to do
low-level systems programming in Unix. C is
almost a superset of C (i.e., nearly all legal C
programs also are legal C programs). - Critics contend that C compatibility -- which was
a strong requirement for a lot of technical
reasons -- along with many other features added
to C later (templates, multiple inheritance,
function pointers, scoping rules, etc.) has
created accidental complexity. Such complexity
forces C developers to think about a lot of
things that are far removed from business logic
(pointers, memory management, arcane compiler
diagnostics, etc.) - Indeed, good C developers must adopt a
disciplined software engineering style this
normally happens with years of experience. - Advantages Dynamic range of the language
advanced compiler technology, compiles to native
code (performance!), and many sophisticated open
source libraries (numerics, string handling, I/O,
networking, etc.) - All of the above advantages are particularly
impacting in finance / trading applications. For
serious numerical work, interfacing with quote
feeds (which are often older C-style interfaces),
or developing trading / business logic, C is
still the best choice for an implementation
language.
6Why C? Alternatives
- Youll often hear that some newer languages are
better than C in theory. These discussions
are highly charged and subjective, with more
opinion than fact ( religious wars ) - Excel/VBA
- Very popular on the front desk of many trading
organizations. Can get a reasonably complex
application running very quickly, but the nature
of spreadsheets and VBA code is such that
applications quickly become unmaintainable. - Ideal as a prototyping tool.
- Matlab
- Same as Excel/VBA but for more mathematically-incl
ined traders and trading quants. - Ideal as a prototyping tool.
- C
- New kid on the block, quickly becoming the
language of choice for new development projects
under MS Windows. - Loosely based on C/C syntactically, but also
strongly tied to Microsofts massive .NET
framework. - Java
- Object-oriented, has garbarge-collection
(foolproof memory management), is
multithreaded, and cross-platform (write once
run anywhere in theory!). - Also is very very slow because the JVM doesnt
compile to native code (x20 times slower than
equivalent C/C code). - Functional programming languages (academics tend
to prefer these) - Lisp, Haskell, OCaml -- Microsoft even has one
now F. - In such languages, there is often a soft
distinction (if any) between data and code. - C is currently the all-purpose language of
choice at most financial firms, and this will
likely be the case for at least the next 10 years.
7(No Transcript)
8(No Transcript)
9(No Transcript)
10(No Transcript)
11(No Transcript)
12(No Transcript)
13include statement we need the functionality to
write output to the console, this functionality
is contained in the iostream library of
functions. The in front means it is a
preprocessor statement it tells the compiler to
look for functions in the iostream library and be
prepared to use them
- include
- using stdcout
- using stdendl
- int main()
- cout
-
- return 0
-
Statements always end with a
We want to use the functions cout and endl from
the iostream library, their full name is
stdcout and stdendl. In order for us to use
the shorthand notation cout and endl we tell the
program that we really mean stdcout and
stdendl
The function main(). This is where execution of
the program begins. Every executable C program
must contain the main function. It is declared to
return an integer and to not take any inputs
The body of the main() function is Contained
within a pair of braces, .. Any statements
contained within a pair of braces is called a
block.
This is the actual functionality of the program.
It takes the stringThis is the f, we can see
it is a string because it is enclose in quotation
marks and feeds it to the output function
cout, using the feed operator prints the string on the console window. The endl
function tells the cout function that there is no
more output here and to write the string and move
the cursor to the next line
The return value of the function main(). Since we
defined the function to output aninteger it has
to return a value. We just make itreturn the
integer 0
14- We could also have written this program
- include
- int main()
- stdcouturn 0
-
- Where instead of the using statements, we use
the full name stdcout and stdendl.
15- A third way is to tell the compiler that we want
to use short names for all the functions whose
full names start with std - include
-
- using namespace std
- int main()
- cout
-
- This has the disadvantage that other namespaces
may contain a cout function as well and we would
then have to be careful which of the two versions
we wanted to use. In large programs this can
become a problem.
16- Thus
- int x
- F(x)
- is valid but
- F(x)
- int x
- is not.
- In C variables can be declared right up to the
point they are they are used. Thus you dont have
to declare all your variables at the beginning of
a function as you do in some languages (e.g. C).
17- includestdendlint main() int num15 int
num27 num1num2 couteturn 0
Declare return type of the function main()as an
integer
Declare two variables of type int and initialize
themto 5 and 7 resp.
This computes the sum of the two numbers but
does not store it anywhere so we have no wayof
retrieving it later
Prints out the sum of 5 and 7
18Variables and Data Types
- Variables are locations in memory. The type of
variable determines how much memory is allocated
to the variable. - Every variable has to be declared before it can
be used. - The main data types well be using are
- double Based on IEEE floating point
specification. Use a double to represent most
real numbers (in the mathematical sense). - double d 3.1415927
- int Represents integers, uses integer
arithmetic. - int i 0
- bool represents true or false
- bool bSuccess true
- stdstring Represents a string of chars, e.g.
- stdstring s Financial Mathematics
19Integer and Floating Point Arithmetic
- Two classic examples
- What is j?
- int i 5/2
- int j i4
- What is the output?
- double x 0.01, sum 0.0
- for (int i 0 i
- sum x
- if (sum 1.0)
- stdcout 1
20(No Transcript)
21(No Transcript)
22What happens if we dont initialize the variables
It does compile, WITH warnings!
23When we try to run it we get after a couple of
error dialogs (which we dismiss by clicking
ignore)
24- What happens is, that the program uses whatever
happens to be in the memory locations allocated
to num1 and num2. This is almost certainly
something left over from a previous program --
garbage. - Moral always make sure that variables are not
only declared but also given a value
(initialized) before they are used.
Initialization can be done when they are declared
or later, but it has to be done before they are
used.
25Useful Debugging Commands
- F5 Run
- Runs your program in a debugger, without leaving
the Visual Studio environment. - Youll see a console come up because this is a
console application. You can print messages to
this console with stdcin and stdcout. - If the program needs to be compiled (i.e. if
youve made any changes to the code) the IDE will
prompt you to do so. - F6 Build
- Compiles your code changes.
- F9 Sets a breakpoint
- Suspends execution at the current line of code.
- F10 Single-step
- Allows you to run your code line-by-line, letting
you examine the contents of variables along the
way (very powerful and useful for learning).
26Homework
- Get the simple program we just described working.
Youll create a Visual C Win32 Console
Application, then perform the edit-compile-run
steps we just went through.