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.


0 comments:

Post a Comment