Showing posts with label Built-in Functions in C. Show all posts
Showing posts with label Built-in Functions in C. Show all posts

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

C - memcmp function

Synopsis:
#include <stdio.h>
int memcmp(const void* cs, const void* ct, int n);
Description:
The memcmp function compares the first n bytes from cs and ct and returns a value indicating their relationship as follows:
Return Value
if Return value if < 0 then it indicates cs less than ct
if Return value if > 0 then it indicates ct is less than cs
if Return value if = 0 then it indicates cs is equal to ct
Example
#include <stdio.h>
int main()
{
  char hexchars [] = "0123456789ABCDEF";
  char hexchars2 [] = "0123456789abcdef";
  char i;
  i = memcmp (hexchars, hexchars2, 16);
  if (i < 0)
    printf ("hexchars < hexchars2\n");
  else if (i > 0)
    printf ("hexchars >  hexchars2\n");
  else
    printf ("hexchars == hexchars2\n");
  return 0;
}
It will proiduce following result:
hexchars < hexchars2

C - memcpy function

Synopsis:
#include <stdio.h>
void* memcpy(void* s, const void* ct, int n);
Description:
The memcpy function copies n bytes from ct to s. If these memory buffers overlap, the memcpy function cannot guarantee that bytes in ct are copied to s before being overwritten. If these buffers do overlap, use the memmove function.
Return Value
The memcpy function returns dest.
Example
#include <stdio.h>
int main()
{
  char src [100] = "Copy this string to dst1";
  char dst [100];
  char *p;
  p = memcpy (dst, src, sizeof (dst));
  printf ("dst = \"%s\"\n", p);
}
It will proiduce following result:
dst = "Copy this string to dst1"

Sunday, 15 January 2012

Realloc function

Synopsis:
#include <stdio.h>
void *realloc(void *mem_address, int newsize); 
Description:
The size of the memory block pointed to by the mem_address parameter is changed to the newsize bytes, expanding or reducing the amount of memory available in the block.
In case that mem_address is NULL, the function behaves exactly as malloc, assigning a new block of newsize bytes and returning a pointer to the beginning of it.
In case that the newsize is 0, the memory previously allocated in mem_address is deallocated as if a call to free was made, and a NULL pointer is returned.
Return Value
A pointer to the reallocated memory block, which may be either the same as the mem_address argument or a new location.
Example
#include <stdio.h>
int main ()
{
  int input,n;
  int count=0;
  int * numbers = NULL;
  do {
     printf ("Enter an integer value (0 to end): ");
     scanf ("%d", &input);
     count++;
     numbers = (int*) realloc (numbers, count * sizeof(int));
     if (numbers==NULL)
       { puts ("Error (re)allocating memory"); exit (1); }
     numbers[count-1]=input;
  } while (input!=0);
  printf ("Numbers entered: ");
  for (n=0;n<count;n++) printf ("%d ",numbers[n]);
  free (numbers);
  return 0;
}
It will proiduce following result:
Enter an integer value (0 to end): 2
Enter an integer value (0 to end): 3
Enter an integer value (0 to end): 4
Enter an integer value (0 to end): 5
Enter an integer value (0 to end): 0
Numbers entered: 2 3 4 5 0

malloc function

Synopsis:
#include <stdio.h>
void * malloc ( int size );
Description:
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.
Return Value
On success, a pointer to the memory block allocated by the function. If fails a NULL value is returned.
Example
#include <stdio.h>
int main ()
{
  int i,n;
  int * pData;
  printf ("Amount of numbers to be entered: ");
  scanf ("%d",&i);
  pData = (int*) malloc (i*sizeof(int));
  if (pData==NULL) exit (1);
  for (n=0;n<i;n++)
  {
    printf ("Enter number #%d: ",n);
    scanf ("%d",&pData[n]);
  }
  printf ("You have entered: ");
  for (n=0;n<i;n++) printf ("%d ",pData[n]);
  free (pData);
  return 0;
}
It will proiduce following result:
Amount of numbers to be entered: 10
Enter number #0: 2
Enter number #1: 3
Enter number #2: 3
Enter number #3: 4
Enter number #4: 5
Enter number #5: 6
Enter number #6: 7
Enter number #7: 8
Enter number #8: 3
Enter number #9: 9
You have entered: 2 3 3 4 5 6 7 8 3 9

