Useful C Library Function
Useful C Library Function
Useful C Library Function
Description
The C library function double pow(double x, double y) returns x raised to the power of yi.e. xy.
Declaration
Following is the declaration for pow() function.
double pow(double x, double y)
Parameters
Return Value
This function returns the result of raising x to the power y.
Example
The following example shows the usage of pow() function.
#include <stdio.h>
#include <math.h>
int main ()
{
printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));
return(0);
}
Let us compile and run the above program that will produce the following result
Declaration
Following is the declaration for sqrt() function.
double sqrt(double x)
Parameters
Return Value
This function returns the square root of x.
Example
The following example shows the usage of sqrt() function.
#include <stdio.h>
#include <math.h>
int main ()
{
return(0);
}
Let us compile and run the above program that will produce the following result
Square root of 4.000000 is 2.000000
Square root of 5.000000 is 2.236068
Declaration
Following is the declaration for acos() function.
double acos(double x)
Parameters
Return Value
This function returns principal arc cosine of x, in the interval [0, pi] radians.
Example
The following example shows the usage of acos() function.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 0.9;
val = 180.0 / PI;
return(0);
}
Let us compile and run the above program that will produce the following result
The arc cosine of 0.900000 is 25.855040 degrees
Declaration
Following is the declaration for cos() function.
double cos(double x)
Parameters
Return Value
This function returns the cosine of x.
Example
The following example shows the usage of cos() function.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 60.0;
val = PI / 180.0;
ret = cos( x*val );
printf("The cosine of %lf is %lf degrees\n", x, ret);
x = 90.0;
val = PI / 180.0;
ret = cos( x*val );
return(0);
}
Let us compile and run the above program that will produce the following result
The cosine of 60.000000 is 0.500000 degrees
The cosine of 90.000000 is 0.000000 degrees
Declaration
Following is the declaration for exp() function.
double exp(double x)
Parameters
Return Value
This function returns the exponential value of x.
Example
The following example shows the usage of exp() function.
#include <stdio.h>
#include <math.h>
int main ()
{
double x = 0;
return(0);
}
Let us compile and run the above program that will produce the following result
The exponential value of 0.000000 is 1.000000
The exponential value of 1.000000 is 2.718282
The exponential value of 2.000000 is 7.389056
Previous Page
Next Page
Description
The C library function double ceil(double x) returns the smallest integer value greater than or
equal to x.
Declaration
Parameters
Return Value
This function returns the smallest integral value not less than x.
Example
The following example shows the usage of ceil() function.
#include <stdio.h>
#include <math.h>
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = 2.8;
val4 = 2.3;
return(0);
Let us compile and run the above program that will produce the following result
value1 = 2.0
value2 = 2.0
value3 = 3.0
value4 = 3.0
Previous Page
Next Page
Description
The C library function double floor(double x) returns the largest integer value less than or equal
to x.
Declaration
Following is the declaration for floor() function.
double floor(double x)
Parameters
Return Value
This function returns the largest integral value not greater than x.
Example
The following example shows the usage of floor() function.
#include <stdio.h>
#include <math.h>
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = 2.8;
val4 = 2.3;
return(0);
}
Let us compile and run the above program that will produce the following result
Value1 = 1.0
Value2 = 1.0
Value3 = 2.0
Value4 = 2.0
Previous Page
Next Page
Description
The C library function double fmod(double x, double y) returns the remainder of x divided by y.
Declaration
Following is the declaration for fmod() function.
double fmod(double x, double y)
Parameters
x This is the floating point value with the division numerator i.e. x.
y This is the floating point value with the division denominator i.e. y.
Return Value
This function returns the remainder of dividing x/y.
Example
The following example shows the usage of fmod() function.
#include <stdio.h>
#include <math.h>
int main ()
{
float a, b;
int c;
a = 9.2;
b = 3.7;
c = 2;
printf("Remainder of %f / %d is %lf\n", a, c, fmod(a,c));
printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b));
return(0);
}
Let us compile and run the above program that will produce the following result
Remainder of 9.200000 / 2 is 1.200000
Remainder of 9.200000 / 3.700000 is 1.800000
Previous Page
Next Page
Description
The C library function char *strcat(char *dest, const char *src) appends the string pointed to
by src to the end of the string pointed to by dest.
Declaration
Parameters
dest -- This is pointer to the destination array, which should contain a C string, and should
be large enough to contain the concatenated resulting string.
src -- This is the string to be appended. This should not overlap the destination.
Return Value
This function returns a pointer to the resulting string dest.
Example
The following example shows the usage of strcat() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char src[50], dest[50];
strcat(dest, src);
return(0);
}
Let us compile and run the above program that will produce the following result:
Final destination string : |This is destinationThis is source|
Previous Page
Next Page
Description
The C library function char *strncat(char *dest, const char *src, size_t n) appends the string
pointed to by src to the end of the string pointed to by dest up to n characters long.
Declaration
Following is the declaration for strncat() function.
char *strncat(char *dest, const char *src, size_t n)
Parameters
dest -- This is pointer to the destination array, which should contain a C string, and should
be large enough to contain the concatenated resulting string which includes the additional
null-character.
Return Value
Example
The following example shows the usage of strncat() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char src[50], dest[50];
return(0);
}
Let us compile and run the above program that will produce the following result:
Final destination string : |This is destinationThis is source|
Previous Page
Next Page
Description
The C library function char *strchr(const char *str, int c) searches for the first occurrence of the
character c (an unsigned char) in the string pointed to by the argument str.
Declaration
Following is the declaration for strchr() function.
char *strchr(const char *str, int c)
Parameters
Return Value
This returns a pointer to the first occurrence of the character c in the string str, or NULL if the
character is not found.
Example
The following example shows the usage of strchr() function.
#include <stdio.h>
#include <string.h>
int main ()
{
const char str[] = "http://www.tutorialspoint.com";
const char ch = '.';
char *ret;
return(0);
}
Let us compile and run the above program that will produce the following result:
String after |.| is - |.tutorialspoint.com|
Previous Page
Next Page
Description
The C library function int strcmp(const char *str1, const char *str2) compares the string pointed
to, by str1 to the string pointed to by str2.
Declaration
Following is the declaration for strcmp() function.
int strcmp(const char *str1, const char *str2)
Parameters
Return Value
This function return values that are as follows:
Example
The following example shows the usage of strncmp() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
if(ret < 0)
{
printf("str1 is less than str2");
}
return(0);
}
Let us compile and run the above program that will produce the following result:
str2 is less than str1
Previous Page
Next Page
Description
The C library function int strncmp(const char *str1, const char *str2, size_t n) compares at most
the first n bytes of str1 and str2.
Declaration
Following is the declaration for strncmp() function.
Parameters
Return Value
This function return values that are as follows:
Example
The following example shows the usage of strncmp() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
if(ret < 0)
{
printf("str1 is less than str2");
}
else if(ret > 0)
{
printf("str2 is less than str1");
}
else
{
printf("str1 is equal to str2");
}
return(0);
}
Let us compile and run the above program that will produce the following result:
str2 is less than str1
Previous Page
Next Page
Description
The C library function char *strcpy(char *dest, const char *src) copies the string pointed to,
by src to dest.
Declaration
Following is the declaration for strcpy() function.
char *strcpy(char *dest, const char *src)
Parameters
dest -- This is the pointer to the destination array where the content is to be copied.
Return Value
This returns a pointer to the destination string dest.
Example
The following example shows the usage of strcpy() function.
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[100];
return(0);
}
Let us compile and run the above program that will produce the following result:
Final copied string : This is tutorialspoint.com
Previous Page
Next Page
Description
The C library function char *strncpy(char *dest, const char *src, size_t n) copies up
to ncharacters from the string pointed to, by src to dest. In a case where the length of src is less
than that of n, the remainder of dest will be padded with null bytes.
Declaration
Following is the declaration for strncpy() function.
char *strncpy(char *dest, const char *src, size_t n)
Parameters
dest -- This is the pointer to the destination array where the content is to be copied.
Return Value
This function returns the final copy of the copied string.
Example
The following example shows the usage of strncpy() function. Here we have used function
memset() to clear the memory location.
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[12];
return(0);
}
Let us compile and run the above program that will produce the following result:
Final copied string : This is tu
Previous Page
Next Page
Description
The C library function size_t strlen(const char *str) computes the length of the string strup to, but
not including the terminating null character.
Declaration
Following is the declaration for strlen() function.
size_t strlen(const char *str)
Parameters
Return Value
This function returns the length of string.
Example
The following example shows the usage of strlen() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char str[50];
int len;
len = strlen(str);
printf("Length of |%s| is |%d|\n", str, len);
return(0);
}
Let us compile and run the above program that will produce the following result:
Length of |This is tutorialspoint.com| is |26|
Previous Page
Next Page
Description
The C library function char *strstr(const char *haystack, const char *needle) function finds the
first occurrence of the substring needle in the string haystack. The terminating '\0' characters are
not compared.
Declaration
Parameters
Return Value
This function returns a pointer to the first occurrence in haystack of any of the entire sequence of
characters specified in needle, or a null pointer if the sequence is not present in haystack.
Example
The following example shows the usage of strstr() function.
#include <stdio.h>
#include <string.h>
int main()
{
const char haystack[20] = "TutorialsPoint";
const char needle[10] = "Point";
char *ret;
return(0);
}
Let us compile and run the above program that will produce the following result:
The substring is: Point
Previous Page
Next Page
Description
The C library function char *strpbrk(const char *str1, const char *str2) finds the first character in
the string str1 that matches any character specified in str2. This does not include the terminating
null-characters.
Declaration
Following is the declaration for strpbrk() function.
char *strpbrk(const char *str1, const char *str2)
Parameters
Return Value
This function returns a pointer to the character in str1 that matches one of the characters in str2, or
NULL if no such character is found.
Example
The following example shows the usage of strpbrk() function.
#include <stdio.h>
#include <string.h>
int main ()
{
const char str1[] = "abcde2fghi3jk4l";
const char str2[] = "34";
char *ret;
return(0);
}
Let us compile and run the above program that will produce the following result:
First matching character: 3
Previous Page
Next Page
Description
The C library function char *strtok(char *str, const char *delim) breaks string str into a series of
tokens using the delimiter delim.
Declaration
Following is the declaration for strtok() function.
char *strtok(char *str, const char *delim)
Parameters
str -- The contents of this string are modified and broken into smaller strings (tokens).
delim -- This is the C string containing the delimiters. These may vary from one call to
another.
Return Value
This function returns a pointer to the last token found in the string. A null pointer is returned if there
are no tokens left to retrieve.
Example
The following example shows the usage of strtok() function.
#include <string.h>
#include <stdio.h>
int main()
{
char str[80] = "This is - www.tutorialspoint.com - website";
const char s[2] = "-";
char *token;
return(0);
}
Let us compile and run the above program that will produce the following result:
This is
www.tutorialspoint.com
website