Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

Float และ Double ในภาษา C

Float

float เป็นชนิดข้อมูลสำหรับจำนวนจริง 32 บิต (IEEE 754 single precision) มีความแม่นยำประมาณ 6 ตำแหน่งทศนิยม

รูปแบบไวยากรณ์

float variable_name;

ตัวอย่าง

#include <stdio.h>
#include <string.h>
int main() {
    float x = 10.327;
    int y = 28;
    printf("The float value : %f\n", x);
    printf("The sum of float and int variable : %f\n", (x + y));
    return 0;
}

ผลลัพธ์

The float value : 10.327000
The sum of float and int variable : 38.327000

Double

double เป็นชนิดข้อมูลจำนวนจริง 64 บิต (IEEE 754 double precision) มีความแม่นยำประมาณ 15 ตำแหน่งทศนิยม

รูปแบบไวยากรณ์

double variable_name;

ตัวอย่าง

#include <stdio.h>
#include <string.h>
int main() {
    float x = 10.327;
    double y = 4244.546;
    int z = 28;
    printf("The float value : %f\n", x);
    printf("The double value : %f\n", y);
    printf("The sum of float, double and int variable : %f\n", (x + y + z));
    return 0;
}

ผลลัพธ์

The float value : 10.327000
The double value : 4244.546000
The sum of float, double and int variable : 4282.873000