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.

Tidak ada komentar:

Posting Komentar