Typecasting เป็นวิธีการในภาษา C ในการแปลงข้อมูลประเภทหนึ่งเป็นอีกประเภทหนึ่ง
การพิมพ์ดีดมีสองประเภท
1.พิมพ์โดยนัย - การแปลงนี้ทำโดยคอมไพเลอร์ เมื่อใช้ตัวแปรชนิดข้อมูลมากกว่าหนึ่งชนิดในนิพจน์ คอมไพเลอร์จะแปลงชนิดข้อมูลเพื่อหลีกเลี่ยงการสูญเสียข้อมูล
นี่คือตัวอย่างการแคสต์ประเภทโดยนัยในภาษาซี
ตัวอย่าง
#include <stdio.h>
int main() {
int a = 10;
char b = 'S';
float c = 2.88;
a = a+b;
printf("Implicit conversion from character to integer : %d\n",a);
c = c+a;
printf("Implicit conversion from integer to float : %f\n",c);
return 0;
} ผลลัพธ์
Implicit conversion from character to integer : 93 Implicit conversion from integer to float : 95.879997
2.การหล่อประเภทที่ชัดเจน - การแปลงนี้ดำเนินการโดยผู้ใช้ นี้เรียกอีกอย่างว่า typecasting ประเภทข้อมูลถูกแปลงเป็นข้อมูลประเภทอื่นโดยผู้ใช้บังคับ
นี่คือรูปแบบการหล่อแบบชัดแจ้งในภาษาซี
(type) expression
นี่คือตัวอย่างการแคสต์ประเภทที่ชัดเจนในภาษา C
ตัวอย่าง
#include <stdio.h>
int main() {
float c = 5.55;
int s = (int)c+1;
printf("Explicit Conversion : %d\n",s);
return 0;
} ผลลัพธ์
Explicit Conversion : 6