.) or arrow (→) if you have a pointer.
sizeof(struct) may include padding bytes - compiler aligns fields for efficiency.
struct Point { int x; int y; }; struct Point p = {10, 20}; printf("%d %d", p.x, p.y);
typedef for convenience:
typedef struct { char name[20]; int age; } Person;
union Number { int i; float f; char c; }; union Number n; n.f = 3.14; printf("%f", n.f);
enum Color { RED, GREEN = 5, BLUE }; enum Color c = BLUE; printf("%d\n", c); // prints 6
struct Status { unsigned int power : 1; unsigned int error : 1; unsigned int mode : 2; // 0–3 };
-E to see the preprocessor phase output or -S for assembly
#define macros
#ifdef, #ifndef, #endif for conditional compilation
#include for file inclusion
#define SQUARE(x) ((x)*(x)) // OK #define SQUARE(x) (x*x) // WRONG
#define PI 3.14159 #define SQUARE(x) ((x)*(x)) #define DEBUG #ifdef DEBUG printf("Debug mode on\n"); #endif
Create a program that defines:
enum Department { IT, HR, SALES }; struct Employee { char name[30]; int age; enum Department dept; };
const char* dept_to_str(enum Department d) to return department names.
Define a union for storing a 32-bit register as either:
unsigned int raw; value, or
struct { unsigned int enable : 1; unsigned int mode : 2; unsigned int error : 1; unsigned int reserved : 28; };
Your program will:
0x…)
Use a macro for debug control:
#define DEBUG 1
#ifdef DEBUG
#ifdef DEBUG printf("[DEBUG] Value = %d\n", x); #endif