I just run this code and what i get for n=1 is not what i expect to get. Can you explain why is this happening?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXRIGA 11
int main()
{
char s[MAXRIGA+2];
char a[MAXRIGA]="pippo";
strncpy(s, a, 1); // n=1
printf("%s", s);
return 0;
}
returns
pF
instead if n=2 or more i get what i want.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXRIGA 11
int main()
{
char s[MAXRIGA+2];
char a[MAXRIGA]="pippo";
strncpy(s, a, 2); // n=2
printf("%s", s);
return 0;
}
returns
pi