【411】COMP9024 Assignment1 问题汇总

1. 构建 Makefile 文件后运行错误,undefined reference to 'sqrt'

2. Linux 下 C 语言程序的调试

  • 将文件保存为hello.c后,在终端中使用敲入以下命令来使用GCC对程序进行编译。
    gcc hello.c -o hello
  • 编译通过后,我们会在当前目录中看到hello文件,这就是编译后生成的可执行文件。
  • 参考:Linux下编写C程序( GCC )(hello,world)

3. 创建 Makefile 文件

  • 相当于将上面的编译命令写入到一个 Makefile 文件中,文件无扩展名,第一个字母可以大写,其他都是小写
  • 通过 make 命令可以执行 Makefile 文件
  • 参考:【410】Linux 系统 makefile 文件
  • 参考:Makefile使用

4. 文件标准输入输出,stdin、stdout、stderr

//读取的数据存储在 str 中
//可以通过手动输入
//也可以通过命令行从文件输入
//a < input.txt
//将需要输入的信息存储到 input.txt 中即可
fgets(str, 50, stdin);
 
//可以直接输出到控制台
//也可以通过命令行输出到文件中
//a < input.txt > output.txt
//只会将含有 stdout 的内容输出到文件中
//带有 stderr 的部分则是正常以错误的形式打印在控制台上
fprintf(stderr, "Error!");
fprintf(stdout, "Error!");

5. 不能使用数组(也就是方括号)

// (char *):说明类型,最好带着
// sizeof(char) * 10:分配内存的大小需要通过计算,不同类型不一样
char *str = (char *)malloc(sizeof(char) * 10);

//判断
if (str == NULL){
    fprintf(stderr, "Memory allocation error.
");
    exit(EXIT_FAILURE);
}

// 基本与上面类似
str = (char *)realloc(str, sizeof(char) * 20);
//判断
if (str == NULL){
    fprintf(stderr, "Memory allocation error.
");
    exit(EXIT_FAILURE);
}

// 释放
free(str);
str = NULL;
原文地址:https://www.cnblogs.com/alex-bn-lee/p/11072721.html