Formatting numbers into strings (snprintf) and parsing structured data fields out of formatted strings (sscanf) are foundational string operations in C C-standard library <stdio.h>.

snprintf and sscanf Code Example

str_format.cc
#include <stdio.h>
 
int main(void) {
    char buffer[64];
    int port = 8080;
    const char *host = "localhost";
 
    // 1. Format numbers and strings safely with snprintf
    snprintf(buffer, sizeof(buffer), "http://%s:%d/api", host, port);
    printf("Formatted: %s\n", buffer);
 
    // 2. Parse values from string using sscanf
    const char *input = "SENSOR_01: 24.5C";
    char sensor_id[16];
    float temp;
    
    if (sscanf(input, "%15[^:]: %fC", sensor_id, &temp) == 2) {
        printf("Parsed Sensor: %s, Temperature: %.1f\n", sensor_id, temp);
    }
 
    return 0;
}