Programs

C Programs
Program 1:
/* Program to demonstrate accessing address of variables  */
#include <stdio.h>
#include <conio.h>
void main()
{
    char a;
    int x;
    float p, q;

    clrscr();
    a = 'A';
    x = 125;
    p = 10.25;
    q = 18.76;

    printf("\n%c is stored at address %u.\n", a, &a);
    printf("\n%d is stored at address %u.\n", x, &x);
    printf("\n%f is stored at address %u.\n", p, &p);
    printf("\n%f is stored at address %u.\n", q, &q);

    getch();
}

 Program 2:
*Program to illustrate argc and argv */
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
    clrscr();
    if(argc != 2)
    {
    printf("You have forgotten to type your name.\n");
    exit(1);
    }
    printf("Hello %s", argv[1]);

    return 0;
}

Program  3:

/*Program to illustrate the array of strucures */
#include<stdio.h>
#include<conio.h>
void main()
{
    struct addr
    {
        char name[30];
        char street[40];
        char city[20];
        char state[3];
        unsigned long int zip;
    }addr_list[3];
    int i;
    clrscr();
    /* Input the values */
    for(i=0; i<3; i++)
    {
        printf("\nEnter the %dth addres information\n",i);
        printf("\nEnter the name of the person:\t");
        scanf("%s", addr_list[i].name);
        printf("\nEnter the street:\t");
        scanf("%s", addr_list[i].street);
        printf("\nEnter the city:\t");
        scanf("%s", addr_list[i].city);
        printf("\nEnter the state:\t");
        scanf("%s", addr_list[i].state);
        printf("\nEnter the zip:\t");
        scanf("%lu", addr_list[i].zip);
    }
        /* Output the values */
    clrscr();
    for(i=0; i<3; i++)
    {
        printf("\n\t\t\tADDRESS INFORMATION %d PERSON\n",i);
        printf("\nName:\t\t\t%s",addr_list[i].name);
        printf("\nStreet:\t\t\t%s",addr_list[i].street);
        printf("\nCity:\t\t\t%s",addr_list[i].city);
        printf("\nState:\t\t\t%s",addr_list[i].state);
        printf("\nZip:\t\t\t%lu",addr_list[i].zip);
    }
    getch();
}

Program 4:
/* Program to illustrate the garbage collection in the automatic variables if they are initialized. */
#include"stdio.h"
#include"conio.h"
void main()
{
    auto int i,j;
    clrscr();
    printf("\nThe values in the variables i and j are:  %d   and   %d",i,j);
    getch();
}


Program 5:
/* Program to illustrate the life of the automatic variables */
#include"stdio.h"
#include"conio.h"
void main()
{
    int m = 1000;
    clrscr();
    function2();
    printf("\nThe value of the variable m in the main() function is = %d\n",m);
    getch();
}
function1()
{
    int m = 10;
    printf("\nThe value of the variable m in the function1() is = %d\n", m);
}
function2()
{
    int m = 100;
    function1();
    printf("\nThe value of the variable in the function2() is = %d\n", m);
}

Program 6:
 /*Program to illustrate the difference between auto and static variables */
#include<stdio.h>
#include<conio.h>
void increment();
void main()
{
    clrscr();
    increment();
    increment();
    increment();
    getch();
}
void increment()
{
    auto int i = 1;
    printf("%d\n", i);
    i = i + 1;
}

Program 7:
/*Program to compare two strings */
#include <stdio.h>
#include<conio.h>
void main()
{
    char first[100], second[100];
    int i;

    clrscr();

    /*Read the strings */

    printf("\nEnter the first string:\t");
    gets(first);
    printf("\nEnter the second string:\t");
    gets(second);

    /*Compare the strings */

    i = 0;
    while(first[i] == second[i] && first[i] != '\0' && second[i] != '\0')
        i = i + 1;

    if(first[i] == '\0' && second[i] == '\0')
        printf("\nStrings are equal!");
    else
        printf("\nStrings are not equal.");
    getch();
}


