lan

English French German Spain Italian Dutch Russian Portuguese Japanese Korean Arabic Chinese Simplified
click the next picture To continue

Wednesday, 13 October 2010

Standard C++ Library Reference

Standard C++ Library Reference

C PROGRAMMING Considering that there are many feet of books on C at the UCSD bookstore,
it is unrealistic to attempt a comprehensive overview of the language in
part of a one-quarter course. Rather I hope to provide enough to get
you started writing programs and familiar enough with the general idea
that you will be able to pick up more skills as your programming experience
grows.
Why not C++?
(1) I don't know C++
(2) Numerical Recipies is available in C, not C++
Why not FORTRAN?
(1) More students now know C
(2) Some people tell me C is better
What if I already know C?
(1) You may not need to take most of this course
(2) But if you do, you can help other students (and me...)
What C textbook would you recommend?
I have only looked at a few books. Kernighan and Ritchie (KR) is a
standard of sort and is quite readable. But the programming
examples are computer-science oriented and not typical of scientific
applications. I also bought Brooks (C Programming: The Essentials
for Engineers and Scientists); this would be a good choice if you
have little programming experience. The examples are more typical
of geophysical programs than KR

Although I will try to make these notes self-contained and provide enough information to do the homework assignments, I advise you to buy or borrow a C book to use as a reference to fill in the many gaps.
To standardize things, I am going to assume that everyone has access to a Sun workstation and the Sun C compiler. I do not want to have to deal with PCs, CodeWarrier, and pull-down menu stuff. Let's keep it simple, OK?
--------------------------- HELLO WORLD ------------------------------------- The best way to learn a computer language is to start writing short
programs that work and then gradually add complexity. The traditional
first C program prints out "hello, world" and looks something like this:
(hello.c)
#include <stdio.h>
int main()
{
printf("hello, world\n"); return 0; }Let's examine this line by line: #include <stdio.h>

By itself, C is a quite low-level language that cannot do many of the
things that one expects a language to be able to do. To expand the
capabilities of the language many standard libraries are available
in C installations but they must first be loaded. This is done at
the beginning of the program. The <stdio.h> library facilitates input
and output and is the most commonly used library. As we will see
later, the <math.h> library is necessary for doing all but the most
elementary mathematics.
EVERY C PROGRAM THAT WE WRITE IN THIS CLASS WILL NEED <stdio.h> Note that this line does NOT end in a semi-colon. int main()
Every program in C is actually a series of functions. Every program must
have one function called "main" that tells the compiler where to start
executing. The "int" defines main as an integer function that will return
an integer value. The "()" provides space for the function arguments;
in this case there are none. Finally, the contents of the function are
included between the curly brackets, { and }. For clarity, it is conventional
to indent the contents of the function.
printf("hello, world\n");
This statement sends "hello, world\n" to the screen. The quotes serve to
define a character string. \n is C notation for a newline character so
that the cursor advances to the next line. If you leave out \n then
the cursor will remain at the end of the line. Thus, we could have written
printf("hello, ");
printf("world\n");
and we would get the same result. Forgetting the "\n" is a common mistake
for beginning programmers (at least it was for me).
to download this course click here

No comments:

Post a Comment