tag:blogger.com,1999:blog-14275121044104536732024-03-05T12:08:35.093+05:30Let us learn "C"This blog covers all the topics of C-language which concentrates on Engineering syllabus,GATE,Placement Questions of Multi National Companies.
This archive contains a complete course for you to learn the 'C' computer language itself.suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.comBlogger64125tag:blogger.com,1999:blog-1427512104410453673.post-63730508347674326992012-01-20T14:02:00.000+05:302012-05-15T16:17:54.611+05:30Built in functions in C<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: large;"><span style="color: blue;">String Manipulation Functions</span><br /></span></b><br />
<br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><a href="http://letuslearncbysurya.blogspot.com/2012/01/string-copy.html" target="_blank">char *strcpy (char *dest, char *src);</a> </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Copy src string into dest string.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/string-copy.html" style="font-family: Verdana, sans-serif;" target="_blank">char *strncpy(char *string1, char *string2, int n);</a><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Copy first n characters of string2 to stringl .</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/string-compare-function.html" style="font-family: Verdana, sans-serif;" target="_blank">int strcmp(char *string1, char *string2);</a><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Compare string1 and string2 to determine alphabetic order.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/strncmp-function.html" style="font-family: Verdana, sans-serif;" target="_blank">int strncmp(char *string1, char *string2, int n); </a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Compare first n characters of two strings.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/strlen-function.html" style="font-family: Verdana, sans-serif;" target="_blank">int strlen(char *string); </a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Determine the length of a string.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/strcat-function.html" style="font-family: Verdana, sans-serif;" target="_blank">char *strcat(char *dest, const char *src); </a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Concatenate string src to the string dest.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/strcat-function.html" style="font-family: Verdana, sans-serif;" target="_blank">char *strncat(char *dest, const char *src, int n); </a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Concatenate n chracters from string src to the string dest.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/strchr-function.html" style="font-family: Verdana, sans-serif;" target="_blank">char *strchr(char *string, int c); </a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Find first occurrence of character c in string.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/strrchr-functon.html" style="font-family: Verdana, sans-serif;" target="_blank">char *strrchr(char *string, int c); </a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Find last occurrence of character c in string.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/strstr-function.html" style="font-family: Verdana, sans-serif;" target="_blank">char *strstr(char *string2, char string*1); </a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Find first occurrence of string string1 in string2.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/strtok-function.html" style="font-family: Verdana, sans-serif;" target="_blank">char *strtok(char *s, const char *delim) ;</a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Parse the string s into tokens using delim as delimiter.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><br /></span><br />
<span class="Apple-style-span" style="color: blue; font-family: Verdana, sans-serif; font-size: large;"><b>Memory Management Functions</b></span><br />
<br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><a href="http://letuslearncbysurya.blogspot.com/2012/01/calloc-function.html" target="_blank">void *calloc(int num elems, int elem_size); </a></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Allocate an array and initialise all elements to zero .</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/free-function.html" style="font-family: Verdana, sans-serif;" target="_blank">void free(void *mem address);</a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Free a block of memory.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/malloc-function.html" style="font-family: Verdana, sans-serif;" target="_blank">void *malloc(int num bytes); </a><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Allocate a block of memory.</span><br />
<a href="http://letuslearncbysurya.blogspot.com/2012/01/realloc-function.html" style="font-family: Verdana, sans-serif;" target="_blank">void *realloc(void *mem address, int newsize);</a><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Reallocate (adjust size) a block of memory.</span><br />
<div>
<b style="background-color: white; font-family: Verdana, sans-serif; font-size: x-large; line-height: 36px;"><br /><span style="color: blue;">Buffer Manipulation</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><br /></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="background-color: transparent; color: blue; text-align: justify;"><a class="func" href="http://letuslearncbysurya.blogspot.com/2012/01/c-memcpy-function.html" style="background-color: transparent; text-align: justify;" target="_blank">void* memcpy(void* s, const void* ct, int n);</a></span><br />Copies n characters from ct to s and returns s. s may be corrupted if objects overlap.<br /><a class="func" href="http://letuslearncbysurya.blogspot.com/2012/01/c-memcmp-function.html" style="background-color: transparent; text-align: justify;" target="_blank"><span class="Apple-style-span" style="color: blue;">int memcmp(const void* cs, const void* ct, int n);</span></a><br />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.<br /><a class="func" href="http://letuslearncbysurya.blogspot.com/2012/01/c-memchr-function.html" style="background-color: transparent; text-align: justify;" target="_blank"><span class="Apple-style-span" style="color: blue;">void* memchr(const void* cs, int c, int n);</span></a><br />Returns pointer to first occurrence of c in first n characters of cs, or NULL if not found.<br /><a class="func" href="http://letuslearncbysurya.blogspot.com/2012/01/c-memset-function.html" style="background-color: transparent; text-align: justify;" target="_blank">void* memset(void* s, int c, int n);</a><span class="Apple-style-span" style="color: blue;"> </span><br />Replaces each of the first n characters of s by c and returns s.<br /><span class="Apple-style-span" style="color: blue;"><a class="func" href="http://letuslearncbysurya.blogspot.com/2012/01/c-memmove-function.html" style="background-color: transparent; text-align: justify;" target="_blank">void* memmove(void* s, const void* ct, int n);</a> </span><br /><span class="Apple-style-span" style="background-color: white;">Copies n characters from ct to s and returns s. s will not be corrupted if objects overlap.</span></span></div>
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-33760456072104549162012-01-17T14:39:00.001+05:302012-01-17T15:20:07.421+05:30C - memmove<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">void* memmove(void* s, const void* ct, int n); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Copies n characters from ct to s and returns s. s will not be corrupted if objects overlap.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The memmove function returns s after moving n characters.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() {</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> static char buf [] = "This is line 1 \n"</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> "This is line 2 \n"</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> "This is line 3 \n";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("buf before = %s\n", buf);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> memmove (&buf [0], &buf [16], 32);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("buf after = %s\n", buf);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">buf before = This is line 1</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This is line 2</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This is line 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">buf after = This is line 2</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This is line 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This is line 3</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-61799547010214274672012-01-17T14:25:00.002+05:302012-01-17T15:20:33.986+05:30C - memset<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">void* memset(void* s, int c, int n);</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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'.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The memset function returns s with the set value to c</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() {</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string, "Hello");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf( "Before using memset |%s|\n", string );</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> memset( string, '\0', sizeof(string) );</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf( "After using memset |%s|\n", string );</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Before using memset |Hello|</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">After using memset ||</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-50456918790774361992012-01-17T14:24:00.003+05:302012-01-17T15:20:48.556+05:30C - memchr<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">void* memchr(const void* cs, int c, int n);</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The memchr function scans cs for the character c in the first n bytes of the buffer.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The memchr function returns a pointer to the character c in cs or a null pointer if the character was not found.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main()</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char src [100] = "Search this string from the start";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char *c;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> c = (char*)memchr (src, 'g', sizeof (src));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> if (c == NULL)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("'g' was not found in the src\n");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> else</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("found 'g' in the src\n");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">found 'g' in the src</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-68675699132975104052012-01-17T14:22:00.003+05:302012-01-17T15:21:03.618+05:30C - memcmp function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int memcmp(const void* cs, const void* ct, int n);</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The memcmp function compares the first n bytes from cs and ct and returns a value indicating their relationship as follows:</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if < 0 then it indicates cs less than ct</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if > 0 then it indicates ct is less than cs</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if = 0 then it indicates cs is equal to ct</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main()</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char hexchars [] = "0123456789ABCDEF";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char hexchars2 [] = "0123456789abcdef";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char i;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i = memcmp (hexchars, hexchars2, 16);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> if (i < 0)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("hexchars < hexchars2\n");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> else if (i > 0)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("hexchars > hexchars2\n");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> else</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("hexchars == hexchars2\n");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">hexchars < hexchars2</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-74447513833036318492012-01-17T14:20:00.001+05:302012-01-17T15:21:21.864+05:30C - memcpy function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">void* memcpy(void* s, const void* ct, int n);</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The memcpy function returns dest.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char src [100] = "Copy this string to dst1";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char dst [100];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char *p;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> p = memcpy (dst, src, sizeof (dst));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("dst = \"%s\"\n", p);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">dst = "Copy this string to dst1"</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-50432556694911252742012-01-15T13:38:00.003+05:302012-01-15T13:38:52.548+05:30Realloc function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">void *realloc(void *mem_address, int newsize); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">A pointer to the reallocated memory block, which may be either the same as the mem_address argument or a new location.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main ()</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int input,n;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int count=0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int * numbers = NULL;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> do {</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Enter an integer value (0 to end): ");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> scanf ("%d", &input);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> count++;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> numbers = (int*) realloc (numbers, count * sizeof(int));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> if (numbers==NULL)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> { puts ("Error (re)allocating memory"); exit (1); }</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> numbers[count-1]=input;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> } while (input!=0);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Numbers entered: ");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> for (n=0;n<count;n++) printf ("%d ",numbers[n]);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> free (numbers);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter an integer value (0 to end): 2</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter an integer value (0 to end): 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter an integer value (0 to end): 4</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter an integer value (0 to end): 5</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter an integer value (0 to end): 0</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Numbers entered: 2 3 4 5 0</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-16366420389624317142012-01-15T12:55:00.000+05:302012-01-15T13:39:16.170+05:30malloc function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">void * malloc ( int size );</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">On success, a pointer to the memory block allocated by the function. If fails a NULL value is returned.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main ()</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int i,n;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int * pData;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Amount of numbers to be entered: ");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> scanf ("%d",&i);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> pData = (int*) malloc (i*sizeof(int));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> if (pData==NULL) exit (1);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> for (n=0;n<i;n++)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Enter number #%d: ",n);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> scanf ("%d",&pData[n]);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("You have entered: ");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> for (n=0;n<i;n++) printf ("%d ",pData[n]);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> free (pData);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Amount of numbers to be entered: 10</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #0: 2</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #1: 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #2: 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #3: 4</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #4: 5</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #5: 6</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #6: 7</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #7: 8</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #8: 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #9: 9</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">You have entered: 2 3 3 4 5 6 7 8 3 9</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-34810274466822901922012-01-15T12:51:00.000+05:302012-01-15T13:41:49.163+05:30free function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">void free(void *mem_address);</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">None.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main ()</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int * buffer1, * buffer2, * buffer3;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> buffer1 = (int*) malloc (100*sizeof(int));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> buffer2 = (int*) calloc (100,sizeof(int));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> buffer3 = (int*) realloc (buffer2,500*sizeof(int));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> free (buffer1);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> free (buffer3);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This program has no output.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This is just demonstration of allocation and deallocation of memory.</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-67184771636204468712012-01-15T12:43:00.002+05:302012-01-15T13:39:40.799+05:30Calloc Function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">void *calloc(int num elems, int elem_size); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Allocates a block of memory for an array of number elements, each of them size bytes long, and initializes all its bits to zero.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">A pointer to the memory block allocated by the function.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main ()</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int i,n;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int * pData;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Amount of numbers to be entered: ");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> scanf ("%d",&i);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> pData = (int*) calloc (i,sizeof(int));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> if (pData==NULL) exit (1);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> for (n=0;n<i;n++)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Enter number #%d: ",n);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> scanf ("%d",&pData[n]);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("You have entered: ");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> for (n=0;n<i;n++) printf ("%d ",pData[n]);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> free (pData);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Amount of numbers to be entered: 10</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #0: 2</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #1: 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #2: 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #3: 4</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #4: 5</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #5: 6</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #6: 7</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #7: 8</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #8: 3</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Enter number #9: 9</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">You have entered: 2 3 3 4 5 6 7 8 3 9</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-62329825552196286602012-01-12T21:11:00.000+05:302012-01-15T13:39:49.345+05:30Strtok Function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">char *strtok(char *s, const char *delim) ;</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">A pointer to the last token found in string.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">A null pointer is returned if there are no tokens left to retrieve.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main ()</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char str[] ="- This, a sample string.";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char * pch;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Splitting string \"%s\" into tokens:\n",str);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> pch = strtok (str," ,.-");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> while (pch != NULL)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("%s\n",pch);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> pch = strtok (NULL, " ,.-");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Splitting string "- This, a sample string." into tokens:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">a</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">sample</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">string</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-29451435148240536612012-01-12T21:04:00.002+05:302012-01-15T13:39:59.911+05:30Strstr Function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">char *strstr(char *string2, char string*1); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char s1 [] = "My House is small";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char s2 [] = "My Car is green";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Returned String 1: %s\n", strstr (s1, "House"));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("Returned String 2: %s\n", strstr (s2, "Car"));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Returned String 1: House is small</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Returned String 2: Car is green</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-81368412064277209502012-01-12T21:02:00.000+05:302012-01-15T13:40:11.260+05:30Strrchr Functon<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">char *strrchr(char *string, int c); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The strrchr function searches string for the last occurrence of c. The null character terminating string is included in the search.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char *s;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char buf [] = "This is a testing";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> s = strrchr (buf, 't');</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> if (s != NULL)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("found a 't' at %s\n", s);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">found a 't' at ting</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-75153711057753919812012-01-12T20:58:00.000+05:302012-01-12T20:58:41.748+05:30Strchr Function<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">char *strchr(char *string, int c); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The strchr function searches string for the first occurrence of c. The null character terminating string is included in the search.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">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.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char *s;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char buf [] = "This is a test";</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> s = strchr (buf, 't');</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> if (s != NULL)</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf ("found a 't' at %s\n", s);</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">found a 't' at test</span><br />
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-6066786994678405752012-01-12T20:55:00.003+05:302012-01-12T20:55:30.380+05:30Strncat Functions<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">char *strncat(char *dest, const char *src, int n); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The strncat function concatenates or appends first n characters from src to dest. All characters from src are copied including the terminating null character.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The strncat function returns dest.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string1[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string2[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string1, "Hello");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string2, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Returned String : %s\n", strncat( string1, string2, 4 ));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Concatenated String : %s\n", string1 );</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Returned String : HelloHell</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Concatenated String : HelloHell</span><br />
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-9750327568794522482012-01-12T20:49:00.002+05:302012-01-12T20:49:52.844+05:30Strcat Function<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">char *strcat(char *dest, const char *src); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The strcat function concatenates or appends src to dest. All characters from src are copied including the terminating null character.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The strcat function returns dest.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string1[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string2[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string1, "Hello");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string2, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Returned String : %s\n", strcat( string1, string2 ));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Concatenated String : %s\n", string1 );</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Returned String : HelloHellooo</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Concatenated String : HelloHellooo</span><br />
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-81576301263579137652012-01-12T20:46:00.002+05:302012-01-12T20:46:43.196+05:30Strlen Function<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b>Synopsis:</b><br />
#include <stdio.h><br />
int strlen(char *src );<br />
<b>Description:</b><br />
The strlen function calculates the length, in bytes, of src. This calculation does not include the null terminating character.<br />
<b>Return Value</b><br />
The strlen function returns the length of src.<br />
<b>Example</b><br />
#include <stdio.h><br />
int main()<br />
{<br />
char string1[20];<br />
char string2[20];<br />
strcpy(string1, "Hello");<br />
strcpy(string2, "Hellooo");<br />
printf("Length of string1 : %d\n", strlen( string1 ));<br />
printf("Length of string2 : %d\n", strlen( string2 ));<br />
return 0;<br />
}<br />
It will proiduce following result:<br />
Length of string1 : 5<br />
Length of string2 : 7<br />
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-54017853214144793092012-01-12T20:42:00.002+05:302012-01-12T20:42:52.283+05:30Strncmp Function<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Synopsis:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int strncmp(char *string1, char *string2, int n); </span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The strncmp function compares first n characters of string1 and string2 and returns a value indicating their relationship.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if < 0 then it indicates string1 is less than string2</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if > 0 then it indicates string2 is less than string1</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if = 0 then it indicates string1 is equal to string1</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string1[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string2[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string1, "Hello");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string2, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Return Value is : %d\n", strncmp( string1, string2, 4));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string1, "Helloooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string2, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Return Value is : %d\n", strncmp( string1, string2, 10));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string1, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string2, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Return Value is : %d\n", strncmp( string1, string2, 20));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value is : 0</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value is : 111</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value is : 0</span><br />
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-50067976190526901812012-01-12T20:38:00.002+05:302012-01-12T20:43:24.542+05:30Strcmp Function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Description:</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The strcmp function compares the contents of string1 and string2 and returns a value indicating their relationship.</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if < 0 then it indicates string1 is less than string2</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if > 0 then it indicates string2 is less than string1</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">if Return value if = 0 then it indicates string1 is equal to string1</span><br />
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Example</span></b><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">#include <stdio.h></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int main() </span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string1[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char string2[20];</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string1, "Hello");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string2, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Return Value is : %d\n", strcmp( string1, string2));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string1, "Helloooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string2, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Return Value is : %d\n", strcmp( string1, string2));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string1, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> strcpy(string2, "Hellooo");</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("Return Value is : %d\n", strcmp( string1, string2));</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">}</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">It will proiduce following result:</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value is : -111</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value is : 111</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Return Value is : 0</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-55231006951553779502012-01-12T20:37:00.000+05:302012-01-12T20:44:30.402+05:30Strncpy Function<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Synopsis</b>:<br />
#include <stdio.h><br />
char *strncpy (char *dest, char *src, int n);<br />
<b>Description:</b><br />
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.<br />
<b>Return Value</b><br />
The strncpy function returns dest.<br />
<b>Example</b><br />
#include <stdio.h><br />
int main()<br />
{<br />
char input_str[20];<br />
char *output_str;<br />
strncpy(input_str, "Hello", 20);<br />
printf("input_str: %s\n", input_str);<br />
/* Reset string */<br />
memset(input_str, '\0', sizeof( input_str ));<br />
strncpy(input_str, "Hello", 2);<br />
printf("input_str: %s\n", input_str);<br />
/* Reset string */<br />
memset(input_str, '\0', sizeof( input_str ));<br />
output_str = strncpy(input_str, "World", 3);<br />
printf("input_str: %s\n", input_str);<br />
printf("output_str: %s\n", output_str);<br />
return 0;<br />
}<br />
It will proiduce following result:<br />
input_str: Hello<br />
input_str: He<br />
input_str: Wor<br />
output_str: Wor</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-59285200010744723932012-01-12T20:34:00.002+05:302012-01-12T20:44:16.750+05:30Strcpy Function<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span class="Apple-style-span" style="color: red;">char *strcpy (char *dest, char *src);</span></b><br />
<b>Synopsis:</b><br />
#include <stdio.h><br />
char *strcpy (char *dest, char *src);<br />
<b><span class="Apple-style-span" style="color: #274e13;">Description:</span></b><br />
The strcpy function copies characters from src to dest up to and including the terminating null character.<br />
<b>Return Value</b><br />
The strcpy function returns dest.<br />
<b><span class="Apple-style-span" style="color: orange;">Example</span></b><br />
#include <stdio.h><br />
int main() {<br />
char input_str[20];<br />
char *output_str;<br />
strcpy(input_str, "Hello");<br />
printf("input_str: %s\n", input_str);<br />
output_str = strcpy(input_str, "World");<br />
printf("input_str: %s\n", input_str);<br />
printf("output_str: %s\n", output_str);<br />
return 0;<br />
}<br />
It will proiduce following result:<br />
input_str: Hello<br />
input_str: World<br />
output_str: World</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-28814600656839222112012-01-10T21:29:00.000+05:302012-01-10T21:30:49.771+05:30Functions in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="background-color: white; color: #222222; line-height: 25px;">In programming, a big task is divided into smaller ones to enable programmers to develop on a solid foundation that others have done instead of starting over from scratch. In order to do so, </span><i style="background-color: white; color: #222222; line-height: 25px; text-align: -webkit-auto;">function</i><span class="Apple-style-span" style="background-color: white; color: #222222; line-height: 25px; text-align: -webkit-auto;"> is invented in programming world. Proper functions hide its operation details from the external programs using them and expose only the interface and its usage.</span></span></div>
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">Function is a block of statements which perform some specific task and always return single value to the calling function. Functions are used to minimize the repetition of code.</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">Some languages distinguish between functions which return variables and those which don't. C assumes that every function will return a value. If the programmer wants a return value, this is achieved using the return statement. If no return value is required, none should be used when calling the function.</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">There are two types of functions in c language.</span><br />
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; font-weight: bold; line-height: 16px;">1. Library Functions</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">A function which is predefined in c language is called library function printf(), scanf(), getch() etc are library functions</span><br />
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; font-weight: bold; line-height: 16px;">2. User Defined Functions</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">A function written by a programmer is called user defined function.</span><br />
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; font-weight: bold; line-height: 16px;">Example</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">#include</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">int add (int x, int y) {</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">int z;</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">z = x + y;</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">return (z);</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">}</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">main ()</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">{</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">int i, j, k;</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">i = 15;</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">j = 5;</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">k = add(i, j);</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">printf ("The value of k is %d\n", k);</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">}</span><br />
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; font-weight: bold; line-height: 16px;">Output</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">The value of k is 30</span><br />
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; font-weight: bold; line-height: 16px;">Scope of Function:</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">Only a limited amount of information is available within the body of each function. Variables declared within the calling function can't be accessed from the outside functions unless they are passed to the called function as arguments.</span><br />
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; font-weight: bold; line-height: 16px;">Global Variables:</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">A variable that is declared out side all functions is called Global variable. Global variables don't die on return from a function. Their value is retained, and is available to any other function in whole program.</span><br />
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; font-weight: bold; line-height: 16px;">Local Variables:</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">A variable that is declared within a function is called Local variable. They are created each time the function is called, and destroyed on return from the function. The values passed to the functions (arguments) are also treated like local variables.</span><br />
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; font-weight: bold; line-height: 16px;">Static Variables:</span><br />
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">Static variables are like local variables but they don't die on return from the function. Instead their last value is retained, and it becomes available when the function is called again.</span></div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-67578034917089398842012-01-09T21:46:00.000+05:302012-01-09T21:46:11.445+05:30Storage Classes in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">There are following storage classes which can be used in a C Program</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">1)auto</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">2)register</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">3)static</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">4)extern</span></div>
<div>
<b><span class="Apple-style-span" style="color: blue; font-family: Verdana, sans-serif;">auto - Storage Class</span></b></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">auto is the default storage class for all local variables.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int Count;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> auto int Month;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b>storage location</b>- RAM<br /><b>Default initial value</b>- Garbage<br /><b>Scope</b>-local to block<br /><b>lifetime</b>-control within the block</span></div>
<div>
<b><span class="Apple-style-span" style="color: blue; font-family: Verdana, sans-serif;">register - Storage Class</span></b></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).</span><div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b>storage location</b>- CPU register<br /><b>Default initial value</b>- Garbage<br /><b>Scope</b>-local to block<br /><b>lifetime</b>-control within the block</span></div>
</div>
<div>
<span class="Apple-tab-span" style="font-family: Verdana, sans-serif; white-space: pre;"> </span><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">{</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> register int Miles;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implimentation restrictions.</span></div>
<div>
<b><span class="Apple-style-span" style="color: blue; font-family: Verdana, sans-serif;">static - Storage Class</span></b></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.</span></div>
<div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b>storage location</b>- RAM<br /><b>Default initial value</b>- 0<br /><b>Scope</b>-local to block<br /><b>lifetime</b>-end of the block</span></div>
</div>
<div>
<span class="Apple-tab-span" style="font-family: Verdana, sans-serif; white-space: pre;"> </span><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">static int Count;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int Road;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("%d\n", Road);</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">static can also be defined within a function. If this is done the variable is initalised at run time but is not reinitialized when the function is called. This inside a function static variable retains its value during vairous calls.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> void func(void);</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> static count=10; /* Global variable - static is the default */</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> main()</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> while (count--) </span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> func();</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> void func( void )</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> static i = 5;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i++;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("i is %d and count is %d\n", i, count);</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> This will produce following result</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 6 and count is 9</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 7 and count is 8</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 8 and count is 7</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 9 and count is 6</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 10 and count is 5</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 11 and count is 4</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 12 and count is 3</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 13 and count is 2</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 14 and count is 1</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> i is 15 and count is 0</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b>NOTE </b>: Here keyword void means function does not return anything and it does not take any parameter. You can memorize void as nothing. static variables are initialized to 0 automatically.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b>Definition vs Declaration : </b>Before proceeding, let us understand the difference between defintion and declaration of a variable or function. Definition means where a variable or function is defined in realityand actual memory is allocated for variable or function. Declaration means just giving a reference of a variable and function. Through declaration we assure to the complier that this variable or function has been defined somewhere else in the program and will be provided at the time of linking. In the above examples char *func(void) has been put at the top which is a declaration of this function where as this function has been defined below to main() function.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">There is one more very important use for 'static'. Consider this bit of code.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char *func(void);</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> main()</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char *Text1;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> Text1 = func();</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char *func(void)</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char Text2[10]="martin";</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> return(Text2);</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Now, 'func' returns a pointer to the memory location where 'text2' starts BUT text2 has a storage class of 'auto' and will disappear when we exit the function and could be overwritten but something else. The answer is to specify</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> static char Text[10]="martin";</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The storage assigned to 'text2' will remain reserved for the duration if the program.</span></div>
<div>
<b><span class="Apple-style-span" style="color: blue; font-family: Verdana, sans-serif;">extern - Storage Class</span></b></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.</span></div>
<div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b>storage location</b>- RAM<br /><b>Default initial value</b>- 0<br /><b>Scope</b>-global<br /><b>lifetime</b>-end of the block</span></div>
</div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.</span></div>
<div>
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">File 1: main.c</span></b></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> int count=5;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> main()</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> write_extern();</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span></div>
<div>
<b><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">File 2: write.c</span></b></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> void write_extern(void);</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> extern int count;</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> void write_extern(void)</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> {</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> printf("count is %i\n", count);</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> }</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Here extern keyword is being used to declare count in another file.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Now compile these two files as follows</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> gcc main.c write.c -o write</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This fill produce write program which can be executed to produce result.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value.</span></div>
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-31324652187317973202012-01-09T21:22:00.001+05:302012-01-09T21:26:17.008+05:30Strings in C<div dir="ltr" style="text-align: left;" trbidi="on">
<span class="Apple-style-span" style="background-color: white; color: #333333; line-height: 16px;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Character type array is called string.All strings end with the NULL character.Use the %s placeholder in the printf() function to display string values.In other words to create a string in C you create an array of chars and set each element in the array to a char value that makes up the string. When sizing the string array you need to add plus one to the actual size of the string to make space for the null terminating character, "\0"</span></span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="background-color: white; color: #333333; font-weight: bold; line-height: 16px;">Declaration:</span><br style="background-color: white; color: #333333; line-height: 16px;" /><span class="Apple-style-span" style="background-color: white; color: #333333; line-height: 16px;">char name[50];</span></span><br />
<span class="Apple-style-span" style="background-color: white;"><span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"></span></span><br />
<div style="line-height: 16px;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">The above statement declares a string called name that can take up to 50 characters. It can be indexed just as a regular array as well.</span></div>
<div style="line-height: 16px;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">name[] = {'t','w','o'};</span></div>
<div style="line-height: 16px;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">Character<span class="Apple-tab-span" style="white-space: pre;"> </span> t<span class="Apple-tab-span" style="white-space: pre;"> </span> w<span class="Apple-tab-span" style="white-space: pre;"> </span>o<span class="Apple-tab-span" style="white-space: pre;"> </span>\0</span></div>
<div style="line-height: 16px;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">ASCII Code<span class="Apple-tab-span" style="white-space: pre;"> </span>116<span class="Apple-tab-span" style="white-space: pre;"> </span>119<span class="Apple-tab-span" style="white-space: pre;"> </span>41<span class="Apple-tab-span" style="white-space: pre;"> </span>0</span></div>
<div style="line-height: 16px;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">The last character is the null character having ASCII value zero.</span></div>
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif; line-height: 16px;">n fact, C’s only truly built-in string-handling is that it allows us to use string constants (also called string literals) in our code. Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character. For example, we can declare and define an array of characters, and initialize it with a string constant:</span><br />
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="line-height: 16px;"> char string[] = "Hello, world!";</span></span><br />
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="line-height: 16px;">In this case, we can leave out the dimension of the array, since the compiler can compute it for us based on the size of the initializer (14, including the terminating \0). This is the only case where the compiler sizes a string array for us, however; in other cases, it will be necessary that we decide how big the arrays and other data structures we use to hold strings are.</span></span><br />
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="line-height: 16px;">To do anything else with strings, we must typically call functions. The C library contains a few basic string manipulation functions, and to learn more about strings, we’ll be looking at how these functions might be implemented.</span></span><br />
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="line-height: 16px;">Since C never lets us assign entire arrays,we use the strcpy function to copy one string to another.</span></span><br />
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="line-height: 16px;">Before starting programming with a String functions we should the include the following header file.</span></span><br />
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="line-height: 16px;">#include<string.h></span></span><br />
<ul>
<li style="color: black;"><div style="text-align: justify;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">In C language Strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL '\0' character:</span></div>
</li>
<li style="color: black;"><div style="text-align: justify;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">It is important to preserve the NULL terminating character as it is how C defines and manages variable length strings. All the C standard library functions require this for successful operation.</span></div>
</li>
<li style="color: black;"><div style="text-align: justify;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">All the string handling functions are prototyped in: string.h or stdio.h standard header file. So while using any string related function, don't forget to include either stdio.h or string.h. May be your compiler differes so please check before going ahead.</span></div>
</li>
<li style="color: black;"><div style="text-align: justify;">
<span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;">If you were to have an array of characters WITHOUT the null character as the last element, you'd have an ordinary character array, rather than a string constant.</span></div>
</li>
<li><span class="Apple-style-span" style="color: #333333;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">String constants have double quote marks around them, and can be assigned to char pointers as shown below. Alternatively, you can assign a string constant to a char array - either with no size specified, or you can specify a size, but don't forget to leave a space for the null character!</span></span></li>
</ul>
<pre style="background-color: #f1f1f1;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">char *string1 = "Hello";
char string2[] = "Hello";
char string3[6] = "Hello";</span></pre>
<div style="text-align: justify;">
<b style="color: #333333; font-family: Verdana, sans-serif;">Functions in C</b></div>
<div style="text-align: justify;">
</div>
<ul>
<li><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b style="color: #333333;">Strlen()</b><pre style="background-color: white; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="color: green;">size_t strlen ( const char * str );
</span></span><span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif; white-space: normal;">This function is used to find the length of the string.</span></pre>
</span></li>
<li><span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><b>strrev()</b>This function is used to reverse the given string.</span></li>
<li><span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><b>strcpy()<br /><span class="Apple-style-span" style="background-color: white; color: green; font-weight: normal; white-space: pre;">char * strcpy ( char * destination, const char * source );</span></b>
This function is used to copy the string from one array variable to another array variable.</span></li>
<li><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b><span class="Apple-style-span" style="color: #333333;">strcat()</span><br /><pre style="background-color: white; font-weight: normal; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="color: green;">char * strcat ( char * destination, const char * source );
</span></span><span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif; white-space: normal;">This function is used to concatenation of two strings. </span></pre>
</b></span></li>
<li><span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><b>strncpy()<br /><span class="Apple-style-span" style="background-color: white; color: green; font-weight: normal; white-space: pre;">char * strncpy ( char * destination, const char * source, size_t num );</span></b>This function of used to copy the string from source to destination but only a fixed n bytes is allowed to copy.</span></li>
<li><span class="Apple-style-span" style="color: #333333; font-family: Verdana, sans-serif;"><b>strncat()<br /><pre style="background-color: white; color: green; font-weight: normal; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">char * strncat ( char * destination, char * source, size_t num );</span></pre>
<span class="Apple-style-span" style="font-weight: normal;">This function of used to con-cat the string from source to destination but only a fixed n bytes is allowed to con-cat.</span></b></span></li>
<li><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="color: #333333; font-weight: bold;">strcmp()</span><span class="Apple-style-span" style="color: #333333;"><b><br /></b></span><div style="color: #333333; font-weight: bold; text-align: left;">
<b><b><b><pre style="background-color: white; color: green; display: inline !important; font-weight: normal; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">int strcmp ( const char * str1, const char * str2 );</span></pre>
</b></b></b></div>
<span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal;">Compares the C string </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">str1</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;"> to the C string </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">str2</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">.</span><br style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;" /><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">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.</span></span></li>
<li><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="color: #333333; font-weight: bold;">strcmpi() or stricmp()</span><b><br /></b><div style="color: #333333; font-weight: bold; text-align: left;">
<b style="color: black; font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="color: #333333;"><b style="color: black; font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><pre style="background-color: white; display: inline !important; font-weight: normal; text-align: -webkit-auto;"><span class="Apple-style-span" style="color: #38761d; font-family: Verdana, sans-serif;">int strcmpi( const char *s1, const char *s2 );</span></pre>
</span></b></span></span></b></div>
<span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal;">The </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">strcmpi()</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;"> function compares, with case insensitivity, the string pointed to by </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">s1</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;"> to the string pointed to by </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">s2</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">. All uppercase characters from </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">s1</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;"> and </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">s2</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;"> are mapped to lowercase for the purposes of doing the comparison. The </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">strcmpi()</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;"> function is identical to the </span><i style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;">stricmp()</i><span class="Apple-style-span" style="background-color: white; color: black; font-weight: normal; text-align: -webkit-auto;"> function.</span></span></li>
<li><span class="Apple-style-span" style="background-color: white; text-align: -webkit-auto;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="color: black; font-size: small;"><b>strrchr()</b></span><span class="Apple-style-span" style="color: green; white-space: pre;">const char * strrchr ( const char * str, int character );</span><div class="C_docsubtop" style="color: black; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div class="prototype" style="color: green; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<pre style="padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> char * strrchr ( char * str, int character );</span></pre>
</div>
</div>
<div style="color: black; font-weight: normal;">
<strong>Locate last occurrence of character in string</strong></div>
<span class="Apple-style-span" style="color: black; font-weight: normal;">Returns a pointer to the last occurrence of </span><i style="color: black; font-weight: normal;">character</i><span class="Apple-style-span" style="color: black; font-weight: normal;"> in the C string </span><i style="color: black; font-weight: normal;">str</i><span class="Apple-style-span" style="color: black; font-weight: normal;">.</span><br style="font-weight: normal;" /><span class="Apple-style-span" style="color: black; font-weight: normal;">The terminating null-character is considered part of the C string. Therefore, it can also be located to retrieve a pointer to the end of a string.</span></span></span></li>
<li><span class="Apple-style-span" style="background-color: white; text-align: -webkit-auto;"><div class="C_docsubtop" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div class="prototype" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<pre style="padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b>memcmp()
</b><span class="Apple-style-span" style="color: green;">int memcmp ( const void * ptr1, const void * ptr2, size_t num );</span></span></pre>
</div>
</div>
<strong><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Compare two blocks of memory</span></strong><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Compares the first <i>num</i> bytes of the block of memory pointed by <i>ptr1</i> to the first <i>num</i> bytes pointed by <i>ptr2</i>, returning zero if they all match or a value different from zero representing which is greater if they do not.</span></span></li>
</ul>
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0tag:blogger.com,1999:blog-1427512104410453673.post-14920504849853971372012-01-08T19:55:00.003+05:302012-01-09T21:46:49.714+05:30Constants<div dir="ltr" style="text-align: left;" trbidi="on">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><b style="background-color: white; line-height: 23px;"><span style="color: #222222; font-size: 10.5pt;">Summary</span></b><i style="background-color: white; line-height: 23px;"><span style="color: #222222; font-size: 10.5pt;">: in this tutorial, you
will learn how use<b> C constants</b> in your code to make your
code more readable and easy to maintain.</span></i></span><br />
<div class="MsoNormal" style="background: white; line-height: 17.8pt; mso-outline-level: 2;">
<b><span style="color: #666666; font-family: Verdana, sans-serif; font-size: 10.5pt;">Introduction to C constants<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background: white; line-height: 17.8pt; mso-outline-level: 3;">
<b><span style="color: #666666; font-family: Verdana, sans-serif; font-size: 9.5pt;">C constants using C preprocessor<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background: white; line-height: 17.8pt;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="color: #222222; font-size: 10.5pt;">There
are a lot of cases you want to use a constant in</span><span style="color: #222222; font-size: 10.5pt;"> </span><span style="color: #222222; font-size: 10.5pt;">your
code</span><span style="color: #222222; font-size: 10.5pt;"> </span><span style="color: #222222; font-size: 10.5pt;">for example to calculate
the square of a circle you code as follows:<o:p></o:p></span></span></div>
<table border="0" cellpadding="0" class="MsoNormalTable">
<tbody>
<tr>
<td style="padding: .75pt .75pt .75pt .75pt;"></td>
<td style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">square = 3.14159 * diameter;</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal" style="background: white; line-height: 17.8pt;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="color: #222222; font-size: 10.5pt;">In the</span><span style="color: #222222; font-size: 10.5pt;"> statement</span><span style="color: #222222; font-size: 10.5pt;">, the constant 3.14159 represents the constant
PI. Instead of putting constant in literal form, we can use symbolic constant
as below:<o:p></o:p></span></span></div>
<table border="0" cellpadding="0" class="MsoNormalTable">
<tbody>
<tr>
<td style="padding: .75pt .75pt .75pt .75pt;"></td>
<td style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">square = PI * diameter;</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal" style="background: white; line-height: 17.8pt;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="color: #222222; font-size: 10.5pt;">As you
see, the C constants make</span><span style="color: #222222; font-size: 10.5pt;"> </span><span style="color: #222222; font-size: 10.5pt;">your
code</span><span style="color: #222222; font-size: 10.5pt;"> </span><span style="color: #222222; font-size: 10.5pt;">readable and the symbolic
constants tell you not only the value but also its meaning. In addition, if a
constant is used in several places, it is better to use symbolic constant because
when you want to change its value you just have to change it in one place that
define the constant, see the below example:<o:p></o:p></span></span></div>
<table border="0" cellpadding="0" class="MsoNormalTable">
<tbody>
<tr>
<td style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">1</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
</td>
<td style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">total = net + TAXRATE * net;</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal" style="background: white; line-height: 17.8pt;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="color: #222222; font-size: 10.5pt;">Later
on if the</span><span style="color: #222222; font-size: 10.5pt;"> tax rate </span><span style="color: #222222; font-size: 10.5pt;">changes,
you only have to change it in where it is defined.But how do you define
symbolic C constants? The first idea of defining a C constant is using C
preprocessor as follows:<o:p></o:p></span></span></div>
<table border="0" cellpadding="0" class="MsoNormalTable">
<tbody>
<tr>
<td style="padding: .75pt .75pt .75pt .75pt;"></td>
<td style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">#define PI 3.14159</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
</td>
</tr>
<tr>
<td style="padding: .75pt .75pt .75pt .75pt;"></td>
<td colspan="2" style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">#define TAXRATE 0.1</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal" style="background: white; line-height: 17.8pt;">
<span style="color: #222222; font-family: Verdana, sans-serif; font-size: 10.5pt;">By
doing this, when your program compiled, the constant values 1.14159 and 0.1
will be substituted everywhere you used PI and TAXRATE. This feature is known
as compile time substitution. Those defined constants are called manifest
constants because when you run the program, all the substitutions have already
been made.<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: 17.8pt;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="color: #222222; font-size: 10.5pt;">Note
that there is no equal sign between constant’s name and value and there is no
semicolon (;) at the end of the</span><span style="color: #222222; font-size: 10.5pt;"> </span><span style="color: #222222; font-size: 10.5pt;">statement.<o:p></o:p></span></span></div>
<div class="MsoNormal" style="background: white; line-height: 17.8pt;">
<span style="color: #222222; font-family: Verdana, sans-serif; font-size: 10.5pt;">It is
highly recommended that you use constant name is upper case so when you see it
in code you know that it is a constant. The formula of defining a constant is
as follows:<o:p></o:p></span></div>
<table border="0" cellpadding="0" class="MsoNormalTable">
<tbody>
<tr>
<td style="padding: .75pt .75pt .75pt .75pt;"></td>
<td style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">#define CONSTANT_NAME value</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal" style="background: white; line-height: 17.8pt; mso-outline-level: 3;">
<b><span style="color: #666666; font-family: Verdana, sans-serif; font-size: 9.5pt;">C constants using const keyword<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background: white; line-height: 17.8pt;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="color: #222222; font-size: 10.5pt;">C90
officially added a second way to create symbolic constants using the</span><span style="color: #222222; font-size: 10.5pt;"> <i>const</i></span><span style="color: #222222; font-size: 10.5pt;">keyword to declare a
constant. This approach is more flexible that using C preprocessor #define such
as you can use keyword const to declare an array, pointer…etc.Once the value of
a constant using const keyword has been initialized, its value cannot be changed
through the program. See the below example:<o:p></o:p></span></span></div>
<table border="0" cellpadding="0" class="MsoNormalTable">
<tbody>
<tr>
<td style="padding: .75pt .75pt .75pt .75pt;"></td>
<td style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">const</span><span style="font-size: 12pt;"> </span><span style="font-size: 10pt;">double</span><span style="font-size: 12pt;"> </span><span style="font-size: 10pt;">PI
= 3.14159;</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
</td>
</tr>
<tr>
<td style="padding: .75pt .75pt .75pt .75pt;"></td>
<td colspan="2" style="padding: .75pt .75pt .75pt .75pt;"><div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span style="font-size: 10pt;">const</span><span style="font-size: 12pt;"> </span><span style="font-size: 10pt;">double</span><span style="font-size: 12pt;"> </span><span style="font-size: 10pt;">TAXRATE
= 0.1;</span></span></div>
</td></tr>
</tbody></table>
</div>suryahttp://www.blogger.com/profile/12009119049723357578noreply@blogger.com0