C basics: main, printf, and functions · Dasar C: main, printf, dan fungsi
C is a compiled language
- C is one of the oldest and most important languages. Linux, databases, and even Python are built with C.
- Unlike Python, C is compiled: the computer first turns your code into a program, then runs it.
- C is strict: every statement ends with a semicolon
;, and every variable needs a type.
C adalah bahasa yang dikompilasi
- C adalah salah satu bahasa tertua dan terpenting. Linux, basis data, bahkan Python dibangun menggunakan C.
- Berbeda dengan Python, C bersifat dikompilasi: komputer terlebih dahulu mengubah kode Anda menjadi program, lalu menjalankannya.
- C bersifat ketat: setiap statement diakhiri dengan titik koma
;, dan setiap variabel memerlukan tipe data.
The C skeleton
- Every C program starts in a function called main. Your code goes inside main.
#include <stdio.h>gives youprintf, which prints text.return 0;at the end ofmainmeans "the program finished without an error".
Rangka dasar C
- Setiap program C dimulai dalam fungsi bernama main. Kode Anda masuk di dalam main.
#include <stdio.h>memberikan Andaprintf, yang mencetak teks.return 0;di akhirmainberarti "program selesai tanpa kesalahan".
#include <stdio.h>
int main(void) {
printf("C runs from inside main.\n");
int year = 11;
printf("Grade %d\n", year);
return 0;
}
printf and types
printfprints text. The\ninside the text means "start a new line".- To print a value, use a placeholder:
%dfor anint,%ffor adouble,%cfor one character. printf("Grade %d\n", year);puts the value ofyearwhere the%dis.- Every variable needs a type first:
int year = 11;ordouble pi = 3.14;.
printf dan tipe data
printfmencetak teks.\ndi dalam teks berarti "mulai baris baru".- Untuk mencetak nilai, gunakan placeholder:
%duntukint,%funtukdouble,%cuntuk satu karakter. printf("Grade %d\n", year);memasukkan nilaiyearke tempat%d.- Setiap variabel memerlukan type terlebih dahulu:
int year = 11;ataudouble pi = 3.14;.
Common mistakes
- Every statement ends with
;, andmainreturnsint(return 0;). printfneeds the right format (%d,%f) and\nfor a new line.
Kesalahan umum
- Setiap statement diakhiri dengan
;, danmainmengembalikanint(return 0;). printfmemerlukan format yang tepat (%d,%f) dan\nuntuk baris baru.
Now you try
- Each task pre-fills the code you need. Write inside
main, or complete the function shown. - Some tasks ask for a function only — do not write your own
main; the checker adds one. - Press Run to compile and run, then Check answer. Your code compiles and runs on the server.
Sekarang Anda coba
- Setiap tugas mengisi prasetiap kode yang Anda butuhkan. Tulis di dalam
main, atau lengkapi fungsi yang ditampilkan. - Beberapa tugas meminta hanya function — jangan tulis sendiri
main; checker akan menambahkannya. - Tekan Run untuk mengkompilasi dan menjalankan, lalu Check answer. Kode Anda dikompilasi dan dijalankan di server.
Compile then run · Kompilasi lalu jalankan
C source is compiled to a program, which you then run. · Sumber C dikompilasi menjadi sebuah program, yang kemudian Anda jalankan.
Inside main, use printf(...) to print exactly Hello, C! on its own line. · Di dalam main, gunakan printf(...) untuk mencetak tepat Hello, C! pada baris tersendiri.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Complete the function int square(int n) so it returns n * n. The checker calls it with several values. Do not write a main — the checker provides one. · Lengkapi fungsi int square(int n) agar mengembalikan n * n. Pengecek memanggilnya dengan beberapa nilai. Jangan tulis main — pengecek menyediakannya.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Inside main, declare int grade = 11;. Then use printf to print two lines: first Hello!, then Grade 11 — print the number with a %d placeholder, not by typing 11. · Di dalam main, deklarasikan int grade = 11;. Kemudian gunakan printf untuk mencetak dua baris: pertama Hello!, lalu Grade 11 — cetak angka tersebut menggunakan placeholder %d, bukan dengan mengetik 11 secara langsung.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.