C語言程式設計 資料類型轉換

來源:互聯網
上載者:User
Technorati 標籤: C語言,資料類型,轉換,隱式,Data Type,Conversion,Convert,Cast

C語言規定,不同類型的資料需要轉換成同一類型後才可進行計算,在整型、實型和字元型資料之間通過類型轉換便可以進行混合運算(但不是所有類型之間都可以進行轉換) .

當混合不同類型的變數進行計算時,便可能會發生類型轉換 .
相同類型的資料在轉換時有規則可循:
字元必須先轉換為整數(C語言規定字元類型資料和整型資料之間可以通用) .
short型轉換為int型(同屬於整型).
賦值時,一律是右部值轉換為左部類型.

另外,

當整型資料和雙精確度資料進行運算時,C先將整型資料轉換成雙精確度型資料,再進行運算,結果為雙精確度類型資料.
當字元型資料和實型資料進行運算時,C先將字元型資料轉換成實型資料,然後進行計算,結果為實型資料.

樣本程式, 測試環境VC6

/*  C語言資料類型轉換 Data Type Conversion in C Programming Language */
#include <stdio.h>
void main()
{
    int int1 = 1;
    unsigned unsigned1=2;
    short short1=1;
    char char1 = 'a';
    long long1=1L;
    float float1 = 2.0F;
    double double1 = 3.0L;

    printf("\n各種資料類型的資料佔用的記憶體空間:\n");
    printf("sizeof(int):%d 位元組\n",sizeof(int1));
    printf("sizeof(unsigned):%d 位元組\n",sizeof(unsigned1));
    printf("sizeof(short):%d 位元組\n",sizeof(short1));
    printf("sizeof(char):%d 位元組\n",sizeof(char1));
    printf("sizeof(long):%d 位元組\n",sizeof(long1));   
    printf("sizeof(float):%d 位元組\n",sizeof(float1));
    printf("sizeof(double):%d 位元組\n",sizeof(double1));

    printf("\n同類型資料間進行運算:\n");
    printf("sizeof(int+int):%d 位元組\n",sizeof(int1+int1));
    printf("sizeof(unsigned+unsigned):%d 位元組\n",sizeof(unsigned1+unsigned1));
    printf("sizeof(short+short):%d 位元組\n",sizeof(short1+short1)); /* 轉換為 int */
    printf("sizeof(char+char):%d 位元組\n",sizeof(char1+char1)); /* 轉換為 int */
    printf("sizeof(long+long):%d 位元組\n",sizeof(long1+long1));
    printf("sizeof(float+float):%d 位元組\n",sizeof(float1+float1));
    printf("sizeof(double+double):%d 位元組\n",sizeof(double1+double1));

    printf("\n不同類型資料間進行運算:\n");
    printf("sizeof(int+unsigned):%d 位元組\n",sizeof(int1+unsigned1)); /* 轉換為 int */
    printf("sizeof(int+char):%d 位元組\n",sizeof(int1+char1)); /* 轉換為 int */
    printf("sizeof(int+long):%d 位元組\n",sizeof(int1+long1)); /* 轉換為 long */
    printf("sizeof(int+float):%d 位元組\n",sizeof(int1+float1)); /* 轉換為 float */
    printf("sizeof(char+float):%d 位元組\n",sizeof(char1+float1)); /* 轉換為 float */
    printf("sizeof(float+double):%d 位元組\n",sizeof(float1+double1)); /* 轉換為 double */
}

 

運行結果

各種資料類型的資料佔用的記憶體空間:
sizeof(int):4 位元組
sizeof(unsigned):4 位元組
sizeof(short):2 位元組
sizeof(char):1 位元組
sizeof(long):4 位元組
sizeof(float):4 位元組
sizeof(double):8 位元組

同類型資料間進行運算:
sizeof(int+int):4 位元組
sizeof(unsigned+unsigned):4 位元組
sizeof(short+short):4 位元組
sizeof(char+char):4 位元組
sizeof(long+long):4 位元組
sizeof(float+float):4 位元組
sizeof(double+double):8 位元組

不同類型資料間進行運算:
sizeof(int+unsigned):4 位元組
sizeof(int+char):4 位元組
sizeof(int+long):4 位元組
sizeof(int+float):4 位元組
sizeof(char+float):4 位元組
sizeof(float+double):8 位元組

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.