Saturday, May 23, 2020

Math function in c programming with examples

Note : - At first define math header file in a c program to access the all math function in c programming for example (sin(),cos(),tan(),pow(),sqrt()) and many more.

Math function in c programming with examples
Math function in c programming with examples

acos() function in c programming language : -

asoc () function in c programming helps to return the arc cosin value of element(x).

Synatx : -  

double acos(double x);

asoc () function in c will return the value between 0 and π.

 asoc() Example

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the acos of */
    value = 0.5;

    /* Calculate the Arc Cosine of value */
    result = acos(value);

    /* Display the result of the calculation */
    printf("The Arc Cosine of %f is %f\n", value, result);

    return 0;
}

 out put : -

Arc Cosine of 0.500000 is 1.047198

asin() function in c programming language : -

asin() function in c do the work same as asin() it helps you to return the arc sine value of element(x).

Synatx : - 

double asin(double x); 

asoc () function in c will return the value between -π/2 and π/2.

asin() Example

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the asin of */
    value = 0.5;

    /* Calculate the Arc Sine of value */
    result = asin(value);

    /* Display the result of the calculation */
    printf("The Arc Sine of %f is %f\n", value, result);

    return 0;
}

 out put : -

Arc Sine of 0.500000 is 0.523599

atan() function in c programming language : -

atan() function in c helps you to return the arc tengent value of element(x).

Synatx : - 

double atan(double x);

atan () function in c will return the value between -π/2 and π/2.

atan() Example

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the atan of */
    value = 0.5;

    /* Calculate the Arc Tangent of value */
    result = atan(value);

    /* Display result for calculation */
    printf("Arc Tangent of %f is %f\n", value, result);

    return 0;
}

 out put : -

Arc Tangent of 0.500000 is 0.463648

atan2() function in c programming language : -

atan2() function in c slightly different form tan2() because return the arc tengent value of element(x/y).

Synatx : - 

double atan2(double y, double x);

atan2() function in c will return the value between -π and π.

atan2() Example : - 

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value1, value2;
    double result;

    /* Assign the two values we will find the atan2 of */
    value1 = 0.5;
    value2 = -0.5;

    /* Calculate the Arc Tangent of value1 and value2 */
    result = atan2(value1, value2);

    /* Display the result of the calculation */
    printf("The Arc Tangent of %f and %f is %f\n", value1, value2, result);

    return 0;
}

 Output : -

Arc Tangent of 0.500000 and -0.500000 is 2.356194

ceil() function in c programming language : -

ceil() function in c to return the smallest integer that is greater than or equal to define value(x).

Synatx : -  

double ceil(double x);

ceil() function in c return the smallest integer that is greater than or equal to define value x

 Example for ceil(): -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the ceil of */
    value = 1.6;

    /* Calculate the ceil of value */
    result = ceil(value);

    /* Display the result of the calculation */
    printf("The ceil of %f is %f\n", value, result);

    return 0;
}

 Output : -

ceil of 1.600000 is 2.000000

cos() function in c : -

cos() function in c return the cosine value of define value(x).

Synatx : - 

double cos(double x);

It returns the cosine value of x and measured in radians.
cos() example : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the cos of */
    value = 0.5;

    /* Calculate the Cosine of value */
    result = cos(value);

    /* Display the result of the calculation */
    printf("The Cosine of %f is %f\n", value, result);

    return 0;
}
 Out put  : - 
 
Cosine of 0.500000 is 0.877583

cosh() function in c programming: -

cosh() function in c return the hyperbolic cosine value of define value(x).

Synatx : - 

double cosh(double x);

 remember if magnitude of x is to large then it returns an error.

 Example cosh() : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the cosh of */
    value = 0.5;

    /* Calculate the Hyperbolic Cosine of value */
    result = cosh(value);

    /* Display the result of the calculation */
    printf("The Hyperbolic Cosine of %f is %f\n", value, result);

    return 0;
}

Output : -

The Hyperbolic Cosine of 0.500000 is 1.127626

