Home

A program to pass a structure to a function as a parameter and return the structure from the function

C Programming Language 094

Program

#include <stdio.h>

struct complex
{
    int real, img;
};

void display(struct complex c1)
{
    printf("\n%d + i%d", c1.real, c1.img);
};

struct complex add(struct complex c1, struct complex c2)
{
    struct complex t;
    t.real = c1.real + c2.real;
    t.img = c1.img + c2.img;
    return t;
}

void main()
{
    struct complex c1, c2, c3;

    printf("Enter a,b for c1 [a + ib]: ");
    scanf("%d %d", &c1.real, &c1.img);

    printf("Enter a,b for c2 [a + ib]: ");
    scanf("%d %d", &c2.real, &c2.img);

    c3 = add(c1, c2);

    printf("Addition of two complex numbers...");
    display(c3);

    printf("\n");
}

Result

Enter a,b for c1 [a + ib]: 2 4
Enter a,b for c2 [a + ib]: 4 2
Addition of two complex numbers...
6 + i6


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments