Commit d53cf0e2 authored by Breno Rilho Lemos's avatar Breno Rilho Lemos 💬

Unions Introduction

parent 732e5b9d
#include <stdio.h>
#include <string.h>
union Data { // A UNION has all its members share the same memory location.
int i;
float f;
char str[20];
int main( ) {
Data data; // In C++, there is no need for the word union preceding a new union variable.
// There is also no need for the word structure preceding a new structure object.
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i); // This does NOT output 10
printf( "data.f : %f\n", data.f); // This does NOT output 220.5
printf( "data.str : %s\n", data.str); // This outputs "C Programming"
return 0;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment