概述
本文为一生一芯系列预学习4 复习c语言的笔记,参考教程笨方法学C,因为本人有C语言基础,所以为了加快进度,本篇笔记中只完成练习0到练习18, 练习32,练习33,练习42,练习44的编程算法和附加题。
练习0
总结
这个练习是关于在linux上配置环境,使用apt命令安装系统包。这个练习很简单,这里记录一下安装过程中的其他问题。
apt安装build-essential时发现我的系统的dpkg存在问题
练习1
总结
入门第一课——Hello, world!
笔记
我使用vim编辑器完成整个练习,在shell中打开vim,输入hello, world的程序,进行编译。
ls
vim ./ex1.c
make ex1
cc ex1.c -o ex1
ex1.c: In function ‘main’:
ex1.c:3:9: warning: implicit declaration of function ‘puts’ [-Wimplicit-function-declaration]
3 | puts("Hello, world!");
| ^~~~
ex1.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘puts’
+++ |+#include <stdio.h>
1 | int main(int argc, char* argv[])
由于终端中使用了个性字符,这里的可能在某些情况下无法正常显示。在没有设置CFLAGS="-Wall"的情况下,这里出现了警告,这可能是由于环境问题。在加上#include
附加题
- 更改文件后应该发生error,编译失败
- 使用for循环多打印5行文本
#include <stdio.h> int main(int argc, char* argv[]) { puts("Hello, world!"); for(int i=0;i<5;i++){ puts("hello"); } return 0; }
结果
- 阅读man 3 puts的文档
练习2
总结
make是很多大型系统的常见构建工具,大部分c/c++工程都使用make和cmake进行构建
笔记
附加题
make ex1
make: 'ex1' is up to date.
make clean
rm -rf ex1
make ex1
cc ex1.c -o ex1
cat Makefile
CFLAGS=-Wall -g
clean:
rm -rf ex1
ex1:
cc ex1.c -o ex1
练习3
总结
使用printf进行格式化输出
笔记
代码运行结果
我们将printf中的age去掉后,输出如下
在printf中,由于没有指定的参数,所以printf会从栈中取一个值,这个值的位置不确定,所以我们会看到一个像随机数的数字。
附加题
为Q3添加上makefile
CFALGS=-wall -g
all: ex3
clean:
rm -rf ./ex3
ex3:
cc ex3.c -o ex3
练习4
总结
Valgrind是一个十分好用的程序调试工具,与gdb不同,gdb是为了更方便的观察程序的运行,而valgrind则是为了更快的告诉你程序的错误或者潜在的错误,比如变量没有初始化,内存没有释放,野指针问题等。
笔记
使用valgrind调试下面的程序
#include <stdio.h>
int main(){
int age = 10;
int height;
printf("I am %d years old.\n");
printf("I am %d inches tall.\n", height);
return 0;
}
我们可以得到如下的输出
valgrind ./ex3
==30798== Memcheck, a memory error detector
==30798== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==30798== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==30798== Command: ./ex3
==30798==
I am -16777944 years old.
==30798== Conditional jump or move depends on uninitialised value(s)
==30798== at 0x48CD027: __vfprintf_internal (vfprintf-process-arg.c:58)
==30798== by 0x48C265A: printf (printf.c:33)
==30798== by 0x109174: main (in /home/fengqi/Documents/GitRepos/ysyx/pre-study_phase/2024_12_18/Q4/ex3)
==30798==
==30798== Use of uninitialised value of size 8
==30798== at 0x48C1AAB: _itoa_word (_itoa.c:177)
==30798== by 0x48CCE58: __vfprintf_internal (vfprintf-process-arg.c:164)
==30798== by 0x48C265A: printf (printf.c:33)
==30798== by 0x109174: main (in /home/fengqi/Documents/GitRepos/ysyx/pre-study_phase/2024_12_18/Q4/ex3)
==30798==
==30798== Conditional jump or move depends on uninitialised value(s)
==30798== at 0x48C1ABC: _itoa_word (_itoa.c:177)
==30798== by 0x48CCE58: __vfprintf_internal (vfprintf-process-arg.c:164)
==30798== by 0x48C265A: printf (printf.c:33)
==30798== by 0x109174: main (in /home/fengqi/Documents/GitRepos/ysyx/pre-study_phase/2024_12_18/Q4/ex3)
==30798==
==30798== Conditional jump or move depends on uninitialised value(s)
==30798== at 0x48CD6F6: __vfprintf_internal (vfprintf-process-arg.c:174)
==30798== by 0x48C265A: printf (printf.c:33)
==30798== by 0x109174: main (in /home/fengqi/Documents/GitRepos/ysyx/pre-study_phase/2024_12_18/Q4/ex3)
==30798==
I am 0 inches tall.
==30798==
==30798== HEAP SUMMARY:
==30798== in use at exit: 0 bytes in 0 blocks
==30798== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==30798==
==30798== All heap blocks were freed -- no leaks are possible
==30798==
==30798== Use --track-origins=yes to see where uninitialised values come from
==30798== For lists of detected and suppressed errors, rerun with: -s
==30798== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
我们可以看到valgrind指出了我们程序运行过程中访问了一些没有被初始化的地址
练习6
总结
使用%s,%d等格式化字符串时,必须按照顺序给printf传入对应的变量。
笔记
根据ex6中提供的代码,程序运行结果如下
练习32
练习32部分笔记位于下面的地址
https://feng-arch.cn/12/27/notes_for_ysyx_project_prestudy_phase_review_c_language_practice_32/
练习33
总结
练习33部分继承练习32中的链表,扩充用来给链表排序的算法,分别为冒泡排序和归并排序。
实现代码部分见git仓库
https://external.feng-arch.cn:35127/fengqi/ysyx
学习笔记部分见如下网页
https://feng-arch.cn/01/09/learning_note_for_ysyx_question_33_bubble_sort_and_merge_sort/
Comments NOTHING