free function

Synopsis:
#include <stdio.h>
void free(void *mem_address);
Description:
A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.
Return Value
None.
Example
#include <stdio.h>
int main ()
{
  int * buffer1, * buffer2, * buffer3;
  buffer1 = (int*) malloc (100*sizeof(int));
  buffer2 = (int*) calloc (100,sizeof(int));
  buffer3 = (int*) realloc (buffer2,500*sizeof(int));
  free (buffer1);
  free (buffer3);
  return 0;
}
It will proiduce following result:
This program has no output.
This is just demonstration of allocation and deallocation of memory.

Calloc Function

Synopsis:
#include <stdio.h>
void *calloc(int num elems, int elem_size); 
Description:
Allocates a block of memory for an array of number elements, each of them size bytes long, and initializes all its bits to zero.
Return Value
A pointer to the memory block allocated by the function.
Example
#include <stdio.h>
int main ()
{
  int i,n;
  int * pData;
  printf ("Amount of numbers to be entered: ");
  scanf ("%d",&i);
  pData = (int*) calloc (i,sizeof(int));
  if (pData==NULL) exit (1);
  for (n=0;n<i;n++)
  {
    printf ("Enter number #%d: ",n);
    scanf ("%d",&pData[n]);
  }
  printf ("You have entered: ");
  for (n=0;n<i;n++) printf ("%d ",pData[n]);
  free (pData);
  return 0;
}
It will proiduce following result:
Amount of numbers to be entered: 10
Enter number #0: 2
Enter number #1: 3
Enter number #2: 3
Enter number #3: 4
Enter number #4: 5
Enter number #5: 6
Enter number #6: 7
Enter number #7: 8
Enter number #8: 3
Enter number #9: 9
You have entered: 2 3 3 4 5 6 7 8 3 9

Thursday, 12 January 2012

Strtok Function

Synopsis:
#include <stdio.h>
char *strtok(char *s, const char *delim) ;
Description:
A sequence of calls to this function split str into tokens, which are sequences of contiguous characters spearated by any of the characters that are part of delimiters.
On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.
To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in separator (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in separator, which becomes the end of the token.
This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.
Return Value
A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.
Example
#include <stdio.h>
int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}
It will proiduce following result:
Splitting string "- This, a sample string." into tokens:
This
a
sample
string

Strstr Function

Synopsis:
#include <stdio.h>
char *strstr(char *string2, char string*1); 
Description:
The strstr function locates the first occurrence of the string string1 in the string string2 and returns a pointer to the beginning of the first occurrence.
Return Value
The strstr function returns a pointer within string2 that points to a string identical to string1. If no such sub string exists in src a null pointer is returned.
Example
#include <stdio.h>
int main() 
{
   char s1 [] = "My House is small";
   char s2 [] = "My Car is green";
   printf ("Returned String 1: %s\n", strstr (s1, "House"));
   printf ("Returned String 2: %s\n", strstr (s2, "Car"));
}
It will proiduce following result:
Returned String 1: House is small
Returned String 2: Car is green

Strrchr Functon

Synopsis:
#include <stdio.h>
char *strrchr(char *string, int c); 
Description:
The strrchr function searches string for the last occurrence of c. The null character terminating string is included in the search.
Return Value
The strrchr function returns a pointer to the last occurrence of character c in string or a null pointer if no matching character is found.
Example
#include <stdio.h>
int main() 
{
  char *s;
  char buf [] = "This is a testing";
  s = strrchr (buf, 't');
  if (s != NULL)
  printf ("found a 't' at %s\n", s);
  return 0;
}
It will proiduce following result:
found a 't' at ting

Strchr Function


Synopsis:
#include <stdio.h>
char *strchr(char *string, int c); 
Description:
The strchr function searches string for the first occurrence of c. The null character terminating string is included in the search.
Return Value
The strchr function returns a pointer to the first occurrence of character c in string or a null pointer if no matching character is found.
Example
#include <stdio.h>
int main() 
{
  char *s;
  char buf [] = "This is a test";
  s = strchr (buf, 't');
  if (s != NULL)
  printf ("found a 't' at %s\n", s);
  return 0;
}
It will proiduce following result:
found a 't' at test

Strncat Functions