exp() function in c programming: -

exp() function in c returns the e with raise the power x.

Synatx : -  

double exp(double x); 

 Example of exp() : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the exp of */
    value = 5;

    /* Calculate the exponential of the value */
    result = exp(value);

    /* Display the result of the calculation */
    printf("The Exponential of %f is %f\n", value, result);

    return 0;
}

Output : -

Exponential of 2.100000 is 8.166170
 

fabs() function in c programming: -

fabs function in c programming helps to return the absolute value of a floating poin.

Synatx : - 

double fabs(double x);

 Example of fabs() : -


#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the fabs of */
    value = -2.1;

    /* Calculate the absolute value of value */
    result = fabs(value);

    /* Display the result of the calculation */
    printf("The Absolute Value of %f is %f\n", value, result);

    return 0;
}

Output : -

Absolute Value of -2.100000 is 2.100000

floor() function in c programming: -

floor() function in c programming is same as ceil() function but the only different is floor function return the largest interger that is smaller than or equal to x.

Synatx : -  

double floor(double x);

 Example of floor() : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the floor of */
    value = 1.6;

    /* Calculate the floor of value */
    result = floor(value);

    /* Display the result of the calculation */
    printf("The floor of %f is %f\n", value, result);

    return 0;
}

 Output : -

floor of 1.600000 is 1.000000

fmod() function in c language: -

fmod() function in c returns the remainder value when x is divided by y.

Synatx : - 

double fmod(double x, double y);

 Example of fmod() : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value1, value2;
    double result;

    /* Assign the values we will find the fmod of */
    value1 = 1.6;
    value2 = 1.2;

    /* Calculate the remainder of value1 / value2 */
    result = fmod(value1, value2);

    /* Display the result of the calculation */
    printf("The fmod of %f and %f is %f\n", value1, value2, result);

    return 0;
}

 Output : -

fmod of 1.600000 and 1.200000 is 0.400000

frexp() function in c language: -

frexp() function in c programming will splites a floating-point value into a fraction and an exponent . Here fraction value is return by frexp function and exponent is stored by exp variable in c.

Synatx : - 

double frexp(double value, int *exp);

remember that fraction must be greater than or equal to 0.5 and less than 1. or it can be zero or must be equal to zero.

Examples of fexp() :-

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    int e;
    double f;

    /* Assign the value we will find the exp of */
    value = 1.5;

    /* Calculate the fraction and exponential of the value */
    f = frexp(value, &e);

    /* Display the result of the calculation */
    printf("The Fraction and Exponential of %f are %f and %d\n", value, f, e);

    return 0;
}

Output : -
 Fraction and Exponential of 1.500000 are 0.750000 and 1

idexp() function in c language: -

If you want to combine the fraction and an exponent into a floating-point value so idexp function in c is best for useing in c programming.

Synatx : -  

double ldexp(double fraction, int exp);

Examples of idexp() :-

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    int e;
    double result;

    /* Assign the value and exponential we will use to find the ldexp */
    value = 1.5;
    e = 2;

    /* Calculate the ldexp of the value and the exponential */
    result = ldexp(value, e);

    /* Display the result of the calculation */
    printf("The ldexp of %f with exponential %d is %f\n", value, e, result);

    return 0;
}

log() function in c programming language : -

log() function in c programming will help you in c program to find the logarithm value of x to the base of e.

Synatx : -

double log(double x);

Example of log()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will calculate the log of */
    value = 1.5;

    /* Calculate the log of the value */
    result = log(value);

    /* Display the result of the calculation */
    printf("The Natural Logarithm of %f is %f\n", value, result);

    return 0;
}

 Output : -

Natural Logarithm of 1.500000 is 0.405465

log10() function in c programming: -

log10() function in c programming will help you in c program to find the logarithm value of x to the base of 10. This function same as like log() function the only different is log10() function return the value with base 10 not with e.

Synatx : -

double log10(double x);

