summaryrefslogtreecommitdiff
path: root/ex5-5.c
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-02-07 22:20:47 +0000
committerMartin Ashby <martin@ashbysoft.com>2023-02-07 22:20:47 +0000
commitcabe1a14b31edce1a247c367f8e1d7265abec34e (patch)
treee66fd91f6682d084ea8484b1be43bd5631dc7610 /ex5-5.c
parent7fa502ba4e60ce762ab529996c66a1a831cb789f (diff)
downloadlearn-c-cabe1a14b31edce1a247c367f8e1d7265abec34e.tar.gz
learn-c-cabe1a14b31edce1a247c367f8e1d7265abec34e.tar.bz2
learn-c-cabe1a14b31edce1a247c367f8e1d7265abec34e.tar.xz
learn-c-cabe1a14b31edce1a247c367f8e1d7265abec34e.zip
Chapter 5
Diffstat (limited to 'ex5-5.c')
-rw-r--r--ex5-5.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/ex5-5.c b/ex5-5.c
new file mode 100644
index 0000000..a6af275
--- /dev/null
+++ b/ex5-5.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+
+char* z_strncpy(char *dest, char *src, size_t n) {
+ char* max = dest + n;
+ for (; *src != '\0' && dest < max; dest++, src++) {
+ *dest = *src;
+ }
+ for (; dest < max; dest++) {
+ *dest = '\0';
+ }
+ return dest;
+}
+
+int main(void) {
+ char buf[100] = "";
+ z_strncpy(buf, "Hello, World", 5);
+ printf("Hello = %s\n", buf);
+ z_strncpy(buf, "Hello, World", 20);
+ printf("Hello, World = [%s]\n", buf);
+} \ No newline at end of file