指针
#include <stdio.h>;
int main() {
// 向内存申请4个字节,用来存储10这个值
int a = 10;
// &a 取a的地址的值
printf("%p\n", &a);
// 指针变量p
int* p = &a;
// 解引用操作符,通过p存储的a地址值来访问a的值并修改
*p = 20;
printf("%d", a);
return 0;
}
#include <stdio.h>;
int main() {
/* 不管是什么类型的指针,都是在创建指针变量
指针变量是用来存放地址的
指针变量的大小取决于一个地址存放的时候需要多大的空间
32位机器上的地址 32bit位 = 4byte字节
64位机器上的地址 64bit位 = 8byte字节
*/
printf("%zu\n", sizeof(char*));
printf("%zu\n", sizeof(short*));
printf("%zu\n", sizeof(int*));
printf("%zu\n", sizeof(float*));
printf("%zu\n", sizeof(double*));
return 0;
}
书写的注意事项
#include <stdio.h>;
int main() {
int* p1, p2, p3;
/* 等同于
int* p1;
int p2;
int p3;
而不是我们所认为的创建三个p,都是指针变量
*/
return 0;
}
结构体
结构体是把一些单一类型组合在一起的做法
#include <stdio.h>;
struct Student
{
char name[20];
int age;
char sex[10];
char tel[12];
};
void print(struct Student* ps) {
// 解出指针所指向的值
struct Student s = *ps;
printf("%s %d %s %s\n", s.name, s.age, s.sex, s.tel);
printf("%s %d %s %s\n", ps->name, ps->age, ps->sex, ps->tel);
}
int main() {
struct Student s = { "liufeifei",17,"nv","11111111111" };
print(&s);
return 0;
}
练习题
- 关于C语言关键字说法正确的是?
A.关键字可以自己创建
B.关键字不能自己创建
C.关键字可以做变量名
D.typedef不是关键字
解析:答案选B。
- 用在switch语句中的关键字不包含哪个?
A.continue
B.break
C.case
D.default
解析:答案选A。continue是用在for循环中的关键字。
- 下面哪个不是关键字?
A.int
B.struct
C.define
D.continue
解析:答案选C。
- 编程题
#include <stdio.h>;
int main() {
int a, b;
scanf_s("%d %d", &a, &b);
printf("%d %d", a / b, a % b);
return 0;
}
- 编程题
#include<stdio.h>
int main()
{
int arr[] = { 73,32,99,97,110,32,100,111,32,105,116,33 };
int i = 0;
int i_length = sizeof(arr) / sizeof(arr[0]);
while (i < i_length) {
// %c会将int数字类型转换为对应ASCII字符
printf("%c", arr[i]);
i++;
}
return 0;
}
- 编程题
#include<stdio.h>
int main()
{
int no;
float c, math, eng;
scanf_s("%d;%f,%f,%f", &no, &c, &math, &eng);
printf("The each subject score of No. %d is %.2f, %.2f, %.2f.\n", no, c, math, eng);
return 0;
}
- 关于static的说法不正确的事
A.static可以修饰局部变量
B.static可以修饰全局变量
C.static修饰的变量不能修改
D.static可以修饰函数
解析:答案选C。
- 下面代码的执行结果是什么
#include<stdio.h>
int sum(int a) {
int c = 0;
static int b = 3;
c += 1;
b += 2;
return a + b + c;
}
int main()
{
int i;
int a = 2;
for (i = 0; i < 5; i++)
{
printf("%d\n", sum(a));
}
return 0;
}
// 解析:答案是8 10 12 14 16
#include<stdio.h>
int sum(int a) {
int c = 0;
static int b = 3;
c += 1; // 1 1 1 1 1
b += 2; // 5 7 9 11 13
return a + b + c; // 1+2+5, 1+2+7, 1+2+9, 1+2+11, 1+2+13
}
int main()
{
int i;
int a = 2;
for (i = 0; i < 5; i++)
{
printf("%d\n", sum(a)); // 8 10 12 14 16
}
return 0;
}
- 关于指针的说法正确的事
A.sizeof(char*)大小一定是1
B.指针变量是个变量,用来存放地址的值
C.指针变量的大小一定是4个字节
D.指针不是变量
解析:答案选B。关于A,sizeof获取的是指针变量占据的字节大小,在不同位数的操作系统char*这个指针变量占据的字节大小不一样,在x64位中是8个字节,在x86中是4个字节。可以根据下面这段代码在不同的操作系统中运行结果得知,同理C也是一样的错误原因。
#include<stdio.h>
int main()
{
printf("%d", sizeof(char*));
return 0;
}
- 找到最大的数 已知用户输入四个整数,输出4个整数中最大的数
// 解析
#include<stdio.h>
// 比较两个数谁大并返回
int moreThan(int num1, int num2) {
if (num1 > num2) {
return num1;
}
else {
return num2;
}
}
int main()
{
int a, b, c, d;
scanf_s("%d %d %d %d", &a, &b, &c, &d);
int max = a;
max = moreThan(max, b);
max = moreThan(max, c);
max = moreThan(max, d);
printf("%d", max);
return 0;
}
- 计算BMI指数
#include<stdio.h>
int main()
{
int weight;
int height;
scanf_s("%d %d", &height, &weight);
float bim = weight / (height / 100.0) / (height / 100.0);
printf("%.2f", bim);
return 0;
}