Vous êtes sur la page 1sur 25

What is C?

a general purpose programming language developed by Dennis Ritchie


can be used to create lists of instructions for a computer to follow a compiled language

What is C Programming?

C
Program Code

C
Compiler

Executable Program

human-readable form

translator

machine-readable form

What about C Compiler?


If you are using a UNIX machine, the C compiler is available for free. (It is called either "cc" or "gcc" and is available on the command line.) If you are a student, then the school will likely provide you with a commercial compiler. If you are working at home on a Windows machine, you may download a free C compiler or purchase a commercial compiler.

Evolutions of C
BCPL B C 1967 1970 1972 (Martin Richards) (Ken Thompson) (Dennis Ritchie)

ANSI C 1989

C++

1990

(Bjourne Stroustrup)

What is C++?
Stroustrup states that the purpose of C++ is to make writing good programs easier and more pleasant for the individual programmer.
Stroustrup added OOP (Object Oriented Programming) features to C without significantly changing the C component. Thus C++ is a "relative" (called a superset) of C, meaning that any valid C program is also a valid C++ program.

Versions of the C++ Language


ANSI C++
Borland C++ Code Warrior (Mac) Turbo C++ Visual C++ (Windows)

Etc.

All of these software packages enable you to create computer programs with C++, but they all implement the C++ language in a slightly different manner. In an attempt to maintain portability of both the C and C++ languages, the American National Standards Institute (ANSI) developed a standard of consistency for C and C++ programming.

Advantages of C++
Available on most machines Can get good performance Can get small size Can manage memory effectively Can control everything

Suitable for almost any type of program (from systems programs to applications)

Disadvantages of C++
Complicated!!!
40 operators, intricate precedence, pointers, etc. can control everything

many exceptions and special cases


tremendous libraries both standard, vendor specific, and available for purchase, but all are intricate

Aspects above can result in high cost, maintenance and reliability problems

Steps in Developing a C++ Program


Plan Edit Compile * Link * Load Run * * IDE reports (uses editor) (converts to binary) (resolves external references) (places into memory) (transfers control, executes) errors at this step; must fix them

C++ Trivia

Due to their power and ease of use, C and C++ were used in the programming of the special effects for Star Wars.

APPLICATIONS THAT CAN BE WRITTEN WITH C LANGUAGE


1. Operating systems. MS Windows, Linux, Unix, VxWorks, Nucleus Plus,VMS, MS-DOS, Vrtx, etc. 2. Controllers such as Tape drives, disk drives, laser printers, ink jet printers, vending machines, microwave ovens, industrial mills, mailing machines, VCRs, DVD players, airplane controllers, car controllers, etc. 3. Desktop applications (yes, another wide field) Video games, databases, database front-ends, spreadsheets, word processors, editors, image editors, architecture tools, schematic routers, assemblers, compilers, web & internet applications. 4. Scientific applications

THE MECHANICS OF CREATING A C++ PROGRAM Editor: accepts the typing of the

source code (or header file). Source file: the file that contains the program you prepared in the editor after you save it. (.cpp) Header file: header files such as iostream.h Preprocessor: performs preliminary operations on files before they are passed to the compiler. Compiler: translates the source code to machine language. Object code: the file containing the translated source code. (.obj) Linker: links the object file with additional code, such as the library codes. Executable code: the file containing the final product. (.exe)

How does a typical C program looks like?

First, a very simple example of a C program:


Example 1:

/* Sample C program that outputs the word Hello */ #include <stdio.h> void main(void) { printf(Hello\n); }
The program shown in the example above is what is referred to as a program source code. The source code is made up of a combination of letters, numbers and other symbols. The source code can be created by the programmer/user using any text editor.

How does a typical C++ program looks like?

First, a very simple example of a C++ program:


Example 1:

// Sample C++ program that outputs the word Hello World #include <iostream.h> int main () { cout << "Hello World!"; return 0; }

How does a typical C++ program looks like?


Example 2:

// Comments at the beginning: // your name // a description of the program // information pertinent to the program #include <iostream.h> #include <stdlib.h> int main(void) { system ("CLS"); cout<< "This is a literal print\n"; cout<< "Yea!"<<endl; return 0; }