Example of log10()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will calculate the log10 of */
    value = 1.5;

    /* Calculate the log10 of the value */
    result = log10(value);

    /* Display the result of the calculation */
    printf("The Base 10 Logarithm of %f is %f\n", value, result);

    return 0;
}

 Output : -

Base 10 Logarithm of 1.500000 is 0.176091

log10() function in c programming: -

modf() function in c programming helps to splites floating point value into an integer and a fractional part. The fractional part return with modf() function in c programming and integer part is stored in the iptr variable

Synatx : - 

double modf(double value, double *iptr);

Example of modf()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double i, f;

    /* Assign the value we will calculate the modf of */
    value = 1.7;

    /* Calculate the modf of the value returning the fractional and integral parts */
    f = modf(value, &i);

    /* Display the result of the calculation */
    printf("The Integral and Fractional parts of %f are %f and %f\n", value, i, f);

    return 0;
}

 Output : - 

Integral and Fractional parts of 1.700000 are 1.000000 and 0.700000

    Some Common Function in c

 pow() function : -

 pow() function in c programming return the value of define element x which is raised to the power of y.

Synatx : -

double pow(double x, double y);

Example of pow()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value1, value2;
    double result;

    /* Assign the values we will use for the pow calculation */
    value1 = 4;
    value2 = 2;

    /* Calculate the result of value1 raised to the power of value2 */
    result = pow(value1, value2);

    /* Display the result of the calculation */
    printf("%f raised to the power of %f is %f\n", value1, value2, result);

    return 0;
}

 sin() function : -

 sin() function in c programming returns the sine of defined value x.

Synatx : -

double sin(double x);

Example of sin()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the sin of */
    value = 0.5;

    /* Calculate the Sine of value */
    result = sin(value);

    /* Display the result of the calculation */
    printf("The Sine of %f is %f\n", value, result);

    return 0;
}

 sinh() function : -

 sinh() function in c programming returns the hyperbolic sine of defined value x.

Synatx : -

double sinh(double x);

Example of sinh()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the sinh of */
    value = 0.5;

    /* Calculate the Hyperbolic Sine of value */
    result = sinh(value);

    /* Display the result of the calculation */
    printf("The Hyperbolic Sine of %f is %f\n", value, result);

    return 0;
}

sqrt() function in c: -

sqrt() function in c programming return the square root of defined value x.

Synatx : -

double sqrt(double x);

Example of sqrt()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the sqrt of */
    value = 25;

    /* Calculate the square root of value */
    result = sqrt(value);

    /* Display the result of the calculation */
    printf("The Square Root of %f is %f\n", value, result);

    return 0;
}

 

tan() function in c: -

tan() function in c programming return the tangent of defined value x.

Synatx : -

double tan(double x);

Example of  tan()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the tan of */
    value = 0.5;

    /* Calculate the Tangent of value */
    result = tan(value);

    /* Display the result of the calculation */
    printf("The Tangent of %f is %f\n", value, result);

    return 0;
}

 

tanh() function in c: -

tanh() function in c programming return the hyperbolic tangent of defined value x.

Synatx : -

double tanh(double x);

Example of  tanh()

 

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the tanh of */
    value = 0.5;

    /* Calculate the Hyperbolic Tangent of value */
    result = tanh(value);

    /* Display the result of the calculation */
    printf("The Hyperbolic Tangent of %f is %f\n", value, result);

    return 0;
}

 these all are the math function in c programming languages which helps you to reduce the length of a c program and also save time in c programming.

 

Wednesday, May 20, 2020

locale.h ,signal.h,stdarg.h functions in c programming





locale.h ,signal.h,stdarg.h functions in c programming
locale.h ,signal.h,stdarg.h functions in c programming

va_arg function() in c programming

va_arg function in c programming use to fetch an arguments form argument list for a function and type. va_arg function does not determine which one is the last arguments for function.

Syntax :- 

type va_arg(va_list ap, type)

va_end function() in c programming

va_end() function in c used to end the process of the variable argument list.

Syntax :-

void va_end(va_list ap);

