summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Juyung Seo <juyung.seo@samsung.com>2014-10-30 13:50:11 +0900
committerDaniel Juyung Seo <juyung.seo@samsung.com>2014-10-30 13:50:11 +0900
commit4a8480697042632ac7027f164c73a22a915458ef (patch)
tree088ae171d6a6df6a5cdce280dee3bf48e5700812
parente57333e1ca11ce627618a848d07902139f323b6e (diff)
misc: Add strncpy sample.
-rw-r--r--misc/strncpy.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/misc/strncpy.c b/misc/strncpy.c
new file mode 100644
index 0000000..24be8fe
--- /dev/null
+++ b/misc/strncpy.c
@@ -0,0 +1,31 @@
1// gcc strncpy.c -o strncpy
2#include <stdio.h>
3#include <string.h>
4
5int main(void)
6{
7 char str1[] = "To be or not to be";
8 char str2[40];
9 char str3[4];
10
11 puts("===start==");
12 puts(str1);
13
14 // str2 size is bigger than str1
15 puts("===str2==");
16 strncpy(str2, str1, sizeof(str1)); // rest of area will be filled by NULL
17 puts(str2);
18 strncpy(str2, str1, sizeof(str2));
19 puts(str2);
20
21 // str3 size is smaller than str1
22 puts("===str3==");
23 strncpy(str3, str1, sizeof(str1)); // this will make overflow
24 puts(str3);
25 strncpy(str3, str1, sizeof(str3));
26 puts(str3);
27
28 puts("===end==");
29
30 return 0;
31}