Saturday, October 2, 2010

C++ tutorial

Let us begin with a quick introduction in C. Our aim is to show the essential elements of the language in real programs, but without getting bogged down in details, rules, and exceptions. At this point,we are not trying to complete or even precise (save that examples are meant to be correct). We want to get you quickly as possible to the point where you can write useful programs and to do what we have to concentrate on the basics: variables, constants, arithmitic control flow,functions, and the rudiments of input and output. We are intensionally leaving out with chapter features of C that are important for writing bigger programs. This include pointers, structures most of C's rich set of operators several control-flow statements, and standard library.

1.1 Getting started

The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all language.
print the words:
hello, world
This is a big hurdle; to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered,everything else is comparatively easy.

In C, the program to print "hello world" is


Just how to run this program depends on the system you are using. As a specific examples, on the UNIX operating system you must create the program in a file whose name ends in ".c", such as hello.c, then compile with the command

cc hello.c

If you have botched anything,such as omittin a character or misspelling something, the compilation will proceed silently,and make an executable file called a.out. If you run a.out by typing the command

it will print

hello, world

On the other systems, the rules will be different;check with a local expert.
Now, for some explanations about the program itself. A C program, whatever its size, consists of "functions"and "variables". A function contains statements that specify the computing operations to be done, and variables store values used during the computation. C functions are like the subroutines and functions in Fortran or the procedures and functions of Pascal. Our example is a functions named "main". Normally you are at liberty to give functions whatever names you like,but "main" is special -your programs begin executing at the beginning of main. This means that every program must have a "main" somewhere

main will usually call other functions to help perform its job, some that you wrote, and others from libraries that are provided for you. The first line of the program.

tells the complier to include information about the standard input/output library; the line appears at the beginning of many C source files. The standard library is described in Chapter 7 and AppendixB.
One method of communicating data between functions is for the calling functions to provide a list of values called "arguments", to the function it calls. The parenthesis after the function name surround the argument list. In this example main is defined to be a function that expects no arguments, which is indicated by the empty list ().

#include include information about standard
main() define a function called main that received no argument
values statements of main are enclosed in braces main calls library function printf to print sequence of
character \n represent the newline character.

"The first C program"

The statements of a function are in closed in braces {}. The function main contains only one statement

printf("hello, world\n");

A function is called by naming it, followed by a parenthesized list of arguments so this called function of "printf" with the argument "hello, world\n". printf is a library function that prints output in this case the string of characters between the quotes.
A sequence of character in double quotes like "hello, world\n"); is called a character string or string constant. For the moment our only use of character will be as argument for printf and other function.
The sequence n\ in the string is C notation for the newline character, which when printed advance the output to the left margin on the next line. If leave out the n\(a wortwhile experiment), you will find that there is no line advance after the output is printed. You must you \n to include a newline character in the printf argument; if try something like

No comments:

Post a Comment