The
C programming language was developed at AT&T for the purpose of writing the
operating system for the PDP-11 series of computers which ultimately became the
UNIX operating system. C was developed with the primary goal of operating
efficiency. Bjarne Stroustrup, also
of AT&T, developed C++ in order to add object oriented constructs to the C
language. Because object oriented technology was new at the time and all
existing implementations of object oriented languages were very slow and
inefficient, the primary goal of C++ was to maintain the efficiency of C.
C++
can be viewed as a traditional procedural language with some additional
constructs. Beginning with C, some constructs are added for object oriented
programming and some for improved procedural syntax. A well written C++ program
will reflect elements of both object oriented programming style and classic
procedural programming. C++ is actually an extendible language since we can
define new types in such a way that they act just like the predefined types
which are part of the standard language. C++ is designed for large scale
software development.
C++ syntax
By definition, syntax is the rules governing the
formation of statements in a programming language. In other words, it is the
specific language (code) you use to turn algorithms into programs and specific
rules you have to follow when programming with C++. As you will find out, if
you make one small mistake when writing code, the compiler will notify you of
the syntax error when you try to run it. C++, like all languages, is very picky
when it comes to syntax. Let's look at a simple example program written in C++.
// BEGIN PROGRAM
// Program to calculate the average of 4 integers
#include
<iostream.h> //
header file for input/output stream
#include
<conio.h> //
header file for standard input/output
int
main( )
{
int
number1, number2, number3,number4; // declaration of variables
float
average;
clrscr(); // Clear the output screen
cout
<< endl << endl;
cout
<< "Enter first integer: ";
cin
>> number1;
cout
<< "Enter second integer: ";
cin
>> number2;
cout
<< "Enter third integer: ";
cin
>> number3;
cout << "Enter third integer: ";
cin >> number4;
average
= (number1 + number2 + number3+number4) / 3.0; // calculates the average
cout
<< "The average of the three integers is: " << average
<< endl;
getch()
; // Wait for a character expression from
keyboard
return
0;
} //
end main( )
For now, notice how each executable statement in the program is
ended with a semi-colon. Every executable statement must always be terminated
with a semi-colon, which signals that it is the end of the statement. Any code
placed after // will not affect the code; // is used for comments and
documentation. Also notice that text is always enclosed in parentheses. Let's
dig deep into the code to find out why you need to use certain code statements.
cout
- console output - It is simply letting the compiler know that something is being sent into the output stream (normally the screen)<<
- insertion operator - cout and the insertion operator go hand and hand; you must use it whenever you issue a cout commandcin
- console input - It is simply letting the compiler know that something is being sent into the input stream (normally into the memory of a computer)>>
- extraction operator - cin and the extraction operator go hand and hand; you must use it whenver you issue a cin commandendl
- end line - basically the same as pressing the return key in a text editor; returns down to the next line
No comments:
Post a Comment