Program 7:
/* Program to concatenate two strings */
#include <stdio.h>
#include <conio.h>
void main()
{
    char first[100], second[100];
    int length, i;

    clrscr();

    /* Read the stings to concatenate */

    printf("Enter the first string to concatenate:\t");
    scanf("%s", first);
    printf("\nEnter the second string to concatenate:\t");
    scanf("%s", second);

    /* Calculate the length of the first string */

    length = 0;
    for(length = 0; first[length] != '\0'; length++);

    /* Append the second string at the end of the first string */

    for(i = 0; second[i] != '\0'; i++)
        first[length+i] = second[i];

    /* Append the null character at the end of the concatenated string */

    first[length+i] = '\0';

    /* Displat the conctenated string */
    printf("\nThe concatenated string is:\t%s",first);
    getch();
}


Program 8:
/* Program to calculate correlation coefficient */
/* correlation is the degree of inter relationship between two varibales */
/* Correlation coeffient(r) is the measure of correlation */
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
    char var1[30], var2[30];
    float num_obser;
    float x[100], y[100];
    float sx = 0, sy = 0,sxy = 0;
    float ssx = 0, ssy = 0;
    float sq_xsum, sq_ysum;
    float  r, dr, nr;

    int i;
    clrscr();
    /*Read the names of the variables and the number of observations */
    printf("\nEnter the name of the first variable:\t");
    gets(var1);
    printf("\nEnter the name of the second variable:\t");
    gets(var2);
    printf("\nEnter the number of observations:\t");
    scanf("%f", &num_obser);

    /* Read the x values of var1 and y values of var2 */
    for(i=1; i <= num_obser; i++)
    {
        printf("\nEnter %s_x[%d] and %s_y[%d] values:\t",var1,i,var2,i);
        scanf("%f %f", &x[i], &y[i]);
       /* Calculation of sum of x,y, sum of product of x and y,sum of squares of x, y */
        sx = sx + x[i];
        sy = sy + y[i];
        sxy = sxy + x[i] * y[i];
        ssx = ssx + pow(x[i],2);
        ssy = ssy + pow(y[i],2);
    }

    sq_xsum = pow(sx, 2);
    sq_ysum = pow(sy,2);
    clrscr();
    printf("\n\t\t\tCORRELATION CO-EFFICIENT\n\n");
    printf("\n%s\t\t",var1);
    for(i= 1 ; i<= num_obser; i++)
        printf("%5.2f\t",x[i]);
    printf("\n\n\n%s\t\t",var2);
    for(i=1; i<= num_obser; i++)
        printf("%5.2f\t",y[i]);
    nr = sxy - sx * sy / num_obser;
    dr = sqrt(ssx - sq_xsum/num_obser) * sqrt(ssy - sq_ysum/num_obser);
    r = nr/dr;
    printf("\n\nCorrelation co-efficient of the given data is:\t %5.2f",r);
    getch();
}



Program 9:
 /*Program to illustrate the difference between auto and static variables */
#include<stdio.h>
#include<conio.h>
int i;
void increment();
void decrement();
void main()
{
    clrscr();
    printf("\nThe initial value of i =  %d\n");
    increment();
    increment();
    decrement();
    decrement();
    getch();
}
void increment()
{
    i = i + 1;
    printf("\nOn incrementing i =  %d\n", i);
}
void decrement()
{
    i = i - 1;
    printf("\nOn decrementing i =  %d\n",i);
}












Program 10:
/* Program to illustrate the function with  argument and no return value */
#include<stdio.h>
#include<conio.h>
void main()
{
    float num1, num2;

    clrscr();
    printf("\nEnter the first number:  ");
    scanf("%f", &num1);
    printf("\nEnter the second number:  ");
    scanf("%f", &num2);
    mul(num1,num2);
    getch();
}
mul(x,y)
float x,y;   /*Parametr declaration */
{
    float result;
    result = x * y;
    printf("\nMultiplication of %5.2f and %5.2f is = %5.2f",x,y,result);
}



Program 11:
/* Program to illustrate the function with  argument and no return value */
#include<stdio.h>
#include<conio.h>
void main()
{
    float num1, num2;

    clrscr();
    printf("\nEnter the first number:  ");
    scanf("%f", &num1);
    printf("\nEnter the second number:  ");
    scanf("%f", &num2);
    mul(num1,num2);
    getch();
}
mul(x,y)
float x,y;   /*Parametr declaration */
{
    float result;
    result = x * y;
    printf("\nMultiplication of %5.2f and %5.2f is = %5.2f",x,y,result);
}



