kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

11_strings.c (724B)


      1 /* string.h surface: memcpy / memset / memcmp / memchr.
      2  *
      3  * kit's freestanding runtime intentionally does not ship the NUL-terminated
      4  * str* family (strlen/strcmp/strchr/strstr/...); the project carries lengths
      5  * explicitly. Only the length-based mem* primitives are provided. */
      6 
      7 #include <stdio.h>
      8 #include <string.h>
      9 
     10 int main(void) {
     11   const char* s = "hello, world";
     12   if (memcmp(s, "hello, world", 13) != 0) return 1; /* incl. NUL */
     13   if (memchr(s, ',', 12) != s + 5) return 2;
     14   if (memchr(s, 'z', 12) != NULL) return 3;
     15 
     16   char buf[16];
     17   memset(buf, '#', sizeof(buf));
     18   memcpy(buf, "hi", 2);
     19   buf[2] = 0;
     20   if (memcmp(buf, "hi", 3) != 0) return 4; /* 'h','i','\0' */
     21 
     22   printf("strings ok\n");
     23   return 0;
     24 }