summaryrefslogtreecommitdiff
path: root/printf-examples.c
blob: f6ea009b19176dcf2c13f4086da8884f9bfbc707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdlib.h>
#include <stdio.h>
#include <err.h>

//Prints only up to the first 5 characters of a string
void printf_substring()
{
	const char *data = "hello-world";
	printf("First 5 characters = '%.5s'\n", data);


	//If the string is shorter than 5 characters,
	//it must be NUL terminated.
	const char *data2= "foo";
	printf("First 5 characters = '%.5s'\n", data2);
}

void printf_varlen_substr()
{
	const char *data = "hello-world";

	//variable length
	int len=6;
	printf("First %d characters = '%.*s'\n", len, len, data);
}

int main()
{
	printf_substring();
	printf_varlen_substr();
	return 0;
}