Friday 20 January 2012

Built in functions in C

String Manipulation Functions


char *strcpy (char *dest, char *src); 
Copy src string into dest string.
char *strncpy(char *string1, char *string2, int n); 
Copy first n characters of string2 to stringl .
int strcmp(char *string1, char *string2); 
Compare string1 and string2 to determine alphabetic order.
int strncmp(char *string1, char *string2, int n); 
Compare first n characters of two strings.
int strlen(char *string); 
Determine the length of a string.
char *strcat(char *dest, const char *src); 
Concatenate string src to the string dest.
char *strncat(char *dest, const char *src, int n); 
Concatenate n chracters from string src to the string dest.
char *strchr(char *string, int c); 
Find first occurrence of character c in string.
char *strrchr(char *string, int c); 
Find last occurrence of character c in string.
char *strstr(char *string2, char string*1); 
Find first occurrence of string string1 in string2.
char *strtok(char *s, const char *delim) ;
Parse the string s into tokens using delim as delimiter.


Memory Management Functions

void *calloc(int num elems, int elem_size); 
Allocate an array and initialise all elements to zero .
void free(void *mem address);
Free a block of memory.
void *malloc(int num bytes); 
Allocate a block of memory.
void *realloc(void *mem address, int newsize); 
Reallocate (adjust size) a block of memory.

Buffer Manipulation



void* memcpy(void* s, const void* ct, int n);
Copies n characters from ct to s and returns s. s may be corrupted if objects overlap.
int memcmp(const void* cs, const void* ct, int n);
Compares at most (the first) n characters of cs and ct, returning negative value if cs<ct, zero if cs==ct, positive value if cs>ct.
void* memchr(const void* cs, int c, int n);
Returns pointer to first occurrence of c in first n characters of cs, or NULL if not found.
void* memset(void* s, int c, int n); 
Replaces each of the first n characters of s by c and returns s.
void* memmove(void* s, const void* ct, int n); 
Copies n characters from ct to s and returns s. s will not be corrupted if objects overlap.

Tuesday 17 January 2012

C - memmove

Synopsis:
#include <stdio.h>
void* memmove(void* s, const void* ct, int n); 
Description:
Copies n characters from ct to s and returns s. s will not be corrupted if objects overlap.
Return Value
The memmove function returns s after moving n characters.
Example
#include <stdio.h>
int main() {
  static char buf [] = "This is line 1 \n"
                       "This is line 2 \n"
                       "This is line 3 \n";
  printf ("buf before = %s\n", buf);
  memmove (&buf [0], &buf [16], 32);
  printf ("buf after = %s\n", buf);
  return 0;
}
It will proiduce following result:
buf before = This is line 1
This is line 2
This is line 3
buf after = This is line 2
This is line 3
This is line 3

C - memset

Synopsis:
#include <stdio.h>
void* memset(void* s, int c, int n);
Description:
The memset function sets the first n bytes in s to c. Generally this function is used to set a memory location to null chracters '\0'.
Return Value
The memset function returns s with the set value to c
Example
#include <stdio.h>
int main() {
  char string[20];
  strcpy(string, "Hello");
  printf( "Before using memset |%s|\n", string );
  memset( string, '\0', sizeof(string) );
  printf( "After using memset |%s|\n", string );
  return 0;
}
It will proiduce following result:
Before using memset |Hello|
After using memset ||

C - memchr

Synopsis:
#include <stdio.h>
void* memchr(const void* cs, int c, int n);
Description:
The memchr function scans cs for the character c in the first n bytes of the buffer.
Return Value
The memchr function returns a pointer to the character c in cs or a null pointer if the character was not found.
Example
#include <stdio.h>
int main()
{
  char src [100] = "Search this string from the start";
  char *c;
  c = (char*)memchr (src, 'g', sizeof (src));
  if (c == NULL)
    printf ("'g' was not found in the src\n");
  else
    printf ("found 'g' in the src\n");
  return 0;
}
It will proiduce following result:
found 'g' in the src
host gator coupon