林中誠 程式設計 20121002 功課1-1 林中誠 程式設計 20121002 功課1-1

#include <stdio.h>
#include <stdlib.h>

int main()
{
   

    short int no1=200; /* 宣告為短整數 */
    int  no2=200;
    long int no3=200;
   
    printf("%d%佔有%d位元組\n",no1,sizeof(no1));  /* 以10進位輸出 */
    printf("%d%佔有%d位元組\n",no2,sizeof(no2));  /* 以10進位輸出 */
    printf("%d%佔有%d位元組\n",no3,sizeof(no3));  /* 以10進位輸出 */
     
    system("pause");
    return 0;
}
 

林中誠 程式設計 20121002 功課2-1 林中誠 程式設計 20121002 功課2-1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    
     har ch1='a';/* 宣告ch1為字元變數 */ 

     char ch2[]="a";/* 宣告ch2為字串變數 */ 

     

     printf("ch1=%c 有%d 位元\n",ch1,sizeof(ch1));

     /* 輸出ch1的值及所佔的位元數 */ 

     printf("ch2=%s 有%d 位元\n",ch2,sizeof(ch2));

     /* 輸出ch2的值及所佔的位元數 */ 

      

     system("pause");

     return 0;

}
 

林中誠 程式設計 20121002 功課1-2 林中誠 程式設計 20121002 功課1-2

#include <stdio.h>
#include <stdlib.h>

int main()
{
   
    unsigned short int s1=-1;/* 超過無號短整數的下限值 */                
    short int s2=32768;  /* 超過短整數的上限值 */           
   
   
    printf("s1=%d\n",s1);  
    printf("s2=%d\n",s2); 
     
    system("pause");
    return 0;
}
 

林中誠 程式設計 20121002 功課2-2 林中誠 程式設計 20121002 功課2-2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    
     /* 四種字串宣告與設定初值模式 */
     char Str_1[6]="Hello";
     char Str_2[6]={ 'H', 'e', 'l', 'l','o','\0'};
     char Str_3[ ]="Hello";
     char Str_4[ ]={ 'H', 'e', 'l', 'l', 'o', '!' };
      
    
     printf("%s\n",Str_1);
     printf("%s\n",Str_2);
     printf("%s\n",Str_3);
     printf("%s\n",Str_4);
    
     system("pause");
     return 0;
}
 

林中誠 程式設計 20121002 功課1-3 林中誠 程式設計 20121002 功課1-3

#include <stdio.h>
#include <stdlib.h>

int main()
{
   
    float f1=123.4568357109375F;/* 宣告單精數浮點數 */
    float f2=21341372.1357912;/* 宣告八位數整數值的單精數浮點數 */
    double d1=123456789.123456789123;/* 宣告倍精數浮點數 */
   
    printf("f1=%f\n",f1);
    printf("f2=%f\n",f2);
    printf("d1=%f\n",d1);
         
    system("pause");
    return 0;
}
 

林中誠 程式設計 20121002 功課2-3 林中誠 程式設計 20121002 功課2-3

#include<stdio.h>
#include<stdlib.h>

int salary=17500;/* 宣告salary為全域變數 */

int main()
{
 
  printf("salary=%d\n",salary);
  {
   int salary=22000;/* 在此宣告salary為區塊變數 */
   printf("salary=%d\n",salary);
  }
  printf("salary=%d\n",salary);
  
  system("pause");
  return 0;
}