hello world程序详解
#include<stdio.h> // 标准输入输出库 预编译文件
#include<math.h> // 包含 math 库
/*
# include 包含 <stdio.h> .h header 头文件
stdio standard 标准 i input 输入 o output 输出
这个文件可能包含一个输入输出的头文件
C99 C语言标准
/*
// 标准 int必须写return 主函数入口
int main() {
/*
* 我的第一个C语言程序 注释 不被编译器识别
*/
// 这是单行注释
printf_s("Hello World!\n"); // print 打印 format 格式化输出
int sqrt_num = sqrt(9); // 使用math库
printf("sqrt_num %d\n", sqrt_num);
return 0;
}
画画小游戏
// draw a shape of different picture
void draw_triangle() {
printf(" /|\n"); // 语句statement是按顺序执行的
printf(" / |\n");
printf(" / |\n");
printf(" / |\n");
printf("/____|\n");
}
while 循环
void print_increment() {
int number = 0; // 设置为计数器
// 判断括号内的expression是否为真
while (number <= 5) {
printf("%d\n", number);
number++; // number = number + 1 的简化
}
}
// 无论表达式是非为真都会执行一次
void test_do_while() {
int number = 6;
do {
printf("%d\n", number);
number++; // number = number + 1 的简化
} while(number <= 5);
}
for 循环
// 遍历数组元素
void print_array_element() {
int luckyNumbers[] = {4, 8, 15, 16, 23, 42};
int i;
for (i = 0; i < 6; i++){
printf("%d\n", luckyNumbers[i]);
}
}
结构体
struct Student {
char name[50];
char major[50];
int age;
double gpa;
};
int main(){
struct Student student1;
student1.age = 32;
student1.gpa = 3.2;
// 这里处理字符串需要strcpy函数
strcpy( student1.name, "Jim");
strcpy( student1.major, "Business");
struct Student student2;
student2.age = 18;
student2.gpa = 2.2;
strcpy( student2.name, "Sam");
strcpy( student2.major, "Art");
printf("%f", student2.gpa);
return 0;
}
猜数字游戏
// 猜数字游戏
void guess_number_game_v1() {
int secretNumber = 5;
int guess;
while (guess != secretNumber) {
printf("Enter a number:");
scanf("%d",&guess);
}
printf("You Win!");
}
void guess_number_game_v2() {
int secretNumber = 5;
int guess;
int guessCount = 0; // 猜测次数记数
int guessLimit = 3; // 猜测次数的限制为3
int outOfGuesses = 0; // 0为假 1为真 用于切换状态
while (guess != secretNumber && outOfGuesses == 0) {
if (guessCount < guessLimit) {
printf("Enter a number:");
scanf("%d",&guess);
guessCount++;
} else {
outOfGuesses = 1;
}
}
if (outOfGuesses == 1) {
printf("out of guesses");
} else {
printf("You Win!");
}
}
2D Arrays & Nested Loops
/*2D Arrays & Nested Loops*/
void traverse_nested_arrays() {
// 需要声明是int类型的数组
int numbers[3][2] = {
{1, 2},
{3, 4},
{5, 6}
};
// printf("%d", numbers[0][0]); // 通过下标访问数组元素
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
printf("%d,",numbers[i][j]);
}
printf("\n");
}
}
1.如何理解c语言的内存模型与指针。
https://www.zhihu.com/question/24466000/answer/27893272
编程指北
https://www.zhihu.com/question/432288840/answer/1798182117
2.变量名储存在哪里,变量的储存机制是什么?
https://www.zhihu.com/question/34266997#:~:text=%E5%8F%98%E9%87%8F%E9%83%BD%E8%A6%81%E5%8D%A0%E6%8D%AE%E4%B8%80%E5%AE%9A,%E7%9A%84%E5%8F%98%E9%87%8F%E7%9A%84%E5%86%85%E5%AD%98%E5%9C%B0%E5%9D%80%E3%80%82&text=%E5%8F%98%E9%87%8F%E5%90%8D%E6%98%AF%E7%BB%99%E7%BC%96%E8%AF%91,%E5%AD%98%E6%94%BE%E5%9C%A8%E5%AF%84%E5%AD%98%E5%99%A8%E4%B8%AD%E4%BA%86%E3%80%82
3.数组连续存储的机制是?
// 变量只是内存地址的别名 通过&取出变量的地址
int age = 30;
double gpa = 3.4;
char grade = "A";
int numbers[3] = {1, 2, 3};
printf("age: %p\ngpa: %p\ngrade: %p\n", &age, &gpa, &grade);
printf("%p\n", &numbers[0]);
printf("%p\n", &numbers[1]);
printf("%p\n", &numbers[2]);
指针是地址
指针变量是存储地址的变量
/*
& 取变量地址
* 定义指针变量 或者解引用(取值)
*/
int age = 30;
int *pAge = &age;
int **ppAge = &pAge;
int ***pppAge = &ppAge;
double gpa = 3.4;
double *pGpa = &gpa;
char grade = "A";
char *pGrade = &grade;
printf("age's memory address: %p\n", &age);
printf("pAge 's memory address: %p\n", &pAge);
printf("ppAge 's memory address: %p\n", &ppAge);
printf("pppAge 's memory address: %p\n", &pppAge);
// * 解引用 这里是指针所指向变量的值
printf("pAge内存储地址所指向的值: %d\n", *pAge);
printf("ppAge 's memory address: %d\n", **ppAge);
printf("pppAge 's memory address: %d\n", ***pppAge);
/*
& 和 * 都是一元运算符 互逆
& 取地址
* 取值
*/
printf("%d\n", age);
printf("%p\n", &age);
printf("%d\n", *&age);
printf("%p\n", &*&age);
printf("%d\n", *&*&age);
文件操作
#include <stdio.h>
void write_flie() {
// w 写入文件 如果没有就创建 或者覆盖
FILE * fpointer = fopen("employees.txt", "w"); // fopen 打开文件
// fprintf(fpointer, "overridden"); // 这里覆盖写入了信息
fprintf(fpointer, "Jim, Saleman\nPam, Receptionist\nOscar, Accouning");
fclose(fpointer); // 关闭文件
}
void append_file() {
// a 在指针末尾追加写入
FILE * fpointer = fopen("employees.txt", "a");
fprintf(fpointer, "\nKelly, Customer Service");
fclose(fpointer);
}
void read_file() {
char line[255];
char line1[255];
FILE * fpointer = fopen("employees.txt", "r");
// 使用fgets读取一行内容
fgets(line, 255, fpointer);
printf("%s", line);
fgets(line1, 255, fpointer);
printf("%s", line1);
fclose(fpointer);
}
int main() {
read_file();
return 0;
}