Skip to content

Module 0: Introduction to Programming

James edited this page Oct 8, 2020 · 22 revisions

Table of Contents

IDE (Integrated Development Environment)

Before we start coding, we need an IDE to simplify the coding process. What is an IDE? To put it simply, an IDE or Integrated Development Environment is a code editor application that also provides a compiler. Here are some IDEs for the C language that are commonly used:

Introduction to the C Language

Statement

In a program, a statement is a command that instructs the computer to carry out a certain action, such as displaying something to the computer.

If we were to put it into an analogy, when we speak to someone, we use a language that we both understand, so that we can convey what we want to tell them. That's what a statement is. Conveying a message to someone is akin to a statement in a program.

"Hello, world."

Let's start by creating a simple program, displaying the iconic sentence "Hello, world!" to the screen (console). This is the code for program:

#include <stdio.h>  

int main()
{
    printf("Hello, world!\n");  
    return 0;  
}  

Copy the code above to your IDE, and then compile and run. The compilation process will result the following output.

Hello, world!

Program Structure

To explain how the above program works, let's divide the code into several parts.

Header File

The first line in the code is called the preprocessor directive. The preprocessor used in this case is '#include'

The '#include' preprocessor tells the preprocessor to insert the contents of another file, the header file <stdio.h> into the source code at the point where the #include directive is found. The <stdio.h> header file is a built-in C library that contains important functions needed by the program. In this case, the header file provides the 'printf()' function to display the sentence “Hello, world!”. Without the library, the program can't be compiled.

main() Function

The main() function in the code is used from the 3rd line until the 7th line.

int main()
{
    printf("Hello, world!\n");  
    return 0;  
}

In every C program, the main() function must always be there. This is because the execution of a code starts from the beginning of the main() function.

  • The 3rd line shows the name and the return type of the function. int is the type of return for the function nameed main(). Then the symbol '{' symbolizes the start of the main() function
  • Line 4 through 6 contains the body of the main()function
  • Line 8 contains the symbol '}' which denotes the end of the main() function. Essentially, the body of a function will always be in-between the '{}' symbols.

Whitespace

Dapat diperhatikan pada kode bahwa baris 2 dan 4 kosong (tidak terdapat karakter apapun). Ini disebut dengan whitespace. Whitespace adalah area kosong pada kode, dan biasanya dtujukan agar kode mudah dibaca.

Terdapat tiga jenis whitespace, yakni space, tab, dan new line. Baris 2 dan 4 adalah contoh dari new line.

Statement

Di dalam fungsi main(), terdapat dua statement yang ditunjukkan pada baris 5 dan 6. Sebagian besar statement diakhiri oleh tanda titik-koma (;).

printf("Hello, world!\n");  
return 0;  

Statement pada baris 5 menginstruksikan program untuk memanggil fungsi printf(). Fungsi printf() adalah fungsi yang tersedia dalam library header <stdio.h> dan digunakan untuk mencetak output pada konsol (layar). Dalam hal ini, fungsi printf() menerima argumen string bertuliskan “Hello, world!\n”. Tanda ‘\n’ dalam string tersebut merupakan karakter spesial yang berfungsi untuk mencetak new line.

Sedangkan statement pada baris 6 disebut dengan return statement. Perintah return 0 pada fungsi main() digunakan untuk mengakhiri program dan menandakan program tersebut sukses dieksekusi.

Komentar

Komentar (comment) adalah bagian dari program yang tidak akan dieksekusi. Komentar sangat berguna untuk mendeskripsikan program yang dibuat, misalnya saja untuk menjelaskan bagian dari kode agar mudah dipahami oleh programmer lainnya. Terdapat dua jenis komentar dalam bahasa C.

Komentar Single-Line

Seperti namanya, komentar single-line hanya bekerja pada satu baris. Komentar single-line diawali dengan simbol // . Semua karakter (pada satu baris) dibelakang simbol // akan diabaikan.

// Ini adalah komentar single-line  
  
// Fungsi untuk mencetak ke layar  
printf("HALO\n");  

Komentar Multi-Line

Sedangkan komentar multi-line dapat bekerja pada lebih dari satu baris. Pasangan simbol /* dan */ digunakan untuk membuat komentar multi-line. Semua karakter yang berada di antara dua simbol tersebut akan diabaikan.

/* 
Ini adalah komentar multi-line 
Semua yang berada di sini akan 
diabaikan 
*/  

< Kembali ke Daftar Isi

Clone this wiki locally