C Program to Draw an Eclipse Shape Using C Graphics

Write a program in C to draw an eclipse on screen using graphics.h header file

In this program, we will draw an eclipse on screen having centre at mid of the screen. We will use ellipse functions of graphics.h header file to draw eclipse on screen. Below is the detailed descriptions of ellipse function.

void ellipse(int xCenter, int yCenter, int startAngle, int endAngle, int xRadius, int yRadius);

Function Argument Description
xCenter X coordinate of center of eclipse.
yCenter Y coordinate of center of eclipse.
startAngle Start angle of the eclipse arc.
endAngle End angle of the eclipse arc. It will draw eclipse starting form startAngle till endAngle.
xRadius Horizontal radius of the eclipse.
yRadius Vertical radius of the eclipse.

To draw a complete eclipse, we should pass start and end angle as 0 and 360 respectively.

C program to draw an eclipse using graphics

In this program we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). First of all we will calculate the center co-ordinates of eclipse which is the center of screen bu calling getmaxx and getmaxy function. Then we draw full eclipse by calling ellipse function.

C Program to Draw an Eclipse Shape Using C Graphics

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
 
int main(){
   int gd = DETECT,gm;
   int x ,y;
   initgraph(&gd, &gm, "X:\\TC\\BGI");
   /* Initialize center of ellipse with center of screen */
   x = getmaxx()/2;
   y = getmaxy()/2;
 
   outtextxy(x-100, 50, "ELLIPSE Using Graphics in C");
   /* Draw ellipse on screen */
   ellipse(x, y, 0, 360, 120, 60);
 
   getch();
   closegraph();
   return 0;
}

Program Output

ELLIPSE

C Program to Draw Concentric Circles of Different Colors Using C Graphics

C Program to Draw Concentric Circles of Different Colors Using C Graphics

Write a program in C to draw concentric circle on screen using graphics.h header file

In this program, we will draw four circle on screen having centre at mid of the screen and radius 30, 50, 70 and 90 pixels. We will use outtextxy and circle functions of graphics.h header file. Below is the detailed descriptions of graphics functions used in this program.

Function Description
initgraph It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode.
getmaxx It returns the maximum X coordinate in current graphics mode and driver.
getmaxy It returns the maximum Y coordinate in current graphics mode and driver.
outtextxy It displays a string at a particular point (x,y) on screen.
circle It draws a circle with radius r and centre at (x, y).
setcolor It changes the current drawing colour. Default colour is white. Each color is assigned a number, like BLACK is 0 and RED is 4. Here we are using colour constants defined inside graphics.h header file.
closegraph It unloads the graphics drivers and sets the screen back to text mode.

C program to draw concentric circles using graphics

In this program we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). It is the first step you need to do during graphics programming. Setting graphics driver as DETECT, means instructing the compiler to auto detect graphics driver. Here we are using getmaxx and getmaxy function to find the centre coordinate of the screen and setcolor function to change he colour of drawing.

C Program to Draw Concentric Circles of Different Colors Using C Graphics

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
 
int main(){
   int gd = DETECT,gm;
   int x ,y;
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   /* Initialize center of circle with center of screen */
   x = getmaxx()/2;
   y = getmaxy()/2;
 
   outtextxy(240, 50, "Concentric Circles");
   /* Draw circles on screen */
   setcolor(RED);
   circle(x, y, 30);
   setcolor(GREEN);
   circle(x, y, 50);
   setcolor(YELLOW);
   circle(x, y, 70);
   setcolor(BLUE);
   circle(x, y, 90);
 
   getch();
   closegraph();
   return 0;
}

Program Output

CONCIR

C Program to Draw Bar Graph Using C Graphics

Write a program in C to draw bar chart on screen using graphics.h header file

In this program, we will draw a bar graph on screen. Here, we will use linesetfillstyle and bar functions of graphics.h header file to draw horizontal and vertical axis and bars on screen.

