How to use typedef to declaring union in C

2 Answers

0 votes
#include <stdio.h>

typedef union Complex
{
    int n;
    float f;
    char ch;
} Complex;


int main(void)
{
    Complex com;
    
    com.n = 100;
    printf("com.n = %d\n", com.n); 
 
    com.ch = 'a';
    printf("com.ch = %c\n", com.ch);
    
    return 0;
}
    
/*
run:
 
com.n = 100
com.ch = a
  
*/

 



answered Aug 18, 2017 by avibootz
0 votes
#include <stdio.h>

typedef union
{
    int n;
    float f;
    char ch;
} Complex;


int main(void)
{
    Complex com;
    
    com.n = 50;
    printf("com.n = %d\n", com.n); 
 
    com.ch = 'z';
    printf("com.ch = %c\n", com.ch);
    
    com.f = 3.14;
    printf("com.f = %.2f\n", com.f);
    
    return 0;
}
    
/*
run:
 
com.n = 50
com.ch = z
com.f = 3.14
  
*/

 



answered Aug 18, 2017 by avibootz

Related questions

2 answers 156 views
156 views asked Aug 18, 2017 by avibootz
1 answer 115 views
115 views asked Dec 29, 2021 by avibootz
1 answer 103 views
103 views asked May 7, 2021 by avibootz
3 answers 300 views
1 answer 170 views
1 answer 135 views
1 answer 152 views
152 views asked Dec 8, 2015 by avibootz
...