next up previous contents
Next: 儲存類別 Up: 變數與常數 Previous: 常數   Contents

常數定義

  1. 使用前處理定義
    #include <stdio.h>
    
    #define LENGTH 10   
    #define WIDTH  5
    #define NEWLINE '\n'
    
    int main()
    {
    
       int area;  
      
       area = LENGTH * WIDTH;
       printf("value of area : %d", area);
       printf("%c", NEWLINE);
    
       return 0;
    }
    
  2. 使用關鍵字 const
    #include <stdio.h>
    
    int main()
    {
       const int  LENGTH = 10;
       const int  WIDTH  = 5;
       const char NEWLINE = '\n';
       int area;  
       
       area = LENGTH * WIDTH;
       printf("value of area : %d", area);
       printf("%c", NEWLINE);
    
       return 0;
    }
    



2015-01-16