va_start function() in c programming

 va_start function in c is used to initializes the variable argument list and va_start function is called before the va_arg function in c.

 Syntax : -

void va_start(va_list ap, parmN);
  
signal.h functions in c programming

raise function() in c programming

raise function in c is used to raises the signals in c represented by sig.

 Syntax : -

int raise(int sig);

Example : -


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void signal_handler(int signal)
{
    /* Display a message indicating we have received a signal */
    if (signal == SIGUSR1) printf("Received a SIGUSR1 signal\n");

    /* Exit the application */
    exit(0);
}

int main(int argc, const char * argv[])
{
    /* Display a message indicating we are registering the signal handler */
    printf("Registering the signal handler\n");

    /* Register the signal handler */
    signal(SIGUSR1, signal_handler);

    /* Display a message indicating we are raising a signal */
    printf("Raising a SIGUSR1 signal\n");

    /* Raise the SIGUSR1 signal */
    raise(SIGUSR1);

    /* Display a message indicating we are leaving main */
    printf("Finished main\n");

    return 0;
}



signal function() in c programming

signal function in c define a function for handle the signals.

 Syntax : - 

void (*signal(int sig, void (*func)(int)))(int);

 it return the pointer to previous handler for this signal.

Examples : -

/* Example using signal by function in programming */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void signal_handler(int signal)
{
    /* Display a message indicating we have received a signal */
    if (signal == SIGUSR2) printf("Received a SIGUSR2 signal\n");

    /* Exit the application */
    exit(0);
}

int main(int argc, const char * argv[])
{
    /* Display a message indicating we are registering the signal handler */
    printf("Registering the signal handler\n");

    /* Register the signal handler */
    signal(SIGUSR2, signal_handler);

    /* Display a message indicating we are raising a signal */
    printf("Raising a SIGUSR2 signal\n");

    /* Raise the SIGUSR2 signal */
    raise(SIGUSR2);

    /* Display a message indicating we are leaving main */
    printf("Finished main\n");

    return 0;
}


setjmp() function in c programming

setjmp() function in c use to store the current environment in env variable to preparation for a call form lognjmp() function in future. .
  Syntax : - 

int setjmp(jmp_buf env);

Example : - 
 
#include <stdio.h>
#include <setjmp.h>

/* Declare a global jmp_buf variable that is available to both func and main */
static jmp_buf env;

void func(void)
{
    /* Display a message indicating we are entering func */
    printf("Starting func\n");

    /* Return to main with a return code of 1 (can be anything except 0) */
    longjmp(env, 1);

    /* Display a message indicating we are leaving func */
    printf("Finishing func\n"); /* This will never be executed! */
}

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    int result;

    /* Display a message indicating we are starting main */
    printf("Starting main\n");

    /* Save the calling environment, marking where we are in main */
    result = setjmp(env);

    /* If the result is not 0 then we have returned from a call to longjmp */
    if (result != 0)
    {
        /* Display a message indicating the call to longjmp */
        printf("longjmp was called\n");

        /* Exit main */
        return 0;
    }

    /* Call func */
    func();

    /* Display a message indicating we are leaving main */
    printf("Finished main\n");

    return 0;
}


longjmp() function in c programming

longjmp() function in c use to restore the environment that comes from the setjmp() call that saved env

  Syntax : -
 

void longjmp(jmp_buf env, int val);

Example : - 

#include <stdio.h>
#include <setjmp.h>

/* Declare a global jmp_buf variable that is available to both func and main */
static jmp_buf env;

void func(void)
{
    /* Display a message indicating we are entering func */
    printf("Starting func\n");

    /* Return to main with a return code of 1 (can be anything except 0) */
    longjmp(env, 1);

    /* Display a message indicating we are leaving func */
    printf("Finishing func\n"); /* This will never be executed! */
}

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    int result;

    /* Display a message indicating we are starting main */
    printf("Starting main\n");

    /* Save the calling environment, marking where we are in main */
    result = setjmp(env);

    /* If the result is not 0 then we have returned from a call to longjmp */
    if (result != 0)
    {
        /* Display a message indicating the call to longjmp */
        printf("longjmp was called\n");

        /* Exit main */
        return 0;
    }

    /* Call func */
    func();

    /* Display a message indicating we are leaving main */
    printf("Finished main\n");

    return 0;
}


 locale.h header file functions in c programming

localeconv() function in c : - 

localeconv() function in c programming return a pointer to a structure that collect the all current locale information.

  Syntax : -

struct lconv *localeconv(void);

Example : -

#include <stdio.h>
#include <locale.h>

int main(int argc, const char * argv[])
{
    /* Define a temporary variable */
    struct lconv *loc;

    /* Set the locale to the environment default */
    setlocale (LC_ALL, "");

    /* Retrieve a pointer to the current locale */
    loc = localeconv();

    /* Display some of the locale settings */
    printf("Thousands Separator: %s\n", loc->thousands_sep);
    printf("Currency Symbol: %s\n", loc->currency_symbol);

    return 0;
}

setlocale() function in c : -

setlocale() function in c programming gives you apportunity to set the program's local information or environment.

  Syntax : -

char *setlocale(int category, const char *locale);

Example : -

#include <stdio.h>
#include <locale.h>

int main(int argc, const char * argv[])
{
    /* Define a temporary variable */
    struct lconv *loc;

    /* Set the locale to the POSIX C environment */
    setlocale (LC_ALL, "C");

    /* Retrieve a pointer to the current locale */
    loc = localeconv();

    /* Display some of the locale settings */
    printf("Thousands Separator: %s\n", loc->thousands_sep);
    printf("Currency Symbol: %s\n", loc->currency_symbol);

    /* Set the locale to the environment default */
    setlocale (LC_ALL, "");

    /* Retrieve a pointer to the current locale */
    loc = localeconv();

    /* Display some of the locale settings */
    printf("Thousands Separator: %s\n", loc->thousands_sep);
    printf("Currency Symbol: %s\n", loc->currency_symbol);

    return 0;
}

assert() function in c : -

assert() is a macro in c programming which is used as a function in c. It checks the valuse of the expression and tha expression would be true normal circumstances.

If value of an expression is nonzero then macro assert() does nothing or value is zero then assert macro writes a message to stderr and close/terminates the programs by calling abort.

  Synatx : -

void assert(int expression);

Example :- 

#include <stdio.h>
#include <assert.h>

int main(int argc, const char * argv[])
{
    /* Define an expression */
    int exp = 1;

    /* Display the value of exp */
    printf("Exp is %d\n", exp);

    /* Assert should not exit in this case since exp is not 0  */
    assert(exp);

    /* Change expression to 0 */
    exp = 0;

    /* Display the value of exp */
    printf("Exp is %d\n", exp);

    /* In this case exp is 0 so assert will display an error and exit */
    assert(exp);

    return 0;
}

thses all are the special function in c which is used in c programs.


Tuesday, May 19, 2020

Learn about all stdio.h headerfile function in c programming

clearerr() function in c programming use to clear the error and end-of-file which is given by the pointer stream as shown in syntax 

 Syntax :-  

void clearerr(FILE *stream);

fclose function in c programming : -

fcolse() function in c use to close all buffer and unwanted  written data in stream pointed by *stream.

 Syntax : - 

int fclose(FILE *stream);

fefo() function in c programming : -

fefo() function in c programming is used to validate for end of file is reached or not.

Syntax : - 

int feof(FILE *stream);

ferror() function in c programming : -


ferror() function in c programming is used to check error indecator for a stream pointer to by steam.

Syntax : - 


int ferror(FILE *stream);

fflush() function in c programming : -


fflush() function in c use for file headling it is used to flush/clear the file and buffer. 

Syntax : -

int fflush(FILE *stream);

fgetc() function in c programming : -


fgetc() function in c use in file headling.It is used to read a line in from the specified file and store it into a string. It stops when either n-1 character is read or newline character is read and end of file is reached.

Syntax : - 

int fgetc(FILE *stream);

