Eagle233-Blog

CS50x 2024 - Week 2 Arrays


Categories CS50x 2024
Tags

1.1k Words   |   5 Minutes


C 语言编译过程

Generated by gpt-4o-mini

C 语言的编译过程通常分为几个主要阶段,每个阶段都有其特定的功能。以下是 C 语言编译过程的简要概述:

1. 预处理 (Preprocessing)

  • 功能:处理以 # 开头的指令,如 #include 和 #define。

  • 输出:生成一个扩展的源代码文件,所有的宏定义和头文件都被替换和包含。

2. 编译 (Compilation)

  • 功能:将预处理后的源代码转换为汇编语言代码。

  • 输出:生成一个汇编语言文件(通常以 .s 结尾)。

3. 汇编 (Assembly)

  • 功能:将汇编语言代码转换为机器代码。

  • 输出:生成一个目标文件(通常以 .o 或 .obj 结尾),其中包含机器代码,但尚未链接。

4. 链接 (Linking)

  • 功能:将一个或多个目标文件和库文件链接成一个可执行文件。

  • 输出:生成最终的可执行文件(通常以 .exe 或无扩展名)。

总结

整个过程可以用以下命令行表示:

gcc -o output_file source_file.c

  • gcc 是 GNU 编译器集合的命令。

  • -o output_file 指定输出的可执行文件名。

  • source_file.c 是要编译的源代码文件。

通过以上步骤,C 语言程序从源代码最终变成可执行的机器代码。


Arrays

arrays[x]

  • Arrays are 0 indexed in C.

  • x: index

  • In C, arrays only store a single type of data.

int array[n] = { element1, element2, ... };

  • In C, n cannot be changed.

  • Each element can be an array itself. (Imagine a 2D / 3D structure)

    • bool battleship[10][10];
  • In Python, index = -1 means the last element of the array. (No such thing in C)

  • 可以指定小于 Size 的元素。(?)

  • Use loops to copy arrays because arryas cannot be seen as variables.

    • example:

      int foo[5], bar[5];
      ~~bar = foo~~;
  • Passed by value but not passed by reference.

    • 思考:结果是什么?如果你知道答案,那么你就理解这个概念了。

      void set_array(int array[4]);
      void set_int(int x);
      int main(void)
      {
          int a = 10;
          int b[4] = { 0, 1, 2, 3 };
          set_int(a);
          set_array(b);
          printf(“%d %d\n”, a, b[0]);
      }
      void set_array(int array[4])
      {
          array[0] = 22;
      }
      void set_int(int x)
      {
          x = 22;
      }

Command Line Arguments

int main(int argc, string argv[])
{

}
  • argc (Argument Count): 一个整数,表示命令行参数的数量,包括程序的名称本身。

  • argv[] (Argument Vector): 一个字符串数组(字符指针数组),用于存储命令行参数的实际值。

    • Stored as a string.

Strings

  • A special kind of array.

  • End with “00” (“\0”, “NUL”).

    • Thus length == n + 1.

    • 但是使用 strlen() 时,获得的是实际的长度。


技巧

这边技巧的部分其实在完成时才发现很多内容在 CS50 Manual Pages 中就能找到,拍大腿啊拍大腿。(再次印证了检索信息能力的重要性?)

当然也不必重写了,权当是一个相对详细一点的索引吧。(即便效率可能还不如官方 Manual)

Get the Length of an Array

#include <string.h>
int length = strlen(phrase);

不使用 strlen 实现获得长度

int string_length(string s)
{
    // Count number of characters up until '\0' (aka NUL)
    int n = 0;
    while (s[n] != '\0')
    {
        n++;
    }
    return n;
}

另一种遍历字符串长度的方法

if (int i = 0; s[i] != '\0'; i++)

Compare break to return 0

  • break 用于控制循环和 switch 的流转,而 return 0 用于结束函数并返回值,特别是在 main 函数中表示程序的退出状态。(但是也许都有结束的效果?)

Convert string to int / float

#include <stdlib.h>
int n = atoi(argv[1]);
float n = atof(argv[1]);

思考:和 (int) 有什么区别呢?

Convert Lowercase Characters to Uppercase Characters

#include <ctype.h>
char c = toupper(n);
char c = tolower(n);

if (isupper(c)) // 检查是否大写
if (islower(c)) // 检查是否小写

不使用 toupper 实现小写转为大写

for (int i = 0, n = strlen(s); i < n; i++)
{
    if (s[i] >= 'a' && s[i] <= 'z')
    {
        printf("%c", s[i] - 32);
    }
    else
    {
        printf("%c", s[i]);
    }
}

检查字符是否是一个字母

#include <ctype.h>
if (isdigit(character))

检查字符是否是一个数字

#include <ctype.h>
if (isalpha(character))

四舍五入

#include <math.h>
double rounded = round(number);
  • The round function will return a floating-point number if the input is a float. If the input is an integer, it will return an integer.

检查字符串是否是一个整数

char *end;
long number = strtol(string, &end, 10);
if (*end == '\0') 
{
    // The string is a valid integer
} 
else 
{
// The string is not a valid integer
}

检查字符串是否一致

#include <string.h>
if (strcmp("hello", "world") == 0) 
{
    // Strings are equal
} 
else 
{
    // Strings are not equal
}


Page views: Loading...  ·  Visitors: Loading...
Except where otherwise noted, original content on this site is dedicated to the public domain under CC0 1.0.
Powered by Hexo & Theme mdsuper
沪ICP备2026040813号
Search