Monday, September 7, 2009

Posted by RonalDo

Sejarah C++




Tahun 1978, Brian W. Kerninghan & Dennis M. Ritchie dari AT & T Laboratories
mengembangkan bahasa B menjadi bahasa C. Bahasa B yang diciptakan oleh Ken Thompson
sebenarnya merupakan pengembangan dari bahasa BCPL ( Basic Combined Programming
Language ) yang diciptakan oleh Martin Richard.

Sejak tahun 1980, bahasa C banyak digunakan pemrogram di Eropa yang sebelumnya
menggunakan bahasa B dan BCPL. Dalam perkembangannya, bahasa C menjadi bahasa paling
populer diantara bahasa lainnya, seperti PASCAL, BASIC, FORTRAN.

Tahun 1989, dunia pemrograman C mengalami peristiwa penting dengan dikeluarkannya
standar bahasa C oleh American National Standards Institute (ANSI). Bahasa C yang
diciptakan Kerninghan & Ritchie kemudian dikenal dengan nama ANSI C.

Mulai awal tahun 1980, Bjarne Stroustrup dari AT & T Bell Laboratories mulai
mengembangkan bahasa C. Pada tahun 1985, lahirlah secara resmi bahasa baru hasil
pengembangan C yang dikenal dengan nama C++. Sebenarnya bahasa C++ mengalami dua tahap
evolusi. C++ yang pertama, dirilis oleh AT&T Laboratories, dinamakan cfront. C++ versi kuno
ini hanya berupa kompiler yang menterjemahkan C++ menjadi bahasa C.

Pada evolusi selanjutnya, Borland International Inc. mengembangkan kompiler C++ menjadi
sebuah kompiler yang mampu mengubah C++ langsung menjadi bahasa mesin (assembly). Sejak
evolusi ini, mulai tahun 1990 C++ menjadi bahasa berorientasi obyek yang digunakan oleh
sebagian besar pemrogram professional.



Struktur Bahasa C++



Contoh 1 : Hasil :


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



Hello World!


Sisi kiri merupakan source code, yang dapat diberi nama hiworld.cpp dan sisi kanan adalah
hasilnya setelah di-kompile dan di-eksekusi.



Program diatas merupakan salah satu program paling sederhana dalam C++, tetapi dalam
program tersebut mengandung komponen dasar yang selalu ada pada setiap pemrograman
C++. Jika dilihat satu persatu :

// my first program in C++
Baris ini adalah komentar. semua baris yang diawali dengan dua garis miring (//) akan
dianggap sebagai komentar dan tidak akan berpengaruh terhadap program. Dapat
digunakan oleh programmer untuk menyertakan penjelasan singkat atau observasi
yang terkait dengan program tersebut.
#include
Kalimat yang diawali dengan tanda (#) adalah are preprocessor directive. Bukan
merupakan baris kode yang dieksekusi, tetapi indikasi untuk kompiler. Dalam kasus ini

kalimat #include memberitahukan preprocessor kompiler untuk
menyertakan header file standard iostream. File spesifik ini juga termasuk library
deklarasi standard I/O pada C++ dan file ini disertakan karena fungsi-fungsinya akan
digunakan nanti dalam program.

int main ()

Baris ini mencocokan pada awal dari deklarasi fungsi main. fungsi main merupakan
titik awal dimana seluruh program C++ akan mulai dieksekusi. Diletakan diawal,
ditengah atau diakhir program, isi dari fungsi main akan selalu dieksekusi pertama
kali. Pada dasarnya, seluruh program C++ memiliki fungsi main.
main diikuti oleh sepasang tanda kurung () karena merupakan fungsi. pada C++, semua
fungsi diikuti oleh sepasang tanda kurung () dimana, dapat berisi argumen didalamnya.
Isi dari fungsi main selanjutnya akan mengikuti,berupa deklarasi formal dan
dituliskan diantara kurung kurawal ({}), seperti dalam contoh.

cout << "Hello World";

Intruksi ini merupakan hal yang paling penting dalam program contoh. cout merupakan
standard output stream dalam C++ (biasanya monitor). cout dideklarasikan dalam
header file iostream.h, sehingga agar dapat digunakan maka file ini harus disertakan.
Perhatikan setiap kalimat diakhiri dengan tanda semicolon (;). Karakter ini
menandakan akhir dari instruksi dan harus disertakan pada setiap akhir instruksi
pada program C++ manapun.

return 0;

Intruksi return menyebabkan fungsi main() berakhir dan mengembalikan kode yang
mengikuti instruksi tersebut, dalam kasus ini 0. Ini merupakan cara yang paling sering
digunakan untuk mengakhiri program.

Tidak semua baris pada program ini melakukan aksi. Ada baris yang hanya berisi komentar
(diawali //), baris yang berisi instruksi untuk preprocessor kompiler (Yang diawali
#),kemudian baris yang merupakan inisialisasi sebuah fungsi (dalam kasus ini, fungsi main)
dan baris yang berisi instruksi (seperti, cout <<), baris yang terakhir ini disertakan dalam
blok yang dibatasi oleh kurung kurawal ({}) dari fungsi main.

Posted by RonalDo

Preprocessor Directives

Preprocessing is a step that takes place before compilation that lets you:

* Replace tokens in the current file with specified replacement tokens.
* Imbed files within the current file
* Conditionally compile sections of the current file
* Generate diagnostic messages
* Change the line number of the next line of source and change the file name of the current file.

A token is a series of characters delimited by white space. The only white space allowed on a preprocessor directive is the space, horizontal tab, vertical tab, form feed, and comments. The new-line character can also separate preprocessor tokens.

The preprocessed source program file must be a valid C program.

Preprocessor directives begin with the # token followed by a preprocessor keyword. The # token must appear as the first character that is not white space on a line. The # is not part of the directive name and can be separated from the name with white spaces.

A preprocessor directive ends at the new-line character unless the last character of the line is the \ (backslash) character. If the \ character appears as the last character in the preprocessor line, the preprocessor interprets the \ and the new-line character as a continuation marker. The preprocessor deletes the \ (and the following new-line character) and splices the physical source lines into continuous logical lines.

Except for some #pragma directives, preprocessor directives can appear anywhere in a program

Posted by RonalDo

Preprocessor Directives

Preprocessor directives, such as #define and #ifdef, are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to perform specific actions. For example, the preprocessor can replace tokens in the text, insert the contents of other files into the source file, or suppress compilation of part of the file by removing sections of text. Preprocessor lines are recognized and carried out before macro expansion. Therefore, if a macro expands into something that looks like a preprocessor command, that command is not recognized by the preprocessor.

Preprocessor statements use the same character set as source file statements, with the exception that escape sequences are not supported. The character set used in preprocessor statements is the same as the execution character set. The preprocessor also recognizes negative character values.

The preprocessor recognizes the following directives:

The number sign (#) must be the first nonwhite-space character on the line containing the directive; white-space characters can appear between the number sign and the first letter of the directive. Some directives include arguments or values. Any text that follows a directive (except an argument or value that is part of the directive) must be preceded by the single-line comment delimiter (//) or enclosed in comment delimiters (/* */). Lines containing preprocessor directives can be continued by immediately preceding the end-of-line marker with a backslash (\).

Preprocessor directives can appear anywhere in a source file, but they apply only to the remainder of the source file.