Saturday, May 22, 2021

, , , ,

clock() function in c with example program

 To calculate the time in c programming we can use clock() function in c which is define in time.h header file clock  can call at the beginning and end of the code for which will help to measure time.

In this process subtract the obtain values for the clock function, and then divide by CLOCKS_PER_SEC (the number of clock ticks per second)to get time.

Read also :-  ellipse function in C programming

 

Syntax for clock function in c


 #include <time.h>
     
     clock_t start, end;
     double cpu_time_used;
     
     start = clock();
     ... /* Do the work. */
     end = clock();
     cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

Following C program will tell how to measure time with the help of clock() function in c and we also use fun() function which will help to terminate the process after enter key press to terminate.

Example program for clock() function in c



/* Program to demonstrate time taken by function fun() */
#include <stdio.h>
#include <time.h>
  
// A function that terminates when enter key is pressed
void fun()
{
    printf("fun() starts \n");
    printf("Press enter to stop fun \n");
    while(1)
    {
        if (getchar())
            break;
    }
    printf("fun() ends \n");
}
  
// The main program calls fun() and measures time taken by fun()
int main()
{
    // Calculate the time taken by fun()
    clock_t t;
    t = clock();
    fun();
    t = clock() - t;
    double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
  
    printf("fun() took %f seconds to execute \n", time_taken);
    return 0;
}

OUTPUT :- 

clock function in c
clock function in c



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;
}


Wednesday, April 14, 2021

, , , ,

line function in computer graphics

line function in c programming :-

Well line function in c computer graphics is used as it name says to draw a line from a point(x1,y1) to point(x2,y2) where (x2,y2) are end points to draw a line.

Here you can also check the c program to draw a line in computer graphics.

 C example program

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

main()
{
   int gd = DETECT, gm;

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

   line(100, 100, 200, 200);

   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.