fgetpos() function in c programming : -

fgetpos() function in c programming use to store the current position of stream into a defined object pointed by pos.

Syntax : -

int fgetpos(FILE *stream, fpos_t *pos);

fgets() function in c programming : - 

fgets() function in c programming use to read the character from the file pointed by stream. It stops when either n-1 character is read or newline character is read and end of file is reached.

Syntax : -

char *fgets(char *s, int n, FILE *stream);

fopen() function in c programming : -

fgets() function in c programming use to open a file to perform various operations like writting, reading etc.

Syntax: - 

FILE *fopen(const char *filename, const char *mode);

fprintf() function in c programming : -

fprintf() function in c programming language is used to write a formatted output to stream.

Syntax : -

int fprintf(FILE *stream, const char *format, ...);

fputc() function in c programming : -

fputc() function in c language is used to write a character into a file. This function writes on character at a time and move the pointer where the next character is going to write.

Syntax : -

int fputc(int c, FILE *stream);

fputs() function in c programming : -

fputs() function in c language is used to write a string into a file. It accepts two arguments pointer to string and file pointer.

Syntax : - 

int fputs(const char *s, FILE *stream);

fread() function in c programming : -

fread() function in c language is used to read the data from pointed file and store in a pointer *ptr.

Syntax : - 

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

freopen() function in c programming : -

freopen() function in c language is used to open an new file associated it with stream in c programming and also close old file in the stream.

Syntax:-
FILE *freopen(const char *filename, const char *mode, FILE *stream);


fscanf() function in c programming : -

fscanf() function in c programming is used to read the formatted output.

Syntax : - 

int fscanf(FILE *stream, const char *format, ...);
  

fseek() function in c programming : -

fseek() function in c language is use to move the file pointer position to the given position.
Syntax : - 

int fseek(FILE *stream, long int offset, int whence);

fsetpos() function in c programming : -

fsetpos() function in c language is use to move the file position to given indicated location for the stream. It also reset the indecator at the end-of-file. 

Syntax : -

int fsetpos(FILE *stream, const fpos_t *pos);

ftell() function in c programming : -

fsetpos() function in c programming language is use to return the current position of file for the stream pointed by stream.

 Syntax : -

long int ftell(FILE *stream);

fwrite() function in c programming : -

fwrite() function in c language is use to write nmemb into a pointed file in c program.

 Syntax : -
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

fwrite() function in c programming : -

fwrite() function work as same as fread() function in c programming these function use to read a character from the stream pointed to by stream.

 Syntax : -
int getc(FILE *stream);

getc() function in c programming : -

getc() function in c programming read the characters from c file stdin stream.

 Syntax : -
int getchar(void);

getc() function return the read character

gets() function in c programming : -

gets() function in c programming read the characters and store into a pointer *s

 Syntax : -

char *gets(char *s);

gets() function return s

perror() function in c programming : -

perror() function in c is very important function this function send a error message to stderr stream into following format:-

string: error-message

 Syntax : -
void perror(const char *s);

putc() function in c programming : -

putc() function is used to display a characters on standard output or it use to write into a file in c programming.
  
Syntax : - 

 int putc(int c, FILE *stream);

putc() function return the write character

putchar() function in c programming : -

putchar() function in c programming use to write a character into the stdout stream or output stream.

Syntax : - 

int putchar(int c);

puts() function in c programming : -

putchar() function in c programming use to write a string into a stdout file in c.

 Syntax : -

int puts(const char *s);

it return the non negative value if successful.

remove() function in c programming : -


remove() function in c programming use to remove a file which is pointed by filename in c.

 Syntax : - 

int remove(const char *filename);

it return the zero if file remove successful.

rename() function in c programming : -


rename() function in c use to rename a specified file in c. You have to close that file before rename it because a file can not be renamed if that file is open.

 Syntax : -

int rename(const char *old, const char *new);

it return the zero if file renamed successfully.

rewind() function in c programming : -


rename() function in c programming set the file position to the beginning of the file for the stream pointed ny the stream.

 Syntax : -

