Useful C Library Function

  • Upload
    ravi

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

  • 7/23/2019 Useful C Library Function

    1/31

    C library function - pow()

    DescriptionThe C library function double pow(double x, double y)returns xraised to the power of yi.e. xy.

    DeclarationFollowing is the declaration for pow() function.

    doublepow(doublex,doubley)

    Parameters x This is the floating point base alue.

    y This is the floating point power alue.

    !eturn "alueThis function returns the result of raising xto the power y.

    #xample

    The following example shows the usage of pow() function.

    #include

    #include

    intmain ()

    {

    printf("Value .! $ %lf&n",pow(.!,))'

    printf("Value .! .* $ %lf",pow(.!,.*))'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result

  • 7/23/2019 Useful C Library Function

    2/31

    Value .! $ .!!!!!!

    Value .! .* $ *.!*-

    C library function - sqrt()

    Description

    The C library function double sqrt(double x)returns the s%uare root of x.

    DeclarationFollowing is the declaration for s%rt() function.

    doubles/rt(doublex)

    Parameters

    x This is the floating point alue.

    !eturn "alue

    This function returns the s%uare root of x.

    #xample

    The following example shows the usage of s%rt() function.

    #include

    #include

    intmain ()

    {

  • 7/23/2019 Useful C Library Function

    3/31

    printf("0/uare root of %lf is %lf&n",.!,s/rt(.!))'

    printf("0/uare root of %lf is %lf&n",.!,s/rt(.!))'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result

    0/uare root of .!!!!!! is .!!!!!!

    0/uare root of .!!!!!! is .1!1

    C library function - acos()

    Description

    The C library function double acos(double x)returns the arc cosine of xin radians.

    Declaration

    Following is the declaration for acos() function.

    doubleacos(doublex)

    Parameters

    x This is the floating point alue in the interal &'*+.

    !eturn "alue

    This function returns principal arc cosine of x in the interal &, pi+ radians.

  • 7/23/2019 Useful C Library Function

    4/31

    #xample

    The following example shows the usage of acos() function.

    #include

    #include

    #de2ne34 .*1

    intmain ()

    {

    doublex,ret,5al'

    x $!.*'

    5al $!.!634'

    ret $acos(x)75al'

    printf("8he arc cosine of %lf is %lf de9rees",x,ret)'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result

    8he arc cosine of !.*!!!!! is .!! de9rees

    C library function - cos()

    Description

    The C library function double cos(double x)returns the cosine of a radian angle x.

  • 7/23/2019 Useful C Library Function

    5/31

    Declaration

    Following is the declaration for cos() function.

    doublecos(doublex)

    Parameters

    x This is the floating point alue representing an angle expressed in radians.

    !eturn "alue

    This function returns the cosine of x.

    #xample

    The following example shows the usage of cos() function.

    #include

    #include

    #de2ne34 .*1

    intmain ()

    {

    doublex,ret,5al'

    x $1!.!'

    5al $34 6!.!'

    ret $cos(x75al )'

    printf("8he cosine of %lf is %lf de9rees&n",x,ret)'

    x $*!.!'

    5al $34 6!.!'

    ret $cos(x75al )'

  • 7/23/2019 Useful C Library Function

    6/31

    printf("8he cosine of %lf is %lf de9rees&n",x,ret)'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result

    8he cosine of 1!.!!!!!! is !.!!!!! de9rees

    8he cosine of *!.!!!!!! is !.!!!!!! de9rees

    C library function - exp()

    Description

    The C library function double exp(double x)returns the alue of eraised to the xthpower.

    Declaration

    Following is the declaration for exp() function.

    doubleexp(doublex)

    Parameters

    x This is the floating point alue.

    !eturn "alueThis function returns the exponential alue of x.

    #xample

    The following example shows the usage of exp() function.

  • 7/23/2019 Useful C Library Function

    7/31

    #include

    #include

    intmain ()

    {

    doublex $!'

    printf("8he exponential 5alue of %lf is %lf&n",x,exp(x))'

    printf("8he exponential 5alue of %lf is %lf&n",x:,exp(x:))'

    printf("8he exponential 5alue of %lf is %lf&n",x:,exp(x:))'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result

    8he exponential 5alue of !.!!!!!! is .!!!!!!

    8he exponential 5alue of .!!!!!! is .-

    8he exponential 5alue of .!!!!!! is -.*!1

    C library function - ceil()-dertisements

    Preious Page

    ext Page

    Description

    The C library function double ceil(double x) returns the smallest integer alue greater than or

    e%ual to x.

    Declaration

    http://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htm
  • 7/23/2019 Useful C Library Function

    8/31

    Following is the declaration for ceil() function.

    doubleceil(doublex)

    Parameters

    x This is the floating point alue.

    !eturn "alue

    This function returns the smallest integral alue not less than x.

    #xample

    The following example shows the usage of ceil() function.

    #include

    #include

    intmain ()

    {

    ;oat5al,5al,5al,5al'

    5al $.1'

    5al $.'

    5al $.'

    5al $.'

    printf ("5alue $ %.lf&n",ceil(5al))'

    printf ("5alue $ %.lf&n",ceil(5al))'

    printf ("5alue $ %.lf&n",ceil(5al))'

    printf ("5alue $ %.lf&n",ceil(5al))'

    return(!)'

  • 7/23/2019 Useful C Library Function

    9/31

    +

    $et us compile and run the aboe program that will produce the following result

    5alue $ .!

    5alue $ .!

    5alue $ .!

    5alue $ .!

    C library function - floor()

    -dertisements

    Preious Page

    ext Page

    Description

    The C library function double floor(double x)returns the largest integer alue less than or e%ualto x.

    Declaration

    Following is the declaration for floor() function.

    double;oor(doublex)

    Parameters x This is the floating point alue.

    !eturn "alue

    http://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htm
  • 7/23/2019 Useful C Library Function

    10/31

    This function returns the largest integral alue not greater than x.

    #xample

    The following example shows the usage of floor() function.

    #include

    #include

    intmain ()

    {

    ;oat5al,5al,5al,5al'

    5al $.1'

    5al $.'

    5al $.'

    5al $.'

    printf("Value $ %.lf&n",;oor(5al))'

    printf("Value $ %.lf&n",;oor(5al))'

    printf("Value $ %.lf&n",;oor(5al))'

    printf("Value $ %.lf&n",;oor(5al))'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result

    Value $ .!Value $ .!

    Value $ .!

    Value $ .!

  • 7/23/2019 Useful C Library Function

    11/31

    C library function - fmod()

    -dertisements

    Preious Page

    ext Page

    Description

    The C library function double fmod(double x, double y)returns the remainder of xdiided by y.

    Declaration

    Following is the declaration for fmod() function.

    doublefmod(doublex,doubley)

    Parameters

    x This is the floating point alue with the diision numerator i.e. x.

    y This is the floating point alue with the diision denominator i.e. y.

    !eturn "alue

    This function returns the remainder of diiding x/y.

    #xample

    The following example shows the usage of fmod() function.

    #include

    #include

    http://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htmhttp://www.tutorialspoint.com/c_standard_library/math_h.htm
  • 7/23/2019 Useful C Library Function

    12/31

    intmain ()

    {

    ;oata,b'

    intc'

    a $*.'

    b $.-'

    c $'

    printf("emainder of %f 6 %d is %lf&n",a,c,fmod(a,c))'

    printf("emainder of %f 6 %f is %lf&n",a,b,fmod(a,b))'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result

    emainder of *.!!!!! 6 is .!!!!!

    emainder of *.!!!!! 6 .-!!!!! is .!!!!!

    C library function - strcat()

    -dertisements

    Preious Page

    ext Page

    Description

    The C library function char *strcat(char *dest, const char *src)appends the string pointed to

    by srcto the end of the string pointed to by dest.

    Declaration

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    13/31

    Following is the declaration for strcat() function.

    char7strcat(char7dest,constchar7src)

    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 oerlap the destination.

    !eturn "alue

    This function returns a pointer to the resulting string dest.

    #xample

    The following example shows the usage of strcat() function.

    #include

    #include

    intmain ()

    {

    charsrc=!,dest=!'

    strcpy(src, "8his is source")'

    strcpy(dest,"8his is destination")'

    strcat(dest,src)'

    printf("?inal destination strin9 @ A%sA",dest)'

    return(!)'

    +

  • 7/23/2019 Useful C Library Function

    14/31

    $et us compile and run the aboe program that will produce the following result0

    ?inal destination strin9 @ A8his is destination8his is sourceA

    C library function - strncat()

    -dertisements

    Preious Page

    ext Page

    Description

    The C library function char *strncat(char *dest, const char *src, size_t n)appends the string

    pointed to by srcto the end of the string pointed to by destup to ncharacters long.

    Declaration

    Following is the declaration for strncat() function.

    char7strncat(char7dest,constchar7src,siBeCtn)

    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.

    src'' This is the string to be appended.

    n'' This is the maximum number of characters to be appended.

    !eturn "alue

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    15/31

    This function returns a pointer to the resulting string dest.

    #xample

    The following example shows the usage of strncat() function.

    #include

    #include

    intmain ()

    {

    charsrc=!,dest=!'

    strcpy(src, "8his is source")'

    strcpy(dest,"8his is destination")'

    strncat(dest,src,)'

    printf("?inal destination strin9 @ A%sA",dest)'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result0

    ?inal destination strin9 @ A8his is destination8his is sourceA

    C library function - strchr()-dertisements

    Preious Page

    ext Page

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    16/31

    Description

    The C library function char *strchr(const char *str, int c)searches for the first occurrence of thecharacter c(an unsigned char) in the string pointed to by the argument str.

    Declaration

    Following is the declaration for strchr() function.

    char7strchr(constchar7str,intc)

    Parameters str'' This is the C string to be scanned.

    c'' This is the character to be searched in str.

    !eturn "alue

    This returns a pointer to the first occurrence of the character c in the string str or 1$$ if the

    character is not found.

    #xample

    The following example shows the usage of strchr() function.

    #include

    #include

    intmain ()

    {

    constcharstr=$"[email protected]" '

    constcharch $D.D'

    char7ret'

  • 7/23/2019 Useful C Library Function

    17/31

    ret $strchr(str,ch)'

    printf("0trin9 after A%cA is E A%sA&n",ch,ret)'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result0

    0trin9 after A.A is E A.tutorialspoint.comA

    C library function - strcmp()

    -dertisements

    Preious Page

    ext Page

    Description

    The C library function int strcmp(const char *str1, const char *str)compares the string pointed

    to by str1to the string pointed to by str.

    Declaration

    Following is the declaration for strcmp() function.

    intstrcmp(constchar7str,constchar7str)

    Parameters

    str1'' This is the first string to be compared.

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    18/31

    str'' This is the second string to be compared.

    !eturn "alue

    This function return alues that are as follows0

    if !eturn alue 2 , then it indicates str is less than str3.

    if !eturn alue 4 , then it indicates str3 is less than str.

    if !eturn alue 5 , then it indicates str is e%ual to str3.

    #xample

    The following example shows the usage of strncmp() function.

    #include

    #include

    intmain ()

    {

    charstr='

    charstr='

    intret'

    strcpy(str,"abcdef")'

    strcpy(str,"FGHIJ?")'

    ret $strcmp(str,str)'

    if(ret

  • 7/23/2019 Useful C Library Function

    19/31

    elseif(ret >!)

    {

    printf("str is less than str")'

    +

    else

    {

    printf("str is e/ual to str")'

    +

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result0

    str is less than str

    C library function - strncmp()

    -dertisements

    Preious Page

    ext Page

    Description

    The C library function int strncmp(const char *str1, const char *str, size_t n) compares at most

    the first nbytes of str1and str.

    Declaration

    Following is the declaration for strncmp() function.

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    20/31

    intstrncmp(constchar7str,constchar7str,siBeCtn)

    Parameters

    str1'' This is the first string to be compared.

    str'' This is the second string to be compared.

    n'' The maximum number of characters to be compared.

    !eturn "alue

    This function return alues that are as follows0

    if !eturn alue 2 , then it indicates str is less than str3.

    if !eturn alue 4 , then it indicates str3 is less than str.

    if !eturn alue 5 , then it indicates str is e%ual to str3.

    #xample

    The following example shows the usage of strncmp() function.

    #include

    #include

    intmain ()

    {

    charstr='

    charstr='

    intret'

    strcpy(str,"abcdef")'

    strcpy(str,"FGHIJ?")'

  • 7/23/2019 Useful C Library Function

    21/31

    ret $strncmp(str,str,)'

    if(ret

  • 7/23/2019 Useful C Library Function

    22/31

    The C library function char *strcpy(char *dest, const char *src)copies the string pointed to

    by srcto dest.

    Declaration

    Following is the declaration for strcpy() function.

    char7strcpy(char7dest,constchar7src)

    Parameters

    dest'' This is the pointer to the destination array where the content is to be copied.

    src'' This is the string to be copied.

    !eturn "alue

    This returns a pointer to the destination string dest.

    #xample

    The following example shows the usage of strcpy() function.

    #include

    #include

    intmain()

    {

    charsrc=!'

    chardest=!!'

    memset(dest,D&!D,siBeof(dest))'

    strcpy(src,"8his is tutorialspoint.com")'

    strcpy(dest,src)'

  • 7/23/2019 Useful C Library Function

    23/31

    printf("?inal copied strin9 @ %s&n",dest)'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result0

    ?inal copied strin9 @ 8his is tutorialspoint.com

    C library function - strncpy()

    -dertisements

    Preious Page

    ext 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 srcto dest. 6n 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.

    char7strncpy(char7dest,constchar7src,siBeCtn)

    Parameters

    dest'' This is the pointer to the destination array where the content is to be copied.

    src'' This is the string to be copied.

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    24/31

    n'' The number of characters to be copied from source.

    !eturn "alue

    This function returns the final copy of the copied string.

    #xample

    The following example shows the usage of strncpy() function. 7ere we hae used function

    memset() to clear the memory location.

    #include

    #include

    intmain()

    {

    charsrc=!'

    chardest='

    memset(dest,D&!D,siBeof(dest))'

    strcpy(src,"8his is tutorialspoint.com")'

    strncpy(dest,src,!)'

    printf("?inal copied strin9 @ %s&n",dest)'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result0

    ?inal copied strin9 @ 8his is tu

  • 7/23/2019 Useful C Library Function

    25/31

    library function - strlen()

    -dertisements

    Preious Page

    ext 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.

    siBeCtstrlen(constchar7str)

    Parameters

    str'' This is the string whose length is to be found.

    !eturn "alue

    This function returns the length of string.

    #xample

    The following example shows the usage of strlen() function.

    #include

    #include

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    26/31

    intmain ()

    {

    charstr=!'

    intlen'

    strcpy(str,"8his is tutorialspoint.com")'

    len $strlen(str)'

    printf("Ken9th of A%sA is A%dA&n",str,len)'

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result0

    Ken9th of A8his is tutorialspoint.comA is A1A

    C library function - strstr()

    -dertisements

    Preious Page

    ext Page

    DescriptionThe C library function char *strstr(const char *haystac!, const char *needle) function finds the

    first occurrence of the substring needlein the string haystac!. The terminating 89,8 characters are

    not compared.

    Declaration

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    27/31

    Following is the declaration for strstr() function.

    char7strstr(constchar7haystacL,constchar7needle)

    Parameters

    haystac!'' This is the main C string to be scanned.

    needle'' This is the small string to be searched with'in haystac: string.

    !eturn "alue

    This function returns a pointer to the first occurrence in haystac: of any of the entire se%uence of

    characters specified in needle or a null pointer if the se%uence is not present in haystac:.

    #xample

    The following example shows the usage of strstr() function.

    #include

    #include

    intmain()

    {

    constcharhaystacL=!$"8utorials3oint"'

    constcharneedle=!$"3oint"'

    char7ret'

    ret $strstr(haystacL,needle)'

    printf("8he substrin9 is@ %s&n",ret)'

    return(!)'

    +

  • 7/23/2019 Useful C Library Function

    28/31

    $et us compile and run the aboe program that will produce the following result0

    8he substrin9 is@ 3oint

    C library function - strpbr!()

    -dertisements

    Preious Page

    ext Page

    Description

    The C library function char *strpbr!(const char *str1, const char *str) finds the first character in

    the string str1that matches any character specified in str. This does not include the terminating

    null'characters.

    Declaration

    Following is the declaration for strpbr:() function.

    char7strpbrL(constchar7str,constchar7str)

    Parameters

    str1'' This is the C string to be scanned.

    str'' This is the C string containing the characters to match.

    !eturn "alue

    This function returns a pointer to the character in str that matches one of the characters in str3 or

    1$$ if no such character is found.

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    29/31

    #xample

    The following example shows the usage of strpbr:() function.

    #include

    #include

    intmain ()

    {

    constcharstr=$"abcdef9hiMLl"'

    constcharstr=$""'

    char7ret'

    ret $strpbrL(str,str)'

    if(ret)

    {

    printf("?irst matchin9 character@ %c&n",7ret)'

    +

    else

    {

    printf("Hharacter not found")'

    +

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result0

    ?irst matchin9 character@

  • 7/23/2019 Useful C Library Function

    30/31

    C library function - strto!()

    -dertisements

    Preious Page

    ext Page

    Description

    The C library function char *strto!(char *str, const char *delim)brea:s string strinto a series of

    to:ens using the delimiter delim.

    Declaration

    Following is the declaration for strto:() function.

    char7strtoL(char7str,constchar7delim)

    Parameters

    str'' The contents of this string are modified and bro:en into smaller strings (to:ens).

    delim'' This is the C string containing the delimiters. These may ary from one call to

    another.

    !eturn "alue

    This function returns a pointer to the last to:en found in the string. - null pointer is returned if there

    are no to:ens left to retriee.

    #xample

    The following example shows the usage of strto:() function.

    http://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htmhttp://www.tutorialspoint.com/c_standard_library/string_h.htm
  • 7/23/2019 Useful C Library Function

    31/31

    #include

    #include

    intmain()

    {

    charstr=!$"8his is E www.tutorialspoint.com E website"'

    constchars=$"E"'

    char7toLen'

    67 9et the 2rst toLen 76

    toLen $strtoL(str,s)'

    67 walL throu9h other toLens 76

    while(toLen N$OPKK )

    {

    printf(" %s&n",toLen )'

    toLen $strtoL(OPKK,s)'

    +

    return(!)'

    +

    $et us compile and run the aboe program that will produce the following result0

    8his is

    www.tutorialspoint.com

    website