Friday, 31 May 2013

Null Terminated Strings

The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprises of the string followed by a null character. Following are some of the string functions which terminates the output string in a NULL.
  • strcpy- Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
  • strncpy- Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
  • strcat- Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
  • strcmp- This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
  • strncmp- This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.
  • gets- In gets( ) function, the function reads a line from the standard input stream stdin (stdio.h-standard input output header file) and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line.
 Rest all string functions terminates the output string at null character or '\0'.

No comments:

Post a Comment