Program 12:
/* Program to illustrate the function with  argument and  return value */
#include<stdio.h>
#include<conio.h>
void main()
{
    float num1, num2, result;

    clrscr();
    printf("\nEnter the first number:  ");
    scanf("%f", &num1);
    printf("\nEnter the second number:  ");
    scanf("%f", &num2);
    result = mul(num1, num2);
    printf("\nMultiplication of %5.2f and %5.2f is = %5.2f",num1,num2,result);
    getch();
}
mul( x, y)    /*Parametr declaration */
float x,y;
{
    float multiply;
    multiply = x * y;
    return(multiply);
}



Program 13:
/* Program to demonstrate the functions with argument*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int number1, number2;

    clrscr();
    printf("\nEnter the first number to perform addition:\t");
    scanf("%d", &number1);
    printf("\nEnter the second number to be added:\t");
    scanf("%d", &number2);
    printf("\nThe resultant of addition is:\t%d",sum(number1, number2));
    getch();
}

sum(n1, n2)
int n1,n2;

{
    int result;

    result = n1 + n2;
    return (result);
}


Program 14:
/*Program to illustrate getchar function */
#include<stdio.h>
#include<conio.h>
void main()
{
    char character;
    clrscr();

    character = ' ';
    printf("\nEnter characters to read.");
    printf("\nPressing the enter key will make the program to stop reading the character.\n");
    while (character != '\n')
        character = getchar();
}



Program 15:
/*Program to illustrate getchar function */
#include<stdio.h>
#include<conio.h>
void main()
{
    char character;
    clrscr();

    character = ' ';
    printf("\nEnter characters to read.");
    printf("\nPressing the enter key will make the program to stop reading the character.\n");
    while (character != '\n')
        character = getchar();
}



Program 17:


/*Sample program to illustrate dynamic memory allocation */
#include <stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
    int num, avg, i,*p, sum = 0;
    clrscr();

    /* Read the number of students */
    printf("\nEnter the number of students:\t");
    scanf("%d", &num);

    /*Memory allocation */
    p = (int *)malloc(num * 2);
        /* num*2 is for allocating two bytes to store int data */
    if (p == NULL)
    {
        printf("\nMemory allocation unsuccessful");
        exit();
    }
    /* Read the marks of the students */
    printf("\nEnter %d students marks:\n",num);
    for(i=0; i < num; i++)
        scanf("%d",(p+i));
        /* increment the pointer to point next location*/
    /* Add the contents of memory locations */
    for(i=0; i < num; i++)
        sum = sum + *(p+i);
    /*Calculate the average */
    avg = sum / num;
    printf("\nAverage mark = %d",avg);
    getch();
}

Program 18:

/* Program to multiply two matrices */
#include<stdio.h>
#include<conio.h>

void main()
{

    int mat1[10][10];
    int mat2[10][10];
    int mat3[10][10];
    int mul;
    int mat1row, mat1col, mat2row, mat2col;
    int i,j,k;

    clrscr();

    /* Read the rows and columns of matrices */
    printf("\nEnter the number of rows in the first matrix:\t");
    scanf("%d", &mat1row);
    printf("\nEnter the nimber of columns in the first matrix:\t");
    scanf("%d", &mat1col);
    printf("\nEnter the number of rows in the second matrix:\t");
    scanf("%d", &mat2row);
    printf("\nEnter the number of columns of the second matrix:\t");
    scanf("%d", &mat2col);


    if (mat1col == mat2row)
    {
        /* Read the first matrix */
        clrscr();
        printf("\nReading the first matrix");
        printf("\n________________________\n");
        readmat(mat1, mat1row, mat1col);

        /* Read the second matrix */
        clrscr();
        printf("\nReading the second matrix");
        printf("\n_________________________\n");
        readmat(mat2, mat2row, mat2col);

        /* Multiply matrices */
        for(i = 0; i < mat1row; i++)
        {
            for(j = 0; j < mat2col; j++)
            {
                mat3[i][j] = 0;
                for(k = 0; k < mat2row; k++)
                    mat3[i][j] = mat3[i][j] + mat1[i][k] * mat2[k][j];
            }
        }

        clrscr();
        /* Print the matrices */
        printf("\nFirst matrix");
        printf("\n____________\n\n");
        printmat(mat1, mat1row, mat1col);

        /* Print the second matrix */
        printf("\nSecond matrix");
        printf("\n_____________\n\n");
        printmat(mat2, mat2row,mat2col);

            /* Print the second matrix */
        printf("\nResultant matrix\n");
        printf("________________\n\n");
        printmat(mat3, mat1row,mat2col);
        getch();

    }
    else
    {
        clrscr();
        printf("\nThe number of the columns of the first matrix is not equal to the number of rows of the second matrix.\n");
        printf("So multiplication of these two matrices cannot be performed. Sorry!");
        getch();
    }
}
readmat(matrix, m, n)
int matrix[10][10], m, n;
{
    int i,j,mat[10][10];

       /*    mat = &matrix[0][0];   */
    for(i=0; i < m; i++)
    {
        for (j = 0; j<n; j++)
        {
            printf("\nEnter the %dth roe %dth column element:\t",i,j);
            scanf("%d",&matrix[i][j]);
        }
    }
}
printmat(matrix, m, n)
int matrix[10][10], m, n;
{
    int i,j;
    for(i=0; i < m; i++)
    {
        for (j = 0; j<n; j++)
            printf("%d\t", matrix[i][j]);
        printf("\n");
    }
}
Program 19:

