next up previous contents
Next: 變數 Up: 資料型態 Previous: 前言   Contents

sizeof

  1. sizeof 函式可以查看變數、常數及資料型態所佔的位元組數。
  2. 範例:查詢各種資料型態所佔 bytes 數。
    [dywang@dyw219 zzz]$ vim sizeof1.c
    [dywang@dyw219 zzz]$ cat sizeof1.c
    #include <stdio.h>
    
    main() {
    	printf("sizeof(char) = %d\n", sizeof(char));
    	printf("sizeof(short) = %d\n", sizeof(short));
    	printf("sizeof(int) = %d\n", sizeof(int));
    	printf("sizeof(long) = %d\n", sizeof(long));
    	printf("sizeof(float) = %d\n", sizeof(float));
    	printf("sizeof(double) = %d\n", sizeof(double));
    }
    
  3. 編譯成功
    [dywang@dyw219 zzz]$ gcc -o sizeof1 sizeof1.c
    
  4. 執行結果與上節列表吻合嗎?
    [dywang@dyw219 zzz]$ ./sizeof1 
    sizeof(char) = 1
    sizeof(short) = 2
    sizeof(int) = 4
    sizeof(long) = 8
    sizeof(float) = 4
    sizeof(double) = 8
    



De-Yu Wang 2019-08-14