This blog is under construction

Saturday 28 April 2012

strncpy example in C

Header file:
    string.h

Synopsis:
     char *strncpy(char *dest, char *src, int n);

Description:
     It copies first n characters of string src to string dest.  Returns string dest on success.  Pads with '\0' if src has less than n characters


strncpy function C example:


  #include<stdio.h>
  #include<string.h>

  int main() {
        char data1[100];
        strncpy(data1, "Hello world", 5);
        data1[5] = '\0';
        printf("Output:%s\n", data1);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Output:Hello

No comments:

Post a Comment