void line(int x1, int y1, int x2, int y2);

It draws a line from (x1, y1) to (x2, y2).

void setfillstyle(int pattern, int color);

It sets the current fill pattern and fill color.

void bar(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);

bar function draws a rectangle and fill it with current fill pattern and color.

Function Argument Description
xTopLeft X coordinate of top left corner.
yTopLeft Y coordinate of top left corner.
xBottomRight X coordinate of bottom right corner.
yBottomRight Y coordinate of bottom right corner.

C program to draw bar graph using graphics

C Program to Draw Bar Graph Using C Graphics

#include <graphics.h>
#include <conio.h>
  
int main() {
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "X:\\TC\\BGI");
 
   settextstyle(BOLD_FONT,HORIZ_DIR,2);
   outtextxy(275,0,"BAR GRAPH");
 
   setlinestyle(SOLID_LINE,0,2);
   /* Draw X and Y Axis */
   line(90,410,90,50);
   line(90,410,590,410);
   line(85,60,90,50);
   line(95,60,90,50);
   line(585,405,590,410);
   line(585,415,590,410);
 
   outtextxy(65,60,"Y");
   outtextxy(570,420,"X");
   outtextxy(70,415,"O");
   /* Draw bars on screen */
   setfillstyle(XHATCH_FILL, RED);
   bar(150,80,200,410);
   bar(225,100,275,410);
   bar(300,120,350,410);
   bar(375,170,425,410);
   bar(450,135,500,410);
 
   getch();
   closegraph();
   return 0;
}

Program Output

BARGRAPH

frexp C Library Function

frexp C Library Function

The function double frexp(double x, int *exponent); breaks the floating point number x into its mantisa (an absolute floating point number between 0.5(included) and 1.0(excluded)) and an integer exponent of 2. It decompose x such that:
x = mantisa * 2exponent

Function prototype of frexp

double frexp(double x, int *exponent);
  • x : A floating point value to be decomposed.
  • exponent : A pointer to an integer where the value of the exponent to be stored.

Return value of frexp

It returns the value of mantissa and also stores the exponent of 2 in the integer pointer passed to it as argument.
If x is zero, both mantisa and exponent are zero.
If x is negative, the mantisa returned by this function is negative.

C program using frexp function

The following program shows the use of frexp function to decompose a floating point number.

frexp C Library Function

#include <stdio.h>
#include <math.h>
 
int main ()
{
  double value, fraction;
  int exponent;
 
  printf("Enter a number\n");
  scanf("%lf", &value);
   
  fraction = frexp(value , &exponent);
  printf("%lf = %lf * 2^%d\n", value, fraction, exponent);
   
  return 0;
}

Output

Enter a number
512
512.000000 = 0.500000 * 2^10

idexp C Library Function

idexp C Library Function

The function double ldexp(double x, int exponent); returns the result of multiplying x with 2 raised to the power of exponent( x * 2exponent).

Function prototype of idexp

double ldexp(double x, int exponent);
  • x : A floating point value representing the mantisa.
  • exponent : An integer representing the value of exponent of 2.

Return value of idexp

It returns the value of x * 2exponent.

C program using idexp function

The following program shows the use of idexp function.

idexp C Library Function

#include <stdio.h>
#include <math.h>
 
int main ()
{
  double x, result;
  int exponent;
 
  printf("Enter a number and exponent\n");
  scanf("%lf %d", &x, &exponent);
   
  /* It returns x*(2^exponent)  */
  result = ldexp(x, exponent);
  printf("%lf * 2^%d = %lf\n", x, exponent, result);
   
  return 0;
}

Output

Enter a number and exponent
3.5 4
3.500000 * 2^4 = 56.000000

Array of Structure in C Programming

  • In this tutorial, we will learn about array of structure variables in c programming language. Declaration of structure array and accessing structure array element using subscript.

A structure in C programming language is used to store set of parameters about an object/entity. We sometime want to store multiple such structure variables for hundreds or objects then Array of Structure is used.

