Jumat, 31 Oktober 2025

ex0606.c nilai 99 88 77

/* ex0606.c */
#include <stdio.h>

int main()
{
    int nilai;

    nilai = 99;
    printf("Nilai ayu=%d\n", nilai);
    nilai = 88; /* variabel nilai digunakan ulang */
    printf("Nilai bayu=%d\n", nilai);
    nilai = 77;
    printf("Nilai charlie=%d\n", nilai);
    return(0);
}

output:
 
Nilai ayu=99
Nilai bayu=88
Nilai charlie=77

Process returned 0 (0x0)   execution time : 0.075 s
Press any key to continue.

Satu variabel yang nilainya sudah didefinisikan, bisa didefinisikan ulang di kesempatan lain.

ex0605.c start = 123

/* ex0605.c */
#include <stdio.h>

int main()
{
    int start = 123;
    printf("The starting value is %d\n", start);
    return(0);
}

output:
 
The starting value is 123

Process returned 0 (0x0)   execution time : 0.084 s
Press any key to continue.

Deklarasi isi variabel bisa dilakukan juga saat awal deklarasi tipe variabelnya, seperti contoh diatas.

ex0604.c int ayu, budi, charlie

/* ex0604.c */
#include <stdio.h>

int main()
{
    int ayu, budi, charlie;
    ayu = 100;
    budi = 90;
    charlie = 80;
    printf("nilai ayu=%d\nnilai budi=%d\nnilai charlie=%d\n",ayu,budi,charlie);
    return(0);
}

output:
 
nilai ayu=100
nilai budi=90
nilai charlie=80

Process returned 0 (0x0)   execution time : 0.081 s
Press any key to continue.

Penulisan tipe variabel bisa digabung, seperti contoh di atas.

Kamis, 30 Oktober 2025

ex0602.c printf chart int float double

/* ex0602.c */
#include <stdio.h>

int main()
{
    char c;
    int i;
    float f;
    double d;

    c = 'a';
    i = 1;
    f = 19.0;
    d = 20000.009;

    printf("%c\n", c);
    printf("%d\n", i);
    printf("%f\n", f);
    printf("%f\n", d);
    return(0);
}

output:
 
a
1
19.000000
20000.009000

Process returned 0 (0x0)   execution time : 0.115 s
Press any key to continue.

Rabu, 29 Oktober 2025

ex0601.c int x & tipe data di C

/* ex0601.c */
#include <stdio.h>

int main()
{
    int x; /* deklarasi variabel x */
    x = 5;
    printf("The value of x is %d. \n",x);
    return(0);
}

output:
 
The value of x is 5.

Process returned 0 (0x0)   execution time : 0.120 s
Press any key to continue.

int x adalah deklarasi variabel x dengan jenis variabel integer (bilangan bulat).


Ada 5 tipe data dalam bahasa pemrograman C:

1. char tipe variabel berisi 1 karakter saja
2. int tipe variabel integer (bilangan bulat)
3. float         tipe variabel float (bilangan pecahan)
4. double         tipe variabel bilangan sangat besar atau sangat kecil
5. void tipe variabel tanpa tipe data

Selasa, 28 Oktober 2025

ex0504.c %d & %f

/* ex0504.c */
#include <stdio.h>

int main()
{
    printf("%d/%d=%d\n",2,5,2/5);
    return(0);
}

output:

2/5=0

Process returned 0 (0x0)   execution time : 0.065 s
Press any key to continue.


2/5=0 (?) Apa yang salah? Seharusnya kan 2/5 = 0.4? Kita coba ubah variabel %d menjadi %f, dan ubah nomernya jadi float, dari 2 menjadi 2.0 misalnya. Menjadi seperti ini:

/* ex0504.c */
#include <stdio.h>

int main()
{
    printf("%1.1f/%1.1f=%1.1f\n",2.0,5.0,(2.0/5.0));
    return(0);
}

output:
 
2.0/5.0=0.4

Process returned 0 (0x0)   execution time : 0.087 s
Press any key to continue.

Sekarang sudah benar.

Senin, 27 Oktober 2025

ex0501.c %d %f integer and float

/* ex0501.c */
#include <stdio.h>

int main()
{
    printf("The value %d is an integer.\n", 986);
    printf("The value %f is a float.\n", 98.6);
    return(0);
}

output:
   
The value 986 is an integer.
The value 98.600000 is a float.

Process returned 0 (0x0)   execution time : 0.104 s
Press any key to continue.

Seperti yang sudah pernah ditulis, %d merujuk pada tipe file integer (desimal), dan %f merujuk pada tipe file float (pecahan).

Untuk menghilangkan angka 0 yang banyak dibelang koma pada tipe file float, kita bisa ubah penulisan %f misal menjadi %1.2f. Contoh:


/* ex0501.c */
#include <stdio.h>

int main()
{
    printf("The value %1.2f is a float.\n", 98.6);
    return(0);
}

output:
 
The value 98.60 is a float.

Process returned 0 (0x0)   execution time : 0.081 s
Press any key to continue.

Minggu, 26 Oktober 2025

ex0301.c return(1) & printf(%d\n, 4*5)

/* ex0301.c */
int main()
{
    return(1);
}

ouput:
 
Process returned 1 (0x1)   execution time : 0.110 s
Press any key to continue.

Fungsi return(1) menghasilkan nilai Process returned 1 (atau lebih besar), biasanya menandakan ada eror atau mengindikasikan hasil sebuah operasi. Secara tradisional, jika hasil Process returned 0, artinya program dijalankan dengan sukses.

Mari kita modifikasi lagi file ex0301.c ini menjadi seperti ini:

/* ex0301.c */
#include <stdio.h>

int main()
{
    /* fungsi printf() butuh stdio.h utk bisa jalan */
printf("4 times 5 is %d\n", 4*5);
    return(0);
}

ouput:
 
4 times 5 is 20

Process returned 0 (0x0)   execution time : 0.125 s
Press any key to continue.

%d pada fungsi printf() bisa disebut sebagai variabel yang diisi dengan nilai integer (desimal) yang  ditulis di belakangnya, dalam hal ini 4*5, yang kemudian akan ditampilkan di output.

ex0201.c puts()

/* ex0201.c */
#include <stdio.h>

int main()
{
    puts("Greetings, human.");
    return 0;
}

output:
   
Greetings, human.

Process returned 0 (0x0)   execution time : 0.105 s
Press any key to continue.

Perintah printf() dan puts() sama-sama digunakan untuk menampilkan output. Bedanya, kalau printf() itu bisa disisipkan spesial format seperti %d untuk integer, %s untuk string,  dan %f untuk float. Sedangkan puts() hanya menampilkan secara simpel dan apa adanya tanpa penyisipan spesial format.

Bedanya lagi, untuk printf() tidak otomatis ada penambahan ganti baris (\n), sedangkan puts() sudah otomatis menambahkan ganti baris jika dipanggil.

Sabtu, 25 Oktober 2025

ex0101.c Hello World!

/* ex0101.c */
#include <stdio.h>
#include <stdlib.h>

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

kalau dijalankan:

Hello World!

Process returned 0 (0x0)   execution time : 0.055 s
Press any key to continue.

Mulai Belajar Bahasa C

Kenapa bahasa C? ya pengen aja. Saya pakai buku rujukan: "C Programming for Dummies 2nd Edition (2021) by Dan Gookin".

Untuk kompilernya, pakai https://www.codeblocks.org/downloads/binaries/ pilih yg pakai MingW.

Selamat menikmati.