#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

  char s1[10]; // room for 10 characters. last character of a string should be '\0'. really room for 9.
  sprintf(s1, "hello"); // s1: 'h', 'e', 'l', 'l', 'o', '\0', _, _, _, _
  printf("%ld\n", strlen(s1)); // strlen: count=0; while (s1[count] != '\0') count++;
  
  char s2[10];
  char s3[10];

  snprintf(s1, 10, "11111111111111111111111111");
  snprintf(s2, 10, "22222222222222222222222222222");
  snprintf(s3, 10, "333333333333333333333");

  printf("s1 %s\ns2 %s\ns3 %s\n", s1, s2, s3);

  printf("%p\n%p\n%p\n", s1, s2, s3);
  
  strncpy(s2, "4", 20); // copies "4" and then '\0' up to 20 of them

  printf("s1 %s\ns2 %s\ns3 %s\n", s1, s2, s3);
  printf("hello\n");


  //
  char *x1 = (char *) malloc(10 * sizeof(char));
  char *x2 = (char *) malloc(10 * sizeof(char));
  char *x3 = (char *) malloc(10 * sizeof(char));

  snprintf(x1, 10, "11111111111111111111111111");
  snprintf(x2, 10, "22222222222222222222222222222");
  snprintf(x3, 10, "333333333333333333333");

  printf("x1 %s\nx2 %s\nx3 %s\n", x1, x2, x3);

  strncpy(x2, "4", 200); // copies "4" and then '\0' up to 20 of them

  printf("x1 %s\nx2 %s\nx3 %s\n", x1, x2, x3);
  
  printf("%p\n%p\n%p\n", x1, x2, x3);
  
  return 0;
}