diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-02-07 22:20:47 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-02-07 22:20:47 +0000 |
commit | cabe1a14b31edce1a247c367f8e1d7265abec34e (patch) | |
tree | e66fd91f6682d084ea8484b1be43bd5631dc7610 /ex5-5.c | |
parent | 7fa502ba4e60ce762ab529996c66a1a831cb789f (diff) | |
download | learn-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.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -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 |