icp/oer/courses/c-basics/sections/12-struct_enum/05-unions/program.c

59 lines
1.1 KiB
C

#include <stdio.h>
typedef union vec3
{
struct{float x,y,z;};
struct{float r,g,b;};
float E[3];
}vec3;
typedef enum type
{
TYPE_INT,
TYPE_FLOAT,
}type;
// TODO: fill in the struct
typedef struct box
{
}box;
// TODO: implement this function, such that it prints the numbers in the right format.
// this function should just print the number and a newline (\n)
void PrintBox(box Box)
{
// You can use a switch here.
}
int main(int argc, int argv)
{
vec3 Vector = {1.0f,2.0f,3.0f};
// You can use r,g and b
printf("Color: %2.2f, %2.2f, %2.2f\n", Vector.r, Vector.g, Vector.b);
printf("Before the loop:\n");
// You can use x,y and z
printf("Position: %2.2f, %2.2f, %2.2f\n", Vector.x, Vector.y, Vector.z);
// You can access the elements via array
for (int i = 0; i < 3; i++)
{
Vector.E[i] = 10.0f;
}
printf("After the loop:\n");
printf("Position: %2.2f, %2.2f, %2.2f\n", Vector.x, Vector.y, Vector.z);
box BoxInt;
box BoxFloat;
// TODO: fill this variables both with the appropriate type and with 15 as a value
PrintBox(BoxInt);
PrintBox(BoxFloat);
return 0;
}