/* Program to multiply two matrices simple program */
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10][10], b[10][10], res[10][10];
    int i,j,k,r,n,c;

    clrscr();
    printf("\nEnter the number of rows and columns\n");
    scanf("%d %d %d", &r, &n, &c);
    printf("\nEnter the elements of matrix A\n");
    for(i=0; i <r;i++)
        for(j=0; j< n; j++)
            scanf("%d", &a[i][j]);
    printf("\nEnter the elements of matrix B\n");
    for(i=0; i <n;i++)
        for(j=0; j< c; j++)
            scanf("%d", &b[i][j]);
    for(i=0; i <r;i++)
        for(j=0; j< c; j++)
            for(k=0; k<n; k++)
                res[i][j] =res[i][j]+ a[i][k] * b[k][j];
    for(i=0; i < r;i++)
    {
        for(j=0; j < c; j++)
            printf("%d\t", res[i][j]);
        printf("\n");
    }

    getch();
}

Program 20:
/* Program to transpose a matrix */
#include <stdio.h>
#include<conio.h>
void main()
{

    int mat1[10][10], mat2[10][10];
    int i,j;
    int m,n;

    clrscr();
    /*Read the order of the matrix */
    printf("\nEnter the number of rows:\t");
    scanf("%d", &m);
    printf("\nEnter the number of columns:\t");
    scanf("%d", &n);

    /*Read the matrix */
    for(i=0; i < m; i++)
    {
        for(j=0; j <n; j++)
        {
            printf("\nEnter the %d row and %d column element:\t",i,j);
            scanf("%d", &mat1[i][j]);
        }
    }
    for(i=0; i < m; i++)
    {
        for(j=0; j <n; j++)
            mat2[i][j] = mat1[j][i];
        printf("\n");
    }
    /* Print the matrix */
    for(i=0; i < m; i++)
    {
        for(j=0; j <n; j++)
            printf("%d\t", mat2[i][j]);
        printf("\n");
    }

    getch();
}

Program 21

/* Program to convert the given number into words */
#include<stdio.h>
#include<conio.h>
void main()
{
    unsigned int number, temp;
    int num_dig=0,array[100],index;
    clrscr();
/* Get the number */
    printf("\nEnter the number\t");
    scanf("%d", &number);
    temp = number;
/* Caluculate the number of digits and save the digits individually in the array */
    do
    {
        array[num_dig] = temp%10;
        num_dig++;
        temp = temp/10;
    } while(temp != 0);
/* Print the equivalent words of the numbers */
       for(index = num_dig-1; index >= 0; index--)
       {
        switch (array[index])
        {
            case 1:
                printf("ONE ");
                break;
            case 2:
                printf("TWO ");
                break;
            case 3:
                printf("THREE ");
                break;
            case 4:
                printf("FOUR ");
                break;
            case 5:
                printf("FIVE ");
                break;
            case 6:
                printf("SIX ");
                break;
            case 7:
                printf("SEVEN ");
                break;
            case 8:
                printf("EIGHT ");
                break;
            case 9:
                printf("NINE ");
                break;
        }
       }
getch();

}
Program 22:

