I have not been able to find any information with a web-search. Where should I be looking?
-
3`man strncpy` here you go – Jesus Ramos May 02 '13 at 22:07
-
@Ryan, Or leave it and mention that it's C++ since he might have meant that. – ikegami May 02 '13 at 22:12
-
Avoid `strncpy`: https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ – chqrlie Dec 04 '16 at 13:11
7 Answers
char myString[256]; // Input string
char dest[256]; // Destination string
strncpy(dest, myString, 10);
dest[10] = 0; // null terminate destination

- 4,587
- 6
- 31
- 60

- 2,806
- 1
- 24
- 23
-
-
3if you want to truncate the string to 10 chars then put myString[10] = 0; (This given that myString points to memory and not a string lettral) – Lefteris E May 02 '13 at 22:14
-
@user2341069 no you cant, as per the man page "The strings may not overlap, and the destination string dest must be large enough to receive the copy." As myString is a pointer to a char array in memory, using the same pointer twice means that they "overlap" in memory, and thus will cause an error. Essentially, this means that the src can not be a substring of dst or vice versa. – TonyArra May 02 '13 at 22:18
-
1
-
2
-
@LefterisE Thank you. It seems they are equivalent but '\0' is often used to indicate a character instead of a number. This question http://stackoverflow.com/questions/16955936/string-termination-char-c-0-vs-char-c-0 has a few sources in it. – TafT May 03 '16 at 14:45
-
I'm afraid I disagree: `strncpy()` has widely misunderstood semantics and is **very** error prone. **DO NOT USE THIS FUNCTION**. Learn why here: https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ – chqrlie Dec 04 '16 at 13:00
char source[] = "abcdefthijklmn";
char target[100];
strncpy(target, source, 10);
target[10] = '\0'; // IMPORTANT!

- 2,155
- 2
- 19
- 28
-
-
3target is not uninitialized pointer. target points to 100 bytes of memory in the stack. the \0 is not nessesary as strncpy will put it – Lefteris E May 02 '13 at 22:13
-
2@LefterisE Not sure about the initialization thing, but `strncpy` will not put a `'\0'` automatically – tianz May 02 '13 at 22:14
-
@LefterisE When I commented, the declaration was `char *target;`. – michaelb958--GoFundMonica May 02 '13 at 22:15
-
1@michaelb958 Yes it was. Thank you. I'm a bit confused with `char *a` and `char a[100]` – tianz May 02 '13 at 22:17
-
i am confused aswell about how source is declared. maybe source should have been char* as it is a pointer to a string literal – Lefteris E May 02 '13 at 22:18
-
@LefterisE I'm fairly sure that `char foo[] = "bar";` initialises `foo` with a copy of `"bar"`. – michaelb958--GoFundMonica May 02 '13 at 22:21
-
I'm afraid I disagree: `strncpy()` has widely misunderstood semantics and is **very** error prone. **DO NOT USE THIS FUNCTION**. Learn why here: https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ – chqrlie Dec 04 '16 at 13:00
Adding to the above answers:
char* someString = "your string goes here";
int main()
{
int n = 10;
printf("(%.*s)\n", n, someString);
return 0;
}

- 884
- 1
- 11
- 40
There are many different ways to achieve your goal:
You can use
snprintf
(safest):char source[] = "abcdefthijklmn"; char target[100]; snprintf(target, sizeof target, "%.10s", source);
You can use
strncat
if you know the destination has at least 11 elements:char source[] = "abcdefthijklmn"; char target[100]; *target = '\0'; strncat(target, source, 10);
You can use
strlen
andmemcpy
(same assumption about the size of destination):char source[] = "abcdefthijklmn"; char target[100]; size_t len = strlen(source); if (len > 10) len = 10; memcpy(target, source, len); target[len] = '\0';
You can use a loop (same assumption about the size of destination):
char source[] = "abcdefthijklmn"; char target[100]; size_t i; for (i = 0; i < 10; i++) { target[i] = source[i]; } target[i] = '\0';

- 131,814
- 10
- 121
- 189
If you're looking for a good source, here's an example of an online man page you can use: http://linux.die.net/man/3/strncpy
Important to note: you could also use memcpy instead of strncpy, but it requires you to add your own null-terminating byte.
Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
Hence, memcpy and strncpy work almost the same here, and memcpy is more efficient and less prone to error.

- 10,607
- 1
- 30
- 46
I got it to work like this.
# include <stdio.h>
# include <string.h>
//Strings. String lenght terminator.
//KHO2016.no1. mingw (TDM-GCC-32) . c-ansi .
int main ()
{
//declare
char src_str[20],dst_str[10];
//valuate
printf ("Enter a sentance of 20 letters\n");
gets (src_str);
strcpy (dst_str,src_str);
//calculate
dst_str [10] ='\0'; // from the "www.stack overflow"
printf ("%s",dst_str);
printf ("\n");
//terminate
return 0;
}

- 1
- 4
You can also use sprintf
with .10
precision format:
#include <stdio.h>
#include <string.h>
int main(void)
{
char source[] = "abcdefghijklmnopqrstuvwxyz";
char dest[11];
memset(dest, '\0', sizeof(dest));
sprintf(dest, "%.10s", source);
printf("%s", dest); // abcdefghij
return 0;
}

- 11,026
- 5
- 30
- 49