Memory slides
Memory slides
1 // Prints an integer
2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 int n = 50;
8 printf("%i\n", n);
9 }
addresses1.c
1 // Prints a string
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 string s = "HI!";
9 printf("%s\n", s);
10 }
addresses5.c
1 // Capitalizes a string
2
3 #include <cs50.h>
4 #include <ctype.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 int main(void)
9 {
10 // Get a string
11 string s = get_string("s: ");
12
13 // Copy string's address
14 string t = s;
15
16 // Capitalize first letter in string
17 t[0] = toupper(t[0]);
18
19 // Print string twice
20 printf("s: %s\n", s);
21 printf("t: %s\n", t);
22 }
copy1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main(void)
5 {
6 int scores[1024];
7 for (int i = 0; i < 1024; i++)
8 {
9 printf("%i\n", scores[i]);
10 }
11 }
swap0.c
1 // Incorrectly gets a string from user using scanf; compile with -Wno-uninitialized
2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 char *s;
8 printf("s: ");
9 scanf("%s", s);
10 printf("s: %s\n", s);
11 }
get3.c
1 // Copies a file
2
3 #include <stdio.h>
4 #include <stdint.h>
5
6 typedef uint8_t BYTE;
7
8 int main(int argc, char *argv[])
9 {
10 FILE *src = fopen(argv[1], "rb");
11 FILE *dst = fopen(argv[2], "wb");
12
13 BYTE b;
14
15 while (fread(&b, sizeof(b), 1, src) != 0)
16 {
17 fwrite(&b, sizeof(b), 1, dst);
18 }
19
20 fclose(dst);
21 fclose(src);
22 }