1.C的基本數(shù)據(jù)類型
java基本數(shù)據(jù)類型 C基本數(shù)據(jù)類型
基本數(shù)據(jù)類型所占字節(jié)數(shù):
| java基本數(shù)據(jù)類型 | C基本數(shù)據(jù)類型 |
|---|---|
| boolean:1 | -- |
| byte:1 | -- |
| char:2 | char:1 |
| short:2 | short:2 |
| int:4 | int:4 |
| long:8 | long:4 |
| float:4 | float:4 |
| double:8 | double:8 |
| -- | singed(有符號(hào)):有符號(hào)數(shù),最高位表示符號(hào)位,可以表示負(fù)數(shù)。 |
| -- | unsinged(無符號(hào)):無符號(hào)數(shù),最高位表示數(shù)值位,不可以表示負(fù)數(shù)。 |
| -- | void(): |
C沒有boolean byte C用 0和!0表示true和false
java一個(gè)字節(jié)所表示的數(shù)值大小

java字節(jié)表示數(shù)值大小
C一個(gè)字節(jié)所表示的數(shù)值大小

C一個(gè)字節(jié)所表示的數(shù)值大小
#include<stdio.h>
#include<stdlib.h>
/**
char, int, float, double, long, short, signed, unsigned, void
signed unsigned 有符號(hào) 無符號(hào) 只能用來修飾整形變量 char int short long 默認(rèn)有符號(hào)
sizeof(int)
*/
main(){
printf("char占%d個(gè)字節(jié)\n", sizeof(char));
printf("int占%d個(gè)字節(jié)\n", sizeof(int));
printf("short占%d個(gè)字節(jié)\n", sizeof(short));
printf("float占%d個(gè)字節(jié)\n", sizeof(float));
printf("long占%d個(gè)字節(jié)\n", sizeof(long));
printf("double占%d個(gè)字節(jié)\n", sizeof(double));
unsigned char c = 128;
printf("c = %d\n",c);
system("pause");
}

image.png
2.C的指針
當(dāng)聲明一個(gè)變量,即會(huì)在內(nèi)存中開辟出一段連續(xù)的內(nèi)存空間。
#include<stdio.h>
#include<stdlib.h>
main(){
int i = 123;
//一般計(jì)算機(jī)中用16進(jìn)制數(shù)來表示一個(gè)內(nèi)存地址
printf("%#x\n",&i);
//int* int類型的指針變量 pointer指針 指針變量只能用來保存內(nèi)存地址
//用取地址符&i 把變量i的地址取出來 用指針變量pointer 保存了起來
//此時(shí)我們可以說 指針pointer指向了 i的地址
//int *pointer ; int * pointer
printf("pointer的值 = %#x\n",pointer);
printf("pointer的內(nèi)存地址 = %#x\n",&pointer);
printf("*pointer的值%d\n",*pointer);
*pointer = 456;
printf("i的值是%d\n",i);
system("pause");
}

image.png

image.png
3.C語言野指針
main(){
//野指針 指針使用之前要初始化 賦給它一個(gè)自己程序中聲明的變量的地址
//指針使用的時(shí)候要注意 int類型的指針要指向int類型的內(nèi)存地址, double類型的指針要指向double類型的地址 ....
//如果亂指會(huì)出bug
int i;
double d = 3.1415;
int* pointer = &d;
printf("pointer的值=%#x\n",pointer);
printf("*pointer = %d\n",*pointer);
system("pause");
}
4.C語言結(jié)構(gòu)體
C的機(jī)構(gòu)體類似于Java的類,struct來聲明C的結(jié)構(gòu)體
#include<stdio.h>
#include<stdlib.h>
/**
c結(jié)構(gòu)體 類似java的class struct來聲明c的結(jié)構(gòu)體
結(jié)構(gòu)體的大小大于等于結(jié)構(gòu)體中每一變量的占字節(jié)數(shù)的和
結(jié)構(gòu)體的大小是最大的那個(gè)變量所占字節(jié)數(shù)的整數(shù)倍
C結(jié)構(gòu)體中不能定義函數(shù)
函數(shù)指針的定義 返回值(*函數(shù)指針變量名字)(返回值);
-> 間接引用運(yùn)算符
*/
void study(){
printf("good good study!\n");
}
//struct Student{
typedef struct Student{
int age; //8
int score; // 4
char sex; //1
void(*studypointer)();
//};
} stud;
main(){
//struct Student = {18,100,'f'};
stud stu = {18,100,'f'};
stu.studypointer = &study;
stu.studypointer();
struct Student* stuPointer = &stu;
printf("*stuPointer.age = %d\n",(*stuPointer).age);
(*stuPointer).sex ='m';
printf("stu.sex = %c\n",stu.sex);
printf("stuPointer->age = %d",stuPointer->age);
//printf("stu.age = %hd\n",stu.age);
//printf("stu.score = %d\n",stu.score);
//printf("stu.sex = %c\n",stu.sex);
// printf("結(jié)構(gòu)體student占%d個(gè)字節(jié)\n",sizeof(stu));
system("pause");
}

image.png
5.C語言枚舉
#include<stdio.h>
#include<stdlib.h>
enum weekday{
MON=9, TUE, WEND,THUR,FRI,SAT,SUN
};
main(){
enum weekday day = MONe;
printf("day= %d",day);
system("pause");
}

image.png
6.C語言自定義類型
#include<stdio.h>
#include<stdlib.h>
typedef int i;
main(){
i j = 123;
printf("j = %d\n",j);
system("pause");
}

image.png