# 第二章 类型、运算符与表达式

基本数据类型：
char -- 字符型
int  -- 整型
float -- 单精度浮点型
double  -- 双精度浮点型


float和double的精度

## const关键字

const int *p = &i; // 指针p指向的内容不能通过p被修改
int * const p = &i; // 指针p本身的内容无法被修改


#include <stdio.h>

int main()
{
int i = 123;
const int *p = &i; // 等价于 int const *p = &i;

//*p = 789; // 错误,指针p指向的内容不能通过p被修改

printf("i = %d.\n", *p);

i = 456;

printf("i = %d.\n", *p);

return 0;
}

#include <stdio.h>

int main()
{

int i = 123;

int * const p = &i;

int j  = 456;

// p = &j;  //错误, p是常量无法修改其值

}


## sizeof

#include <stdio.h>

int main()
{

printf("%lu\n", sizeof(char));
printf("%lu\n", sizeof(int));
printf("%lu\n", sizeof(float));
printf("%lu\n", sizeof(double));

return 0;

}



#include <stdio.h>

int main()
{
int a = 0;
double d = 10.21;
printf("sizeof(a) = %lu\n", sizeof(a));
printf("sizeof(d) = %lu\n", sizeof(d));
printf("sizeof(a + d) = %lu\n", sizeof(a + d));
return 0;
}

sizeof(a) = 4
sizeof(d) = 8
sizeof(a + d) = 8


#include <stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 7, 98, 0, 12, 35, 99, 14 };
printf("Number of elements:%lu \n", sizeof(arr) / sizeof(arr[0]));
return 0;
}

Number of elements:11



int* ptr = (int*)malloc(10 * sizeof(int));



## struct 对齐

The sizeof for a struct is not always equal to the sum of sizeof of each individual member. This is because of the padding added by the compiler to avoid alignment issues. Padding is only added when a structure member is followed by a member with a larger size or at the end of the structure.

Different compilers might have different alignment constraints as C standards state that alignment of structure totally depends on the implementation.


### 案例1

#include <stdio.h>

int main()
{

struct A {

// sizeof(int) = 4
int x;

// sizeof(double) = 8
double z;

// sizeof(short int) = 2
short int y;
};

printf("Size of struct: %ld", sizeof(struct A));

return 0;
}


Size of struct: 24


### 案例2

#include <stdio.h>

int main()
{

struct B {
// sizeof(double) = 8
double z;

// sizeof(int) = 4
int x;

// sizeof(short int) = 2
short int y;
};

printf("Size of struct: %ld", sizeof(struct B));

return 0;
}


Size of struct: 16


### 案例3


// C program to illustrate
// size of struct
#include <stdio.h>

int main()
{

struct C {
// sizeof(double) = 8
double z;

// sizeof(short int) = 2
short int y;

// sizeof(int) = 4
int x;
};

printf("Size of struct: %ld", sizeof(struct C));

return 0;
}



Size of struct: 16


## 指针与数组

int strlen(char *string);
int strlen(char string[]);



#include <stdio.h>

void printfc(char a[])
{

while (*a != '\0')
{
printf("%c  ", *a);
a ++; // 此处a是指针可以自增
}
printf("\n");

return ;
}

int main()
{

char a[] = "Hello World!";

printfc(a);

return 0;
}



H e l l o   W o r l d !

#include <stdio.h>

int main()
{

char a[] = "Hello World!";

while (*a != '\0')
{
printf("%c ", *a);
a ++; // 此处错误,数组名a是常量,无法进行自增运算
}
printf("\n");

return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

very_happy

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-24

06-01
11-21
05-14
03-06