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

Integer Promotions ใน C


มี data types ที่ใช้ bytes น้อยกว่า integer เช่น char, short ถ้าทำ operations บนพวกมัน จะ automatically promoted เป็น int เรียกว่า integer promotions

โปรแกรมสาธิต integer promotion ใน C:

ตัวอย่าง

#include <stdio.h> int main() {    char x = 68;    char y = 34;    printf("The value of x is: %d", x);    printf("\nThe value of y is: %d", y);    char z = x/y;    printf("\nThe value of z : %d", z);    return 0; }

ผลลัพธ์

The output of the above program is as follows.

The value of x is: 68 The value of y is: 34 The value of z : 2

เข้าใจโปรแกรมข้างต้น:

Variables x และ y เป็น char data type ตอนหาร พวกมัน promoted เป็น int อัตโนมัติ resultant value เก็บใน z เรียกว่า integer promotion Code snippet:

char x = 68; char y = 34; printf("The value of x is: %d", x); printf("\nThe value of y is: %d", y); char z = x/y; printf("\nThe value of z : %d", z);