/*Program to check whther the given sting is a palindrome */
#include<stdio.h>
#include<conio.h>
void main()
{
    char string[100], flag= 'T';
    int i, j, length;

    clrscr();

    /* Read the string */
    printf("\nEnter the string to be checked for palindrome\t:");
    scanf("%s", string);

    /*Find the length of the srting */
    length = strlen(string);

    for(i = 0,j=length-1; i <= length/2-1; i++,j--)
    {
        if(string[i] != string[j])
        {
            flag = 'F';
            printf("\nThe given string is not a palindrome!");
            break;
        }
    }
    if (flag == 'T')
        printf("\n\nThe given string %s is a palindrome!",string);
    getch();
}

Program 23:

/* Program to illustrate pointers in one-dimensional array */
#include <stdio.h>
#include <conio.h>
void main()
{
    int *p, sum, i;
    static int x[5] = {5,9,6,3,7};

    clrscr();
     i = 0;
     p = x;
     sum = 0;
     printf("\nElement    Value     Address \n\n");
     while( i < 5)
     {
        printf("x[%d]        %d    %u\n",i, *p, p);
        sum = sum + *p;
        i++, p++;
     }
     printf("\nSum = %d\n", sum);
     printf("\n&x[0] = %u\n", &x[0]);
     printf("\np = %u\n",p);
    getch();
}

Program 25:
/*Program to illustrate passing pointer to the functions */
#include <stdio.h>
#include <conio.h>
void main()
{
    int x, y;

    clrscr();
    x = 100;
    y = 200;
    printf("\nThe values of x and y befor exchane are: %d  and  %d",x,y);
    exchange(&x, &y);
    printf("\nThe values of x and y after exchange are: %d and  %d",x,y);
    getch();
}

exchange(a,b)
int *a , *b;
{
    int t;
    t = *a;        /* Assign the value at address a to t */
    *a = *b;        /* Assign the value at b into a */
    *b = t;         /* Assign t to b */
}
Program 26:
/* Program to demonstrate the pointer in C */
#include<stdio.h>
#include<conio.h>
void main()
{
    int x,y;
    int *ptr;

    clrscr();

    x = 10;
    ptr = &x;
    y = *ptr;

    printf("\nValue of x is %d",x);
    printf("\n%d is stored at address %u",x, &x);
    printf("\n%d is stored at address %u",*&x, &x);
    printf("\n%d is stored at address %u",*ptr, ptr);
    printf("\n%d is stored at address %u",y, &*ptr);
    printf("\n%d is stored at address %u",y, &y);

    getch();
}
Program 27:
/*Program to illustrate the use of register variables */
#include<stdio.h>
#include<conio.h>
void main()
{
    register int i;

    clrscr();
    for(i = 0; i <= 10; i++)
        printf("\n%d",i);
    getch();
}
Program 28:

/*Program to calculate the regression co-efficient */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    char var1[100], var2[100];
    float x[100], y[100], num;
    float sx = 0, sy = 0, sxy = 0, ssx = 0;
    float mx, my, sq_xsum;
    float b,a,dr,nr;
    int i;

    clrscr();
    /*Read the names of the variables, number of observations and the corresponding x and y values */
    printf("\nEnter the name of the first variable:\t");
    gets(var1);
    printf("\nEnter the name of the second variable:\t");
    gets(var2);
    printf("\nEnter the number of observations:\t");
    scanf("%f",&num);
    for(i=1; i <= num; i++)
    {
        printf("Enter %s_x[%d] and %s_y[%d] values:\t",var1,i,var2,i);
        scanf("%f %f",&x[i], &y[i]);
    /* Calculate sum of x, sum of y, sum square of x, sum of square y and sum of product of x and y */
        sx = sx + x[i];
        sy = sy + y[i];
        ssx = ssx + pow(x[i],2);
        sxy = sxy + x[i] * y[i];
    }
    mx = sx / num;
    my = sy / num;
    sq_xsum = pow(sx,2);
    nr = sxy - sx * sy / num;
    dr = ssx - sq_xsum / num;
    b = nr / dr;
    a = my - b * mx;
    clrscr();
    printf("\n\t\t\tREGRESSION CO-EFFICIENT\n");
    printf("\n%s\t\t",var1);
    for(i=1; i <= num; i++)
        printf("%5.2f\t",x[i]);
    printf("\n\n%s\t\t",var2);
    for(i = 1; i <= num; i++)
        printf("%5.2f\t",y[i]);
    printf("\n\nThe first regression co-efficient b = %5.2f",b);
    printf("\nThe y intercept a = %5.2f",a);
    getch();
}