Synopsis:
#include <stdio.h>
char *strncat(char *dest, const char *src, int n); 
Description:
The strncat function concatenates or appends first n characters from src to dest. All characters from src are copied including the terminating null character.
Return Value
The strncat function returns dest.
Example
#include <stdio.h>
int main() 
{
  char string1[20];
  char string2[20];
  strcpy(string1, "Hello");
  strcpy(string2, "Hellooo");
  printf("Returned String : %s\n", strncat( string1, string2, 4 ));
  printf("Concatenated String : %s\n", string1 );
  return 0;
}
It will proiduce following result:
Returned String : HelloHell
Concatenated String : HelloHell

Strcat Function


Synopsis:
#include <stdio.h>
char *strcat(char *dest, const char *src); 
Description:
The strcat function concatenates or appends src to dest. All characters from src are copied including the terminating null character.
Return Value
The strcat function returns dest.
Example
#include <stdio.h>
int main() 
{
  char string1[20];
  char string2[20];
  strcpy(string1, "Hello");
  strcpy(string2, "Hellooo");
  printf("Returned String : %s\n", strcat( string1, string2 ));
  printf("Concatenated String : %s\n", string1 );
  return 0;
}
It will proiduce following result:
Returned String : HelloHellooo
Concatenated String : HelloHellooo

Strlen Function


Synopsis:
#include <stdio.h>
int strlen(char *src );
Description:
The strlen function calculates the length, in bytes, of src. This calculation does not include the null terminating character.
Return Value
The strlen function returns the length of src.
Example
#include <stdio.h>
int main()
{
  char string1[20];
  char string2[20];
  strcpy(string1, "Hello");
  strcpy(string2, "Hellooo");
  printf("Length of string1 : %d\n", strlen( string1 ));
  printf("Length of string2 : %d\n", strlen( string2 ));
  return 0;
}
It will proiduce following result:
Length of string1 : 5
Length of string2 : 7

Strncmp Function


Synopsis:
#include <stdio.h>
int strncmp(char *string1, char *string2, int n); 
Description:
The strncmp function compares first n characters of string1 and string2 and returns a value indicating their relationship.
Return Value
if Return value if < 0 then it indicates string1 is less than string2
if Return value if > 0 then it indicates string2 is less than string1
if Return value if = 0 then it indicates string1 is equal to string1
Example
#include <stdio.h>
int main() 
{
  char string1[20];
  char string2[20];
  strcpy(string1, "Hello");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strncmp( string1, string2, 4));
  strcpy(string1, "Helloooo");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strncmp( string1, string2, 10));
  strcpy(string1, "Hellooo");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strncmp( string1, string2, 20));
  return 0;
}
It will proiduce following result:
Return Value is : 0
Return Value is : 111
Return Value is : 0

Strcmp Function

Description:
The strcmp function compares the contents of string1 and string2 and returns a value indicating their relationship.
Return Value
if Return value if < 0 then it indicates string1 is less than string2
if Return value if > 0 then it indicates string2 is less than string1
if Return value if = 0 then it indicates string1 is equal to string1
Example
#include <stdio.h>
int main() 
{
  char string1[20];
  char string2[20];
  strcpy(string1, "Hello");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strcmp( string1, string2));
  strcpy(string1, "Helloooo");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strcmp( string1, string2));
  strcpy(string1, "Hellooo");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strcmp( string1, string2));
  return 0;
}
It will proiduce following result:
Return Value is : -111
Return Value is : 111
Return Value is : 0

Strncpy Function

Synopsis:
#include <stdio.h>
char *strncpy (char *dest, char *src, int n);
Description:
The strcpy function copies n characters from src to dest up to and including the terminating null character if length of src is less than n.
Return Value
The strncpy function returns dest.
Example
#include <stdio.h>
int main()
{
  char input_str[20];
  char *output_str;
  strncpy(input_str, "Hello", 20);
  printf("input_str: %s\n", input_str);
  /* Reset string */
  memset(input_str, '\0', sizeof( input_str ));
  strncpy(input_str, "Hello", 2);
  printf("input_str: %s\n", input_str);
  /* Reset string */
  memset(input_str, '\0', sizeof( input_str ));
  output_str = strncpy(input_str, "World", 3);
  printf("input_str: %s\n", input_str);
  printf("output_str: %s\n", output_str);
  return 0;
}
It will proiduce following result:
input_str: Hello
input_str: He
input_str: Wor
output_str: Wor
host gator coupon