Jul 7, 2024
#include <stdio.h>
int main(void) {
int n = 50;
printf("%p\n", &n);
return 0;
}
#include <stdio.h>
int main(void) {
int n = 50;
int *p = &n;
printf("%p\n", p);
return 0;
}
#include <stdio.h>
int main(void) {
int n = 50;
int *p = &n;
printf("%d\n", *p);
return 0;
}
p.#include <cs50.h>
#include <stdio.h>
int main(void) {
string s = "HI";
printf("%p\n", s);
printf("%p\n", &s[0]);
printf("%p\n", &s[1]);
printf("%p\n", &s[2]);
return 0;
}
char*#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *s = get_string("Enter a string: ");
if (s == NULL) { return 1; }
// Allocate memory
char *t = malloc((strlen(s) + 1) * sizeof(char));
strcpy(t, s);
// Capitalize first letter in t
if (strlen(t) > 0) {
t[0] = toupper(t[0]);
}
printf("%s\n", s);
printf("%s\n", t);
// Free memory
free(t);
return 0;
}
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void) {
FILE *file = fopen("phonebook.csv", "a");
if (file == NULL) {
return 1;
}
char *name = get_string("Name: ");
char *number = get_string("Number: ");
fprintf(file, "%s,%s\n", name, number);
fclose(file);
return 0;
}
CP command to copy contents from one file to another.#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t byte;
int main(int argc, char *argv[]) {
if (argc != 3) {
return 1;
}
FILE *source = fopen(argv[1], "r");
FILE *dest = fopen(argv[2], "w");
if (source == NULL || dest == NULL) {
return 1;
}
byte buffer;
while (fread(&buffer, sizeof(byte), 1, source)) {
fwrite(&buffer, sizeof(byte), 1, dest);
}
fclose(source);
fclose(dest);
return 0;
}
fread and fwrite.Emoji: 🖥️