Powered By Blogger

Monday 14 January 2013

Functions

Functions as u have seen earlier ,used to transfer messages and data in our C Program. Functions contain all the information/ data. it can be single level function as well as multi-level function.
Single Level Function
Consider the following single-level function :

main()
{
printf("single-level function\n");
}

So, in the above illustration main is the only function available .

Multi-level Function
you can create multiple functions in your C Program ,or you can say whenever a user create a function by itself (user-created function) .
Consider this :

main()
{
-
-
-
}
cprog()
{
-
-
-
}
...

so, here you can see cprog() ,is a user0defined function ,added to the C code ,that's multi-level function's example .

There are some predefined Functions available in library :-

strlen() is used to find length of a given string
strcpy() is used to copy strings
strcmp() is used to compare strings
strcat() is used to concantinate strings

strlen() example: 

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

int main ()
{
  char szInput[256];
  printf ("Enter a sentence: ");
  gets (szInput);
  printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
  return 0;
}

 strcpy() example


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

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

strcat() example


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

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}




No comments:

Post a Comment