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 199 views
199 views asked Aug 18, 2017 by avibootz
1 answer 176 views
176 views asked Dec 29, 2021 by avibootz
1 answer 150 views
150 views asked May 7, 2021 by avibootz
3 answers 368 views
1 answer 223 views
1 answer 176 views
1 answer 181 views
181 views asked Dec 8, 2015 by avibootz
...