Program 29:
/*Program to illustrate the difference between auto and static variables */
#include<stdio.h>
#include<conio.h>
void increment();
void main()
{
    clrscr();
    increment();
    increment();
    increment();
    getch();
}
void increment()
{
    static int i = 1;
    printf("%d\n", i);
    i = i + 1;
}

Program 30:
/*Program to compare two strings */
#include <stdio.h>
#include<conio.h>
void main()
{
    char first[100], second[100];
    int i;

    clrscr();

    /*Read the strings */

    printf("\nEnter the first string:\t");
    scanf("%s", first);
    printf("\nEnter the second string:\t");
    scanf("%s", second);

    /*Compare the strings */

    i = 0;
    while(first[i] == second[i] && first[i] != '\0' && second[i] != '\0')
        i = i + 1;

    if(first[i] == '\0' && second[i] == '\0')
        printf("\nStrings are equal!");
    else
        printf("\nStrings are not equal.");
    getch();
}
Program 31:
/* Program to compare two strings */
#include<stdio.h>
#include<conio.h>
void main()
{
    char source[100], destination[100];
    int index;
    clrscr();

/*Read the string */

    printf("\nEnter a string:\t");
    scanf("%s", source);

/*Copy the read string into another string variable */

    for(index = 0; source[index] != '\0'; index++)
        destination[index] = source[index];

/* Append the null character in the string */
    destination[index] = '\0';

/* Display the copied string */
    printf("\nThe string copied is:\t%s",destination);
    getch();
}
Program 32:
/* Program to illustrate the use of structure pointer */
#include<stdio.h>
#include<conio.h>
struct my_time
    {
        int hours;
        int minutes;
        int seconds;
    };
void display(struct my_time *t);
void update(struct my_time *t);
void main()
{
    struct my_time systime;

    clrscr();
    systime.hours = 0;
    systime.minutes = 0;
    systime.seconds = 0;

    update(&systime);
    display(&systime);

    getch();
}
void update(struct my_time *t)
{
    t->seconds++;
    if(t->seconds ==60)
    {
        t->seconds = 0;
        t->minutes++;
    }
    if(t->minutes)
    {
        t->minutes = 0;
        t->hours++;
    }
    if(t->hours == 24)
        t->hours = 0;

}
void display(struct my_time *t)
{
    printf("%02d:",  t->hours);
    printf("%02d:",  t->minutes);
    printf("%02d:",  t->seconds);
}
Program 33:
/* Program to demostrate the use of structures */
#include<stdio.h>
#include<conio.h>
void main()
{
    struct addr
    {
        char name[30];
        char street[40];
        char city[20];
        char state[3];
        unsigned long int zip;
    };
    struct addr addr_info;
    clrscr();
    /* Input the values */
    printf("\nEnter the addres information\n");
    printf("\nEnter the name of the person:\t");
    scanf("%s", addr_info.name);
    printf("\nEnter the street:\t");
    scanf("%s", addr_info.street);
    printf("\nEnter the city:\t");
    scanf("%s", addr_info.city);
    printf("\nEnter the state:\t");
    scanf("%s", addr_info.state);
    printf("\nEnter the zip:\t");
    scanf("%lu", addr_info.zip);
    /* Output the values */
    clrscr();
    printf("\n\t\t\tADDRESS INFORMATION\n\n");
    printf("\nName:\t\t\t%s",addr_info.name);
    printf("\nStreet:\t\t\t%s",addr_info.street);
    printf("\nCity:\t\t\t%s",addr_info.city);
    printf("\nState:\t\t\t%s",addr_info.state);
    printf("\nZip:\t\t\t%lu",addr_info.zip);
    getch();
}

Program 34:

/* Program to illustrate union */
#include <stdio.h>
#include<conio.h>
void main()
{
    union u_type
    {
        int i;
        char ch;
    }uni_var;
    clrscr();
    uni_var.i = 15;
    printf("\nThe values accessd through integer and character members:\n");
    printf("%d  %d",uni_var.i,uni_var.ch);
    uni_var.i = 150;
    printf("\n\nLook out the diffence with the same statement:");
    printf("\n%d  %d",uni_var.i,uni_var.ch);
    printf("(requires two bytes to store)");
    uni_var.ch = 'A';
    printf("\n\nThe values accessed throgh integer and character members:\n");
    printf("%c  %c",uni_var.i,uni_var.ch);

    getch();
}






















No comments:

Post a Comment