Hardware Reference
In-Depth Information
Alternatively, the prototype can be declared before the function is used:
/* Prototype declaration */
int cube(int x);
/* Main function */
void main()
{
int y;
y = cube(8);
}
/* Cube function */
int cube(int x)
{
return x*x*x;
}
A simple 'Hello, World!' program written in ANSI C would appear as follows:
/* Name: hello.c */
/* Language: ANSI C */
/* Output: Prints "Hello, World!" */
#include <stdio.h>
void main()
{
printf("Hello, World!"\n);
}
Whilst its equivalent in C ++ would be:
/* Name:
hello.cpp
*/
/* Language: C++
*/
/* Output:
Prints "Hello, World!"
*/
#include <iostream.h>
using namespace std;
int main()
{
cout << "Hello, World!\n");
return 0;
}
Note that the namespace is a group of definitions and the cout object is
defined within the standard, std , namespace in the iostream.h header file
( cout works in much the same way as printf which is defined in stdio.h ).
There are, of course, many more differences between C and C ++ but a detailed
explanation is beyond the scope of this topic.
As a further example of the use of C ++ the following program written in
Turbo C ++ for DOS performs the same function as the MASM32 program
( compare.asm ) shown on page 186. The output produced by the program is
shown in Figure 7.10.
// Name: compare.cpp
// Language: Turbo C++ 3.0
// Output:
Compares input value with 10
// Note:
Demonstrates branching based on
//
<, >, and == comparisons in C++
Search WWH ::




Custom Search