The CODE:

Information about the code: COMPONENTS This is AcommentPROGRAM beginning OF a C++ line. All the lines with two slash signs (//) are considered // Comments at the comments and do not have any effect on the beginning: behavior of the program. They can be used by // your name the programmer to include short explanations // a description of the or observations within the source itself. In this program // information pertinent to case, the line is a brief description of what our the program program does. Preprocessor directives begin with # and tell the computer to find the filename that follows and read it in. The file iostream.h (Input/Output stream) is used by cout. The stdlib.h is used to clear the screen. Note: <iostream.h> should always be your first "header" file.

#include <iostream.h> #include <stdlib.h>

This code C++ PROGRAM COMPONENTS OF Abegins the actual program.

int main(void)

EVERY C++ program has a main( ) function. A function is a block of code that performs one or more actions. While functions are usually called within a program, main( ) is called automatically. You may see various adaptations of this line of code. While other adaptations are allowed, be sure to maintain "int" as the return type because every ANSI-compliant C++ program will return zero to the IDE. Between the French curly braces is the body of function main( ). It is indented and terminates by returning 0 to the IDE. This body: * clears the screen, * prints Yea! to the screen. The semicolon character (;) signifies the end of the instruction and must be included after every instruction in any C++ program.

system ("CLS"); cout<< "This is a literal print.\n"; cout<<"Yea! return 0; }

INITIAL STEPS IN LOADING MICROSOFT VISUAL C++


Open the Software ( MS Visual C++) If you are working in a PC environment: 1. Click Start Menu 2. Select Programs 3. Select Visual Studio 4. Select MS Visual C++ 6.0 When the Tip of the Day dialog box appears, close it by clicking on the Close button. If you do not want to see the Tip of the Day every time you open Visual C++, click the Show tips at Startup check box to remove the check mark and deselect this feature.

PARTS OF A MICROSOFT C++ INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)


Microsoft Visual C++ offers an easyto-use GUI, graphical user interface. It contains the following: 1.Titlebar displays the project filename 2.Menubar contains the menu of C++ commands 3.Toolbars displays icons for the commands in the menu bar

4. Project Workspace Window displays the two kind of views: - the Class View and File View 5. Editor Window that will enable the programmer to write and edit codes. 6. Output Window will display the different kind of errors while the program is executed

WRITING C++ PROGRAM


Once you have created your project, it is time to start typing the source code for your program.

1.

Pull down the FILE menu

2. Select NEW 3. Select the FILE tab 4. Select C++ Source File 5. Enter a file name Your project name and your file name can be the same if you wish. Your source code files will have the extension .cpp 6. Click OK

WRITING C++ PROGRAM


7. You will now see the three main windows of MS Visual C++. The Project Workspace (on the left) has two tabs named Class View and File View at the bottom. We will be using the File View tab to determine if all of the needed files are contained within a project. 8. You may start typing now.

WRITING C++ PROGRAM


// my first program in C++
#include <iostream.h> int main () { cout << "Hello World!"; return 0; }

CLOSING FILE
1. Pull down the FILE menu 2. Select CLOSE WORKSPACE to close the entire screen. 3. Answer YES to the dialog box. 4. Pull down the FILE menu and select EXIT to leave the software package. Choosing CLOSE only, will close only the program and not the project.

RUNNING A C++ PROGRAM


Once you have typed your source code, it is time to save your work and see if your program works properly. 1. Pull down FILE menu, select SAVE - or use the save icon 2. Pull down BUILD menu, select BUILD project.exe - or press F7, or choose the build icon 3. If an error message occurs, double click on the error message to get an arrow pointing to the spot in the program source code that caused the error. 4. Pull down the BUILD menu and select EXECUTE project.exe - or use execute icon 5. Press ALT and ENTER to see your display in a full black screen. 6. Press any key to close the black screen. Do NOT click the mouse on the underlying screen to close the black screen.

Hands-on # 1:
Create a simple C++ program that will display the following information: 1. Full Name 2. Level & Section 3. Birthdate 4. Address

Vous aimerez peut-être aussi