Monday, January 25, 2010

Bahasa C dan C++

Dedicated to Dosen Dody Sanjaya

1. Pendahuluan


1.1. Sejarah C & C++
During the 60s, while computers were still in an early stage of development, many new programming languages appeared. Among them, ALGOL 60, was developed as an alternative to FORTRAN but taking from it some concepts of structured programming which would later inspire most procedural languages, such as CPL and its succesors (like C++). ALGOL 68 also influenced directly in the development of data types in C. Nevertheless ALGOL was an unspecific language and its abstraction made it little practical to solve most commercial tasks.

In 1963 the CPL (Combined Programming language) appeared with the idea of being more specific for concrete programming tasks of that time than ALGOL or FORTRAN. Nevertheless this same specificity made it a big language and, therefore, difficult to learn and implement.

In 1967, Martin Richards developed the BCPL (Basic Combined Programming Language), that signified a simplification of CPL but kept the most important features the language offered. Although it continued being an abstract and somewhat large language.

In 1970, Ken Thompson, immersed in the development of UNIX at Bell Labs, created the B language. It was a port of BCPL for a specific machine and system (DEC PDP-7 and UNIX), and was adapted to his particular taste and necessities. The final result was an even greater simplification of CPL, although dependent on the system. It had great limitations like it did not compile to executable code but threaded-code, which generates slower code in execution, and therefore was inadequate for the development of an operating system. Reason why from 1971, Denis Ritchie, from the Bell Labs team, began the development of a B compiler which, among other things, was able to generate executable code directly. This "New B", finally called C, introduced in addition, some other new concepts to the language like data types (char).

In 1973, Denis Ritchie, had developed the bases of C. The inclusion of types, its handling, as well as the improvement of arrays and pointers, along with later demonstrated capacity of portability without becoming a high-level language, contributed to the expansion of the C language. It was established with the book "The C Programming Language" by Brian Kernighan and Denis Ritchie, known as the White Book, and that served as de facto standard until the publication of formal ANSI standard (ANSI X3J11 committee) in 1989.

In 1980, Bjarne Stroustrup, from Bell labs, began the development of the C++ language, that would receive formally this name at the end of 1983, when its first manual was going to be published. In October 1985, the first commercial release of the language appeared as well as the first edition of the book "The C++ Programming Language" by Bjarne Stroustrup.

During the 80s the C++ language was being refined until it became a language with its own personality. All that with very few losses of compatibility with the code with C, and without resigning to its most important characteristics. In fact, the ANSI standard for the C language published in 1989 took good part of the contributions of C++ to structured programming.

From 1990 on, ANSI committee X3J16 began the development of a specific standard for C++. In the period elapsed until the publication of the standard in 1998, C++ lived a great expansion in its use and today is the preferred language to develop professional applications on all platforms.

source: http://www.cplusplus.com/info/history.html


2. Pembahasan
Kalian sudah mengenal bahasa pemrograman Pascal kan? Jadi adalah mudah untuk memahami bahasa pemrograman lain. Contohnya adalah bahasa C dan C++ ini. Untuk memulainya, mari kita lihat contoh program (yang sudah me-‘legenda’) Hello World:

Pascal:
begin
writeln(‘Hello, world.’);
end.

C:
#include <stdio.h>
int main()
{
printf(“Hello, world.\n”);
return 0;
}

C++:
#include <iostream>
int main()
{
std::cout << “Hello, world.\n”;
return 0;


2.1. Gambaran Umum Sebuah Program
Program Pascal;
{ini bagian ‘header’ program, berisi library-library yang diperlukan ataupun komentar-komentar ga jelas…}

uses crt;

{berikut adalah bagian deklarasi kostanta, type bentukan, dan variabel global yang akan digunakan}
const
        pi = 3.1415926;
        {…;}
type
        angka : integer;
        {…;}
var
         i,j : integer;
         {…;}

{berikut adalah bagian deklarasi fungsi-fungsi dan prosedur yang akan dipakai}
function kuadrat(x : integer) : longint;
begin
          kuadrat := x*x;
end;
procedure tukar(var a:integer, var b:integer);
var temp : integer;
begin
           temp := a;
           a := b;
           b := temp;
end;

{akhirnya, main program}
begin
            writeln(‘Selamat datang’);

Inilah Pascal yang selama ini kalian kenal. Sudah tidak asing kan dengan source code di atas? Coba bandingkan dengan source code C dan C++ di bawah ini.

/* C */
/*ini bagian ‘header’ program, berisi library-library yang diperlukan ataupun komentar-komentar ga jelas…*/
#include <stdio.h>
#include <string.h>
#include <math.h>
/* … */
/*berikut adalah bagian deklarasi kostanta, type bentukan, dan variabel global yang akan digunakan*/
const float pi = 3.1415926;
/* const …; */
typedef int angka;
/* typedef …; */
int i,j;
/* …; */
/*berikut adalah bagian deklarasi fungsi-fungsi dan prosedur yang akan dipakai*/
long int kuadrat(int x)
{
         return (x*x);
}
void tukar(int *a, int *b);
int temp;
{
         temp := *a;
         *a := *b;
         *b := temp;
}
/*akhirnya, main program*/
int main()
{
         printf(“Selamat datang!\n”);
         /* …; */
         return 0;
}


Beberapa hal yang perlu diingat mengenai bahasa C:
- Komentar dalam C menggunakan /* … */ dan bisa memuat beberapa baris sekaligus.
- Sebuah program dalam C harus memiliki fungsi bernama main(), fungsi ini adalah fungsi spesial karena di situlah main program kita.
- Dalam C sebenarnya tidak ada procedure, procedure didefinisikan sebagai function yang mengembalikan nilai void (void = sesuatu yang tidak ada).
- Setiap baris perintah harus diakhiri dengan titik koma ‘;’ kecuali yang diawali dengan kres ‘#’.
- Blok program diawali kurung-kurawal-buka ‘{’ dan diakhiri kurung-kurawal-tutup ‘}’.
- Bahasa C adalah bahasa yang case-sensitive, jadi perhatikan dalam penamaan variabel, type, maupun pemanggilan fungsi-fungsi yang sudah ada.
- Perintah terakhir di dalam main() yaitu return 0 digunakan untuk mengindikasikan bahwa program tersebut sukses berjalan. Pada beberapa kontes pemrograman, hal ini merupakan salah satu syarat diterimanya program. (Program yang tidak me-return exit code 0 dianggap mengalami runtime error.)

//C++:
//ini bagian ‘header’ program, berisi library-library yang
//diperlukan
#include <iostream>
#include <fstream>
#include <string>
//#include …
//…
//berikut adalah bagian deklarasi kostanta, type bentukan,
//dan variabel global yang akan digunakan
const float pi = 3.1415926;
const …;
typedef int angka;
typedef …;
int i,j;
//..;
//berikut adalah bagian deklarasi fungsi-fungsi
//dan prosedur yang akan dipakai
long int kuadrat(int x)
{
        return (x*x);
}
void tukar(int *a, int *b);
int temp;
{
         temp := *a;
         *a := *b;
         *b := temp;
}
//akhirnya, main program
int main()
{
         std::cout << “Selamat datang!” << endl;
         //…;
         return 0;
}


2.2. Type, Variabel, dan Konstanta
2.2.1. Type
Type data numerik bilangan bulat:

to be continued..

No comments:

Post a Comment