wiki:h-string
Table of Contents
<string.h>
<string.h> provides null-terminated string functions (strlen, strcpy, strcat, strcmp) and memory operations (memcpy, memmove, memset). Avoid strcpy/strcat (unsafe); use bounded variants or snprintf instead.
The mem* functions work on raw bytes; the str* functions assume null termination.
Example
This example tokenizes a CSV line using strtok and measures each field.
// compile: gcc -o stringexample stringexample.c // run: ./stringexample // description: tokenize CSV line by comma and print each field #include <string.h> #include <stdio.h> int main() { char line[] = "alice,30,engineer,london"; char* tok = strtok(line, ","); int field = 0; while (tok) { printf("field %d: \"%s\" (len %zu)\n", field++, tok, strlen(tok)); tok = strtok(NULL, ","); } return 0; }
wiki/h-string.md · Last modified: by 127.0.0.1