Both structure variables and in-built data type gets same treatment in C programming language. C language allows us to create an array of structure variable like we create array of integers or floating point value. The syntax of declaring an array of structure, accessing individual array elements and array indexing is same as any in-built data type array.

Declaration of Structure Array in C

struct Employee
{
    char name[50];
    int age;
    float salary;
}employees[1000];
or
struct Employee employees[1000];

In above declaration, we are declaring an array of 1000 employees where each employee structure contains name, age and salary members. Array employees[0] stores the information of 1st employee, employees[1] stores the information of 2nd employee and so on.

Accessing Structure Fields in Array
We can access individual members of a structure variable as

array_name[index].member_name
For Example
employees[5].age

C Program to Show the Use of Array of Structures

In below program, we are declaring a structure “employee” to store details of an employee like name, age and salary. Then we declare an array of structure employee named “employees” of size 10, to store details of multiple employees.

Array of Structure in C Programming

#include <stdio.h>
#include <conio.h>
 
struct employee {
    char name[100];
 int age;
 float salary;
};
 
int main(){
   struct employee employees[10];
   int counter, index, count, totalSalary;
    
   printf("Enter Number of Employees\n");
   scanf("%d", &count);
    
   /* Storing employee detaisl in structure array */
   for(counter=0; counter<count; counter++){ 
       printf("Enter Name, Age and Salary of Employee\n");
       scanf("%s %d %f", &employees[counter].name, 
           &employees[counter].age, &employees[counter].salary);
   }
    
   /* Calculating average salary of an employee */
   for(totalSalary=0, index=0; index<count; index++){
       totalSalary += employees[index].salary;
   }
    
   printf("Average Salary of an Employee is %f\n", 
       (float)totalSalary/count);
 
   getch();
   return 0;
}

Output

Enter Number of Employees
3
Enter Name, Age and Salary of Employee
Jack 30 100
Enter Name, Age and Salary of Employee
Mike 32 200
Enter Name, Age and Salary of Employee
Nick 40 300
Average Salary of an Employee is 200.000000

isprint C Library Function

isprint C Library Function

isprint function checks whether a character is printable character or not. A printable character is a character that occupies a printing position on a display we may also define it as a character that is not a control character.

Function prototype of isprint

int isprint(int c);

Function isprint() takes a character as input in form of an integer. When we pass a value of type char to isprint function, corresponding ASCII value of that character is passed.

Return value of isprint

If passed character is a printable character, then isprint function returns non-zero(true) integer otherwise 0(false).

C program using isprint function

The following program is to check whether a character is printable character or not.

isprint C Library Function

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
 
int main(){
    char string[] = "Aa. \t1\n";
    int index = 0;
     
    while(string[index] != '\0'){
        if(isprint(string[index])){
            printf("'%c' is a printable character\n", string[index]);
        } else {
            printf("'%c' is not a printable character\n", string[index]);
        }
        index++;
    }
     
    getch();
    return 0;
}

Output

'A' is a printable character
'a' is a printable character
'.' is a printable character
' ' is a printable character
'        ' is not a printable character
'1' is a printable character
'
' is not a printable character

atan2 C Library Function

atan2 C Library Function

The function double atan2(double y, double x); returns the arc tangent of y/x, expressed in radians. Function atan2 takes into account the sign of both arguments in order to determine the quadrant.

Function prototype of atan2

double atan2(double y, double x);

  • y : A floating point value representing an Y-coordinate.
  • x : A floating point value representing an X-coordinate.

Return value of atan2

Function atan2 returns the principal arc tangent of y/x, in the interval [-Pi, +Pi] radians.

C program using atan2 function

The following program shows the use of atan2 function to calculate inverse tangent of y/x.

atan2 C Library Function

#include <stdio.h>
#include <math.h>
 
#define PI 3.14159
 
