Computer Programming C Laboratory: Unions C Programs

Write a C program to pass union to a function

C Programs using Structures & Unions

Write a C program to pass union to a function.

Eg: Raise a number to a power.

Evaluate y=xn, where 'x' and 'y' are floating point values and 'n' can be either float or int. If 'n' is an integer, then 'y' can be evaluated by multiplying 'x' by itself an appropriate number of times.

/* Program to pass union to a function.*/

#include <stdio.h>

#define N 3

#define NULL 0

/* Global declarations */

typedef union

{

    float aa;

    int bb;

}nvals;

typedef struct

{

    float x;

    char flag;

    nyals exp;

} values;

void main()

{

/* Local definitions */

values a;

float power(values a);

int i;

float n,y;

/* Statements */

printf("Enter a value for X (ie., x power n)...........");

scanf("%f",&a.x);

printf("\nEnter a value for N(ie., x power n).............");

scanf("%f", &n);

i= (int)n;

a.flag=(i==n)?'i': 'f';

if(a.flag== 'i')

        a.exp.bb=i;

else

        a.exp.aa=n;

if(a.flag=='f'&&a,x<=0.0)

{

          printf("can't raise a nonpositive number to a");

          printf("\nfloating point power");

}

else

{

          y=power(a);

          printf("\ny = %.4f",y);

} /* if..else */

} /* main */

float power (values a)

{

          int i;

          float y = a.x;

          if(a.flag== 'i')

          {

                    if(a.exp.bb==0)

                    y=1.0;

                 else

                 {

                    for (i=1;i<abs(a.exp.bb); ++i)

                              y=a.x;

                    if (a.exp.bb<0)

                              y=1./y;

                 } /* if..else */

} /* if */

else

y=exp(a.exp.aa*log(a.x));

return(y);

} /* power() */

OUTPUT

Enter a value for X (ie., x power n).....5

Enter a value for N(ie., x power n).....2

y  = 25.0000

EXPLANATION: This program refers to passing union to a function. The union nvals and structure values are declared initially. The main function reads the variable n from the terminal and check the condition a. flag='i' if yes then initialise a.exp.bb=i else a.exp.aa=n. if statement is executed and prints the true block otherwise prints the else block of statement. In function power() if statement checks the conditions a.flag==i. If condition is true then true block is executed or else block is executed and returned a value to the main program.

 

Computer Programming C Laboratory: Unions C Programs : Tag: C Programming : - Write a C program to pass union to a function


Computer Programming C Laboratory: Unions C Programs



Under Subject


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation