Showing posts with label function in c. Show all posts
Showing posts with label function in c. Show all posts

Monday, April 26, 2021

, , , , ,

strncpy() Function in c programming with example

 strncpy() Function in c programming is used to copies the specified number of characters from source string (src) to destination string and this is an only difference between strcpy() function and strncpy() Function.

 Syntax for strncpy() in c

char *strncpy(char *str1, const char *str2, size_t n)


In the given syntax str1 is Destination string and str2 – Source string.

strncpy() in c return the pointer to the destination string after this function copied  n characters of source string.

Example program for strncpy() function in c

#include <stdio.h>
#include <string.h>
int main () {
   char str1[20]; 
   char str2[25];
   /* The first argument in the function is destination string. 
    * In this case we are doing full copy using the strcpy() function. 
    * Here string str2 is destination string in which we are copying the 
    * specified string 
    */ 
   strcpy(str2, "welcome to fucntioninc.in");

   /* In this case we are doing a limited copy using the strncpy()
    * function. We are copying only the 7 characters of string str2 to str1
    */
   strncpy(str1, str2, 7);

   printf("String str1: %s\n", str1); 
   printf("String str2: %s\n", str2);

   return 0;
}



Friday, April 23, 2021

, , , ,

ellipse function in C programming

 ellipse function in c

 Learn to Declarations ellipse function in computer graphic

 void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius)


  This function is used to draw an ellipse where (x,y) are coordinates of center of that draw ellipse, stangle is the starting angle, end angle is the ending angle,  int xradius, int yradius are parameters specifies the X and Y radius of that drawn ellipses. 

Well if you want to draw an ellipse for strangles and end angle should be 0 and 360 respectively.

Program example for ellipse function in computer graphic


#include<graphics.h>
#include<conio.h>

main()
{
   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   ellipse(100, 100, 0, 360, 50, 25);

   getch();
   closegraph();
   return 0;
}


Monday, April 5, 2021

, , , , ,

Arc function in C with graphics in c

 Declaration: void arc(int x, int y, int stangle, int endangle, int radius);

"arc" operate is employed to draw associate arc with center (x, y) for example draw a corcle in c programming and stangle specifies beginning angle for arc, end angle for arc can be define by endangle and the and last parameter specifies the radius of the arc.

 arc operate may also be accustomed draw a circle except for that beginning angle and finish angle ought to be zero and 360 severally.

Draw A circle in c programming 

 #include <graphics.h>
#include <conio.h>

int main()
{
   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   arc(100, 100, 0, 135, 50);
   
   getch();
   closegraph();
   return 0;
}



Well in the written c program for circle in c which is having (100,100) center '0' is the starting angle, 135 is the end angle and '50' is an radious.

Thursday, February 25, 2021

, , , ,

Example Program for isdigit() in c - Function in c

 isdigit() function in C programming use to checks whether a character is numeric character (0-9) or not.

 Prototype of isdigit() Function in C

int isdigit( int arg );


  • isdigit() function in c programming take a single argument which is an integer and return type int.
  • Given character is converted to its ASCII value in isdigit() Function
  • It is defined in <ctype.h> header file in c.

Return value for C isdigit()

isdigit() Return value in c
 isdigit() Return value in c


Example Program for isdigit() function in c


#include <stdio.h>
#include <ctype.h>

int main()
{
    char c;
    c='5';
    printf("Result when numeric character is passed: %d", isdigit(c));

    c='+';
    printf("\nResult when non-numeric character is passed: %d", isdigit(c));

    return 0;
}

OUTPUT : - 

Result when numeric character is passed: 1
Result when non-numeric character is passed: 0

 

 

Thursday, December 17, 2020

, , ,

fopen function in c programming with it's example

 fopen c function is used to open a file which is denoted by the file name in c programming.

Syntax for fopen function in c : -

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

Parameters For for fopen c : -


filename : - this element contain the file name which is going to be open.

Mode :- This string in c contain the method to access the file.

Check the mode for open a file in c programming with the help of fopen c function. 

  • "r" Mode : - This mode opens a file for reading.
  • "w" Mode : - this one use to create a empty file for writing.
  • "a" Mode : - It use to append the data  at the end of the file.
  • "r+" Mode : - This mode open a file to make an update in reading and writing.
  •  "w+" Mode :  - Create an empty file for both reading and writing.
  •  "a+" Mode : - Use to open an file for appending and also for reading.

Return Value for fopen c function : -

  •  fopen function in c return a file pointer otherwise give a null.
  • errno global variable is used to indicate the error in fopen function.

Example program for fopen function in c :

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

int main () {
   FILE * fp;

   fp = fopen ("file.txt", "w+");
   fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
   
   fclose(fp);
   
   return(0);
}


Example for fopen c
Example program for fopen c

Thursday, December 3, 2020

, , , ,

strtok Fucntion in c with example Program for strtok

  •  strtok function in c programming will help you to give the token of given input.
  • This kind of function (strtok in c ) is very useful which reduce the length of c program.
strtok in c
 strtok

 

Use of the strtok function in c

strtok is an inbuilt function which is part of the <string.h> c header file.

Let's see first : - 

#include <string.h>
 
char* strtok(char* str, const char* delim);
  • This function take an input str and also a delimiter character delim.
  • Then strtok() in c split the input string into the tokens which is based on the delimited character.

Return Value of strtok in c

  • This function return a single string.
  • strtok() in c call this function continuously until we get NULL input sting.

Example for strtok in c

#include <stdio.h>
#include <string.h>
 
int main() {
    // Our input string
    char input_string[] = "Hello from function in c!";
 
    // Our output token list
    char token_list[20][20]; 
 
    // We call strtok(input, delim) to get our first token
    // Notice the double quotes on delim! It is still a char* single character string!
    char* token = strtok(input_string, " ");
 
    int num_tokens = 0; // Index to token list. We will append to the list
 
    while (token != NULL) {
        // Keep getting tokens until we receive NULL from strtok()
        strcpy(token_list[num_tokens], token); // Copy to token list
        num_tokens++;
        token = strtok(NULL, " "); // Get the next token. Notice that input=NULL now!
    }
 
    // Print the list of tokens
    printf("Token List:\n");
    for (int i=0; i < num_tokens; i++) {
        printf("%s\n", token_list[i]);
    }
 
    return 0;
}


We use the strtok(NULL, " ") function in c and use the loop for until the get NULL.

Output strtok in c

Token List:
Hello
from
Function
in
c

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.