1.數據類型簡介ios
c++中數據類型分爲兩種:預約義類型和自定義數據類型。c++
預約義類型:整型(int 4個字節)數組
字符型函數
布爾型 (bool)spa
浮點型(4個字節)3d
空類型:關鍵字void,不能用於普通變量的聲明和普通的操做,只能用於指針型變量,函數返回值和函數參數。指針
指針類型c++11
自定義類型:數組、結構體(struct)、聯合體(union)、枚舉(enum).具體在下邊介紹blog
2.類型修飾符:內存
signed,unsigned,short,long。
signed int a;//有符號整數
short int a;//短整型,2個字節
long int a;//長整型,4個字節
c++11中新增:long long整型有兩種:long long和unsigned long long。在C++11中,標準要求long long整型能夠在不一樣平臺上有不一樣的長度,但至少有64位。咱們在寫常數字面量時,可使用LL後綴(或是ll)標識一個long long類型的字面量,而ULL(或ull、Ull、uLL)表示一個unsigned long long類型的字面量。好比:
long long int lli = -9000000000000000000LL;
unsigned long long int ulli = -9000000000000000000ULL;
另外這幾個符號只能修飾int。
3.佔位符總結:
%d:int
%ld:long
%f:float
%lf:double
%p:輸出變量地址
%x/%X:輸出十六進制數
%s:輸出字符串
%o:無符號八進制整數
%u:無符號十進制整數
%e/%E:用科學記數法輸出浮點數
4.基本數據類型代碼示例:
#include"stdio.h" int a; float b; double c; void ceshi(); int main(void) { int d; int num=2147483646;//和最大值差1 最大值:2147483647 float f=1.234567; char arr[]="abc"; printf("num=%d\n",num+21);//-2147483648; printf("f=%f\n",f/4);//0.308642 printf("arr的地址:%p\n",arr);//0028FEFC printf("arr的字符串:%s\n",arr);//abc long int lnum; printf("長整型佔用字節數:%d\n",sizeof(lnum));//4 printf("整型佔用字節數:%d\n",sizeof(num));//4 printf("默認值:%d\n",lnum);//38 隨機的 printf("main默認值:%d\n",d); //隨機數 printf("默認值:%f\n",b); //0.000000 printf("默認值:%f\n",c); //0.000000 printf("默認值:%lf\n",c); //0.000000 ceshi(); return 0; } void ceshi(){ int a=2,b=3,c=5,num; num=a+b%c; printf("jisuan: %d\n",num);//5 char cc[64]; scanf("%s",cc); printf("輸入的值爲:%s aa\n",cc); }
5.結構體數據類型
5.1結構體是由一系列具備相同類型或者不一樣類型的數據構成的數據集合。
定義:
struct 名稱{
數據類型 變量名1;
數據類型 變量名2;
。。。。
數據類型 變量名n;
}結構體變量名;
5.2結構體數組:
struct Info{
int age;
char name[32];
}info[SIZE];
5.3結構體嵌套:
struct BookInfo{
char name[SIZE];
char author[SIZE];
int status;
};
struct ReadreInfo{
char Name[SIZE];
char Date[SIZE];
};
//結構體嵌套
struct LibraryInfo{
int ID;
struct BookInfo book;
struct ReadreInfo reader;
}LibraryInfo;
代碼示例:
#include <stdio.h> #define SIZE 32 int main(void){ struct BookInfo{ char name[SIZE]; char author[SIZE]; int status; }; struct ReadreInfo{ char Name[SIZE]; char Date[SIZE]; }; //結構體嵌套 struct LibraryInfo{ int ID; struct BookInfo book; struct ReadreInfo reader; }LibraryInfo; printf("input the book id:\n"); scanf("%d",&LibraryInfo.ID); printf("input the book name:\n"); scanf("%s",LibraryInfo.book.name); printf("input the book author:\n"); scanf("%s",LibraryInfo.book.author); printf("input the book status:\n"); scanf("%d",&LibraryInfo.book.status); printf("input the reader name:\n"); scanf("%s",LibraryInfo.reader.Name); printf("input the reader BorrowDate:\n"); scanf("%s",LibraryInfo.reader.Date); printf("圖書信息以下:\n"); printf("圖書編號:%d\n",LibraryInfo.ID); printf("書名:%s\n",LibraryInfo.book.name); printf("圖書做者:%s\n",LibraryInfo.book.author); printf("圖書當前狀態:%d\n",LibraryInfo.book.status); printf("讀者姓名:%s",LibraryInfo.reader.Name); printf("借閱日期:%s",LibraryInfo.reader.Date); return 0; }
6.聯合體
聯合體的使用方式和結構體及普通變量相似。
union 名稱{
類型變量符 變量名;
......
類型變量符 變量名;
};
聯合體的主要做用就是節省內存,由於聯合體中的·變量不像結構體中的成員變量那樣,系統會給其中的每個變量都分配相應的內存。在聯合體中,全部的成員變量共享一塊內存空間。該內存空間是其成員變量中佔字節數最多的變量所佔用的內存空間,而且全部的成員變量共用這一塊內存空間,所以聯合體中全部成員變量地址是同樣的。
示例代碼:
#include <stdio.h> #define SIZE 32 int main(void){ union Info{ int age; char Name[32]; char Address[64]; }info; printf("input age:\n"); scanf("%d",&info.age); printf("age:%d\n",info.age); printf("input name:\n"); scanf("%s",info.Name); printf("name:%s\n",info.Name); printf("input address:\n"); scanf("%s",info.Address); printf("address:%s\n",info.Address);//jinan printf("name:%s\n",info.Name); //jinan 和上邊的同樣,被覆蓋 printf("age:%d\n",info.age); //1634625898 printf("name地址:%p\n",info.Name); printf("age地址:%p\n",&info.age); //0028FED0地址和上邊同樣 return 0; }
、、
從圖中能夠看出:最下邊address和name最後的輸出是同樣的,jinan把前邊的chaochao覆蓋掉啦
另外它全部變量的地址也是同樣的。
7.枚舉類型
enum 枚舉類型名{
元素列表;
};
例:enum Season{SPRING,SUMMER,AUTOMN,WINTER};
示例代碼:
#include <iostream> #include <stdio.h> using namespace std; int testEnum(); int main() { cout << "Hello world!" << endl; testEnum(); return 0; } //枚舉類型中的變量爲整型常量,除了定義時能夠賦值外,其餘時間都不能夠 int testEnum(){ enum FourSeason1{ SPRING, SUMMER, AUTOMN, WINTER }; enum FourSeason2{ SPRING2=2, SUMMER2, AUTOMN2, WINTER2 }; enum FourSeason3{ SPRING3=1, SUMMER3, AUTOMN3=5, WINTER3 }; enum FourSeason1 curSeason1,PreSeason1; enum FourSeason2 curSeason2,PreSeason2; enum FourSeason3 curSeason3,PreSeason3; curSeason1=WINTER; PreSeason1=AUTOMN; curSeason2=WINTER2; PreSeason2=AUTOMN2; curSeason3=WINTER3; PreSeason3=AUTOMN3; printf("curSeason1 is %d\n",curSeason1);//3 printf("PreSeason1 is %d\n",PreSeason1);//2 printf("curSeason2 is %d\n",curSeason2);//5 printf("PreSeason2 is %d\n",PreSeason2);//4 printf("PreSeason2 is %d\n",curSeason3);//6 printf("PreSeason2 is %d\n",PreSeason3);//5 printf("SUMMER3 is %d\n",SUMMER3);//2 SUMMER3 cout << SUMMER3 <<endl; return 0; }
先寫這麼多,之後再補充。。