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

int main(void) {
	char format[20] = {0};
	char buf[50] = {0};
	char str[] = "Hello World! How are you?";
	size_t howMuchToRead = 8;

	/* Using sscanf - till whitespace or size specified */
	snprintf(format, sizeof format, "%%%zus", howMuchToRead);
	sscanf(str, format, buf);
	printf("Using sscanf with %%8s format :%s\n", buf);

	/* Using sscanf - read everything upto newline */
	snprintf(format, sizeof format, "%%%zu[^\n]", howMuchToRead);
	sscanf(str, format, buf);
	printf("Using sscanf with %%8[^\\n] format :%s\n", buf);

	/* Using precision specifier */
	snprintf(format, sizeof format, "%%.%zus", howMuchToRead);
	snprintf(buf, sizeof buf, format, str);
	printf("Using precision specifier :%s\n", buf);
	return 0;
}