void rewind(FILE *stream);

setbuf() function in c programming : -


setbuf() function in c programming let you changed the way of buffered and also let you set the size and location of the buffer otherwise it takes default values in c.

Control buffer : -

(void) setvbuf(stream, buf, _IOFBF, BUFSIZ);

default buffer : - 

(void) setvbuf(stream, NULL, _IONBF, 0);

 Syntax : -

void setbuf(FILE  *stream, char *buf);


tmpfile() function in c programming : -


tmpfile() function in c programming creates the temporary file.
temporary file will be deleted if you close that file or at the end of the programs.

 Syntax : -
FILE *tmpfile(void);

tmpnam() function in c programming : -


tmpfile() function in c programming gives a name to the  temporary file.If the argument is a null pointer, then it store an static file name otherwise it store the changed file name into s.

 Syntax : -

char *tmpnam(char *s);


These all are the file handling in c programming which is going to use in stdio.h header file before using it don't forget to include the stdio.h header file on the head.

Sunday, May 17, 2020

stdlib.h function like abort(), abs(),atof(),atoi() in c programming


This chapter will help you to learn about abort(), abs(),atof(),atoi(),atol(),bsearch(), exit(),getenv(), labs(),mblen(),mbstowcs(),qsort(),rand(),srand(),strtod(),strtod(),system() and many more function in c programming.

stdlib.h function like abort(), abs(),atof(),atoi() in c programming
stdlib.h function like abort(), abs(),atof(),atoi() in c programming

abort() function in c programming :- 

abort() function in c programming raises the SIGABRT signal and causes the program termination but abort() function may not close files that are open and neither may also not delete temporary files nor may not flush stream buffer. It also don't call
registered function with atexit().

  • Example for abort function in c programming

#include <stdio.h>
#include <stdlib.h> 
int main() 
{ 
  FILE *fp = fopen("C:\\myfile.txt", "w"); 
    
  if(fp == NULL) 
  { 
    printf("\n could not open file "); 
    getchar(); 
    exit(1); 
  }   
    
  fprintf(fp, "%s", "Geeks for Geeks"); 
    
  /* ....... */
  /* ....... */
  /* Something went wrong so terminate here */  
  abort(); 
    
  getchar(); 
  return 0;   
}   

abs() function in c programming :- 

abs() function in c Language returns the absolute value of an integer. Absolute value is always positive.

Syntax : - 

int abs(int x);
  •  Example for abs() function in c : - 

     


#include <stdio.h>
#include <stdlib.h> 
int main()
{
   int m = abs(200);     // m is assigned to 200
   int n = abs(-400);    // n is assigned to -400
 
   printf("Absolute value of m = %d\n", m);
   printf("Absolute value of n = %d \n",n);
   return 0;
}

it return a absolute value.

atexit() function in c  : - 

atexit() function in cprogramming language is a register function  which is called at the time of termination of a program normally.

call atexit() function number of times so the last function to be registered as temination which is called to terminate a program normally. 
Syntax : - 

int atexit(void (*func)(void));

atof() function in c 

atof function in c programming use to convert a string to a floating-point number (double).

 atof function skip all white space characters at the beginning of the string and it convert the subsequent characters as part of the number. and it stop when it encounters the first character that isn't a number.

Syntax :- 

double atof(const char *nptr);
floating-point representation of a string return in atof function in c.

atoi() function in c

atoi function in c programming use to convert a string to a integer.

Syntax :-

int atoi(const char *nptr);
integer representation of a string return in atoi function in c.

atol() function in c

atol function in c programming use to convert a string to a long integer.

Syntax :-

long int atol(const char *nptr);

long integer representation of a string return in atol function in c

bsearch() function in c

bsearch() function in c programming use to search an sorted array of nel object initial element which is pointed to by base, and the element that matches the object is pointed to by key. Size of element in that array is specified by width.

 Syntax : -

void *bsearch(const void *key, const void *base, size_t num_members,
              size_t size, int (*compare_function)
              (const void *, const void *));

bsearch() function in c is return a pointer of matching array.

exit() function in c programming :-

exit() function in c programming use to calls the all function which is registered with atexit() and terminates program, and this function also flushed File buffers, streams are closed, and temporary files are deleted.

Syntax : -

void exit(int status);

getnev() function in c : -

getnev() function in c programming search the environment list for operating system and return a ponter for that string which store the list and string associated with name.

Syntax : - 

char *getenv(const char *name);
 its return a pointer for that string which is associated with name and contain the list of environment for operating system.

 labs() function in c programming  :- 

labs() function in c return the absolute value for a long integer.

 Synatx :-

long int labs(long int x);

 it return the absolute value for a long intger.

mblen() function in c programming : -

mblen() function in c programming compute the lenth of multi byte character.

 Synatx :-

int mblen(const char *s, size_t n);
 it examines the first n byte of string and check is it foming a character or not.

mbstowcs() function in c programming : - 

mbstowcs() function in c is use to convert Multibyte String to Wide Character Stringin c programming.

Syntax : -

size_t mbstowcs(wchar_t *pwcs, const char *s, size_t n);

qsort() function in c :-

qsort() function in c programming use to sort an array.

 Synatx : -

void qsort(void *base, size_t num_members, size_t size, 
           int (*compare_function) (const void *, const void *));
it return an sorted array in descending order

rand() function in c :-

rand() function in c programming is use to Generate Pseudo-Random Number and return a number between 0 to RAND_MAX

 Syntax : -

int rand(void);

srand() function in c :-

When srand() function is called in c programming then initializes the sequence of pseudo-random numbers

Syntax : -

void srand(unsigned int seed);

strtod() function in c :- 

strtod() function in c programming use to convert a string to a double.

Syntax : - 

double strtod(const char *nptr, char **endptr);

it return the double representation of a string.

strtol function in c :-

strtod() function in c programming use to convert a string to a long integer.

Syntax : -

long int strtol(const char *nptr, char **endptr, int base);
it return the long integer representation of a string.

Example:-


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    char value[10];
    char *eptr;
    long result;

    /* Copy a value into the variable */
    /* It's okay to have whitespace before the number */
    strcpy(value, " 123");

    /* Convert the provided value to a decimal long */
    result = strtol(value, &eptr, 10);

    /* If the result is 0, test for an error */
    if (result == 0)
    {
        /* If a conversion error occurred, display a message and exit */
        if (errno == EINVAL)
        {
            printf("Conversion error occurred: %d\n", errno);
            exit(0);
        }

        /* If the value provided was out of range, display a warning message */
        if (errno == ERANGE)
            printf("The value provided was out of range\n");
    }

    /* Display the converted result */
    printf("%ld decimal\n", result);

    /* Copy a hexadecimal value into the variable */
    strcpy(value, "0x19e");

    /* Convert the provided value to a decimal long */
    result = strtol(value, &eptr, 16);

    /* If the result is 0, test for an error */
    if (result == 0)
    {
        /* If a conversion error occurred, display a message and exit */
        if (errno == EINVAL)
        {
            printf("Conversion error occurred: %d\n", errno);
            exit(0);
        }

        /* If the value provided was out of range, display a warning message */
        if (errno == ERANGE)
            printf("The value provided was out of range\n");
    }

    /* Display the converted result */
    printf("%lx hexadecimal\n", result);

    return 0;
}

system() function in c :- 

system() function in c programming allows a C program to run an another program by passing a command line  to the operating system's.

Syntax :- 

int system(const char *string);

wcstombs function in c :-

wcstombs() function in c programming use to convert wide character string to Multibyte String in c.

.syntax:-

size_t wcstombs(char *s, const wchar_t *pwcs, size_t n);

wctomb()function in c :- 

wctomb() function in c programming Convert Wide Character to Multibyte Character in c.

syntax : - 

int wctomb(char *s, wchar_t wchar);

 these all function are stdlib.h function these are very useful in c programming.