int main(){
    double Y, X, radian, degree;
    printf("Enter value of Y and X\n");
    scanf("%lf %lf", &Y, &X);
     
    radian = atan2(Y, X);
    /* 
     *  Radian to degree conversion
     *  One radian is equal to 180/PI degrees.
     */
    degree = radian * (180.0/PI);
     
    printf("The arc tan2 of %0.4lf and %0.4lf is %0.4lf radian\n",
        Y, X, radian);
    printf("The arc tan2 of %0.4lf and %0.4lf is %0.4lf degree\n",
        Y, X, degree);
         
    return 0;
}

Output

Enter value of Y and X
5 5
The arc tan2 of 5.0000 and 5.0000 is 0.7854 in radian
The arc tan2 of 5.0000 and 5.0000 is 45.0000 in degree

isupper C Library Function

isupper() function checks whether a character is uppercase alphabet(A-Z) or not.

Function prototype of isupper

int isupper(int c);

Function isupper() takes a character as input in form of an integer. When we pass a value of type char to isupper() function, corresponding ASCII value of that character is passed.

Return value of isupper

If passed character is a uppercase character, then isupper function returns non-zero(true) integer otherwise 0(false).

C program using isupper function

The following program is to check whether a character is uppercase character or not.

isupper C Library Function

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
 
int main(){
    char string[] = "Aa.1";
    int index = 0;
     
    while(string[index] != '\0'){
        if(isupper(string[index])){
            printf("'%c' is a uppercase character\n", string[index]);
        } else {
            printf("'%c' is not a uppercase character\n", string[index]);
        }
        index++;
    }
     
    getch();
    return 0;
}

Output

'A' is a uppercase character
'a' is not a uppercase character
'.' is not a uppercase character
'1' is not a uppercase character

C Program to Draw 3D Bar Graph Using C Graphics

Write a program in C to draw 3D bar chart on screen using graphics.h header file

In this program, we will draw a 3D bar graph on screen. Here, we will use linesetfillstyle and bar3d functions of graphics.h header file to draw horizontal and vertical axis and bars on screen.

void line(int x1, int y1, int x2, int y2);

It draws a line from (x1, y1) to (x2, y2).

void setfillstyle(int pattern, int color);

It sets the current fill pattern and fill color.

void bar3d(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight, int depth, int topFlag);

bar3d function draws a 3D cuboid and fill front facing surface with current fill pattern and color.

Function Argument Description
xTopLeft X coordinate of top left corner.
yTopLeft Y coordinate of top left corner.
xBottomRight X coordinate of bottom right corner.
yBottomRight Y coordinate of bottom right corner.
depth It specifies the depth of bar in pixels.
topFlag It specifies whether a 3D top to put on the bar or not(any non-zero value specifies a 3d top other wise no 3d top).

C program to draw 3D bar graph using graphics

C Program to Draw 3D Bar Graph Using C Graphics

#include <graphics.h>
#include <conio.h>
  
int main() {
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   settextstyle(BOLD_FONT,HORIZ_DIR,2);
   outtextxy(275,0,"3D BAR GRAPH");
 
   setlinestyle(SOLID_LINE,0,2);
   /* Print X and Y Axis */
   line(90,410,90,50);
   line(90,410,590,410);
   line(85,60,90,50);
   line(95,60,90,50);
   line(585,405,590,410);
   line(585,415,590,410);
 
   outtextxy(65,60,"Y");
   outtextxy(570,420,"X");
   outtextxy(70,415,"O");
 
   /* Print 3D bars */
   setfillstyle(XHATCH_FILL, RED);
   bar3d(150,80,200,410, 15, 1);
   bar3d(225,100,275,410, 15, 1);
   bar3d(300,120,350,410, 15, 1);
   bar3d(375,170,425,410, 15, 1);
   bar3d(450,135,500,410, 15, 1);
 
   getch();
   closegraph();
   return 0;
}

Program Output

3DBarGraph