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.

0 comments:

Post a Comment