測驗
https://sandbox.cs50.io/
https://cs50.harvard.edu/x/2020/weeks/2/
https://www.youtube.com/watch?v=8PrOp9t0PyQ&t=11s
進入點
Source code :
https://cs50.harvard.edu/x/2020/psets/2/
C編譯過程
- precompiling 編譯前
- compiling 編譯
- assembling 組合
- linking
Source code
1 | #include <cs50.h> |
經過 precompiling
1 | ... |
compiling 編譯發生的事情 會經過
- intel AMD 大廠
- CPU 處理基礎(底層操作)
@main
1
2
3
4
5
6
7
8
9
10
11...
main :
.cfi_startproc
##### BB#0:
pushq %rbp
.Ltmp0:
.cfi_def_cfa_offset16
.Ltmp2:
subq $16, $rsp
xorl %eax, %eax
movl %eax, edi
之後開始組合
assembling
011010101010110011010
101010101010101010101
101010101010101010101
linking
1 | hello.c cs50.c pritf.c |
int main(void) {
printf(“hello word \n”)
}
1 |
|
#include <stdio.h>
int main(void) {
string_name = get_string(“what your name? \n”);
printf(“hello, %s”, name);
}
1 | 跳錯 |
#include <stdio.h>
int main(void) {
for(int i =0; i<10; i++) {
printf(“#\n”);
}
}
1 | ./buggy2 |
#include <cs50.h>
#include <stdio.h>
int main(void)
{
char c1 = ‘H’;
char c2 = ‘I’;
char c3 = ‘!’;
printf(“%c %c %c\n”, c1, c2, c3);
printf(“%i %i %i\n”, c1, c2, c3);
}
1 |
|
make hello
./hello
1 | ----------------- |
#include <cs50.h>
#include <stdio.h>
int main(void) {
string s =get_string(“Input : “);
printf(“Output: “);
for(int i=0; s[i] != '\0'; i++) {
printf("%c", s[i]);
}
printf("\n");
}
1 |
|
#include <cs50.h>
#include <stdio.h>
float average(int length, int array[]);
int main(void){
int n = get_int(“Scores : “);
int scores[n];
for(int i=0; i < n; n++) {
scores[i] = get_int("Score %i", i + 1);
}
printf("Average: %.1f\n", average(n, scores));
}
float average(int length, int array[]) {
int sum =0;
for(int i =0; i <length; i++) {
sum += array[i];
}
return (float)sum / (float)length;
}
```