Introduction C Language

Download Report

Transcript Introduction C Language

Introduction to C Language
─ C語言資料型態
大綱






常數與變數
整數資料型態與變數宣告
浮點數資料型態與變數宣告
字元資料型態與變數宣告
資料的大小
Homework
常數與變數

常數(constant)

指固定不變的資料,程式執行當中不能改變
其值




整數常數:12、-234、0、10000、…
字元常數:‘A’、‘Z’、…
浮點數常數:8.23、0.1232、0.001、…
C++提供三種常數表示法



十進位整數常數:直接寫出數值。
八進位整數常數:以0為開頭的數字。
十六進位整數常數:以0x為開頭的數字。
常數與變數 (續)

Ex. 計算十進位、八進位與十六進位的123、
0123、0x123
#include<stdio.h>
#include<stdlib.h>
void main()
{
printf("%d\n",123);
printf("%d\n",0123);
printf("%d\n",0x123);
printf("%o\n",0123);
printf("%x\n",0x123);
system("PAUSE");
}
常數與變數 (續)

變數



指程式中可以用來儲存資料的空間的“名
字”,如同每個人都有一個名字一樣,當程
式中需要使用某變數時,可以用此名字來稱
呼之
Ex. a=10; //設定變數a的值為10
變數在使用前需先宣告才能使用,其目的在




指定變數的資料型態
配置變數使用的記憶體空間
避免有兩個不同的變數使用相同的名字
為什麼需要宣告變數呢?
常數與變數 (續)

變數的命名規則

變數的命名沒有長度限制,可以包含英文字、數字
和底線符號(_)


大小寫英文字母不能互通,也就是同一個英文字的
大小寫是不同的兩個符號



Ex. happydog和Happydog是不同的變數
儘量不要以底線符號做為變數的開頭


Ex. happydog、happy_cat、class3
Ex. _clearscreen函數可以清除目前的顯示區域
變數不得與C語言的關鍵字名稱相同
變數名稱不能有特殊符號(如:~、\、@、-、 …)

Ex. George&Mary、happy-cat
整數資料型態與變數宣告

整數 int



短整數 short int



宣告方法: int v; 或是 int v=120;
整數範圍:-2147483648~2147483647 (-231~231-1)
宣告方法: short int v;
整數範圍:-32768~32767 (-215~215-1)
長整數 long int


宣告方法: long int v; 或是 long v;
整數範圍:-2147483648~2147483647 (-231~231-1)
整數資料型態與變數宣告 (續)

無符整數 unsigned int


宣告方法: unsigned int v;
整數範圍: 0~216-1
整數宣告型態
長度 bits
signed int
32
short int
unsigned int
unsigned short int
long int
unsigned long int
16
32
16
32
32
值的範圍
備註
signed int可簡寫為int即可。
一般的整數宣告int其實就是宣告signed int
short int可簡寫為short
unsigned int可簡寫為unsigned
unsigned short int可簡寫為unsigned short
long int可簡寫為long
unsigned long int 可簡寫為unsigned long
整數資料型態與變數宣告 (續)

Ex. 寫一程式測試短整數能否表示下列數值?
32767、32768、32769、-32768、-32769
#include <stdio.h>
#include <stdlib.h>
void main()
{
short int v1=32767, v2=32768, v3=32769, v4=-32768, v5=-32769;
printf("v1=%d\n",v1);
printf("v2=%d\n",v2);
printf("v3=%d\n",v3);
printf("v4=%d\n",v4);
printf("v5=%d\n",v5);
system("PAUSE");
}
整數資料型態與變數宣告 (續)
浮點數資料型態與變數宣告

浮點數 float



可用來宣告數學裡的實數,也就是具有小數點的數值
宣告方法: float f=123.456;
兩種表示方法

一般小數點表示法


指數表示法



Ex. 123.456  printf(“%f”, f);
Ex. 1.23456*102  printf(“%e”, f);
浮點數的小數位有6位,即便小數部份沒有那麼多位數,
仍會以0遞補之,如有超過位數,則會捨棄之
小數顯示位元的控制:%d.df  %8.2f
浮點數資料型態與變數宣告(續)

倍精數 double




如果覺得浮點數的精密度不夠,可以將浮點
數宣告為倍精數
記憶體使用:8 bytes
表示範圍:1.7E+/-308
宣告方法: double pi=3.141592654;
浮點數資料型態與變數宣告(續)

Ex. 練習以不同的格式輸出浮點數
#include <stdio.h>
#include <stdlib.h>
void main()
{
float f1=12.3456789;
float f2=12.3456;
printf("%f\n",f1);
printf("%f\n",f2);
printf("%e\n",f2);
printf("%2.2f\n",f2);
system("PAUSE");
}
浮點數資料型態與變數宣告(續)
字元資料型態與變數宣告

字元 char



電腦中用來顯示的字,也就是ASCII所指的
符號
記憶體使用:1 byte
宣告方法: char c;或是char ch=‘A’;
字元資料型態與變數宣告(續)

Ex.讀取使用者輸入的一個字元,接著在螢幕上
顯示該字元的ASCII碼及前後兩個字元
#include <stdio.h>
#include <stdlib.h>
void main()
{
char c;
printf("Please input a character:");
scanf("%c", &c);
printf("The ascii code of %c is %d\n", c, c);
printf("The character before %c is %c\n", c, c-1);
printf("The character after %c is %c\n", c, c+1);
system("PAUSE");
}
字元資料型態與變數宣告(續)
字元資料型態與變數宣告(續)

特殊字元表示法

電腦的字元集中有些字元是控制字元或是特
殊字元(或稱為脫序字元escape sequence),無
法使用鍵盤輸入或顯示字元表達  需使用
兩個字元的表示法來顯示  以‘ \ ’ (反斜
線:backslash)

Ex. ‘\n’  換行  ASCII碼10
字元資料型態與變數宣告(續)
脫序字元 字元作用 ASCII表示法 ASCII碼(值)
\n
新行
NL(LF)
10 or 0x0a
\t
橫向定位點
HT
9
\v
縱向定位點
VT
11 or 0x0b
\b
反空白
BS
8
\r
返回
CR
13 or 0x0d
\f
跳頁
FF
12 or 0x0c
\a
電腦嗶聲
BEL
7
\\
反斜線
\
92 or 0x5c
\?
問號
?
63 or 0x3f
\'
單引號
'
39 or 0x27
\"
雙引號
"
34 or 0x22
\ooo
八進位值
ooo
\xhhh
十六進位值
hhh
\0
空位元
NUL
0
字元資料型態與變數宣告(續)

Ex. 練習特殊字元的表示法
#include <stdio.h>
#include <stdlib.h>
void main()
{
printf("The dog \has three legs.\n");
printf("\123uper man!\n");
printf("\1234uper man!\n");
printf("\x4Aump to the happy world!\n");
printf("Please save the \"APPLE\"on the directory of \'C:\\pic\'.\n");
system("PAUSE");
}
資料的大小

資料的大小 sizeof


當一個變數被宣告完畢後,編譯器就會分配
適當大小的記憶空間給此變數,如果我們想
知道這個變數佔用多少記憶體,可以使用
sizeof敍述來求得
Ex.試觀念下列程式,猜猜其執行結果為何?
#include <stdio.h>
#include <stdlib.h>
void main()
{ int a; float b; double c; char d; unsigned int e;
printf("The size of 'int' is %d\n", sizeof(int));
printf("The size of 'float' is %d\n", sizeof(float));
printf("The size of 'double' is %d\n", sizeof(double));
printf("The size of 'char' is %d\n", sizeof(char));
printf("The size of 'unsigned int' is %d\n", sizeof(unsigned int));
printf("The size of 'long int' is %d\n", sizeof(long int));
printf("The size of 'a' is %d\n", sizeof(a));
printf("The size of 'b' is %d\n", sizeof(b));
printf("The size of 'c' is %d\n", sizeof(c));
printf("The size of 'd' is %d\n", sizeof(d));
printf("The size of 'e' is %d\n", sizeof(e));
printf("The size of 'a*b' is %d\n", sizeof(a*b));
printf("The size of 'a*d' is %d\n", sizeof(a*d));
printf("The size of '123' is %d\n", sizeof(123));
printf("The size of \"Jack\" is %d\n", sizeof("Jack"));
system("PAUSE");
}
Homework

試讓使用者輸入下述資料並將使用者輸入資料列
印於螢幕上
Please input your id: 9103001
Please input your age:18
Please input your weight: 73.6
Please input your height:178.789
Your resume:
id:9103001
age:18
height:178.79
weight:73.60
輸出結果
使用者輸入資料