Back to C Programming
2026-07-126 min read

User-defined Types C Keywords

Learn User-defined Types C Keywords step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on User-defined Types and C Keywords! This tutorial is designed to provide you with a deep understanding of these crucial concepts, helping you excel in exams, interviews, and real-world programming scenarios. Let's dive in!

Prerequisites

To fully grasp the material covered in this lesson, it is essential that you have a solid foundation in C programming basics, including variables, data types, operators, control structures, and functions. If you are not familiar with these topics, we recommend reviewing them before proceeding.

Core Concept

In C programming, user-defined types allow us to create custom data structures that better suit our specific needs. This is achieved through the use of Structures (structs) and Unions. Additionally, understanding C keywords is crucial for writing efficient and error-free code. In this section, we will explore both user-defined types and C keywords in detail.

Structures (Structs)

A structure is a user-defined data type that groups related variables together under a single name. To define a structure, we use the struct keyword followed by the structure tag and enclose its members within curly braces {}. Here's an example of a simple structure:

struct Student {
int roll_number;
char name[50];
float marks;
};

In this example, we have created a structure named Student, which has three members: roll_number, name, and marks.

To declare a variable of this structure type, we use the following syntax:

struct Student student1;

We can also initialize the structure variables during declaration:

struct Student {
int roll_number;
char name[50];
float marks;
};

struct Student student1 = {1, "John Doe", 85.5};

Unions

A union is a user-defined data type that allows multiple variables to share the same memory location. This means that only one member of a union can be accessed at any given time. To define a union, we use the union keyword followed by the union tag and enclose its members within curly braces {}. Here's an example of a simple union:

union Data {
int i;
float f;
char str[20];
};

In this example, we have created a union named Data, which has three members: i, f, and str.

To declare a variable of this union type, we use the following syntax:

union Data data1;

We can access the union members using the dot operator (.). However, since only one member can be accessed at any given time, it is essential to know which member contains the desired data.

C Keywords

C provides a rich set of keywords that help in organizing and structuring our code effectively. Some commonly used C keywords include:

  • auto: Declares a local variable whose type is automatically determined by the compiler.
  • break: Terminates the execution of the current loop.
  • case: Specifies a case within a switch statement.
  • char: Defines a character data type.
  • const: Declares a constant variable that cannot be modified.
  • continue: Skips the remaining statements in the current iteration of a loop and moves on to the next iteration.
  • default: Specifies the default action for a switch statement when no matching case is found.
  • do...while: Executes a loop at least once before checking the condition.
  • double: Defines a floating-point data type that can store larger decimal numbers with higher precision.
  • else: Specifies the code to be executed if the condition in an if or switch statement is false.
  • enum: Defines an enumerated data type, which consists of a set of named integer constants.
  • extern: Declares a variable that exists outside the current file.
  • float: Defines a floating-point data type that can store decimal numbers with single precision.
  • for: Defines a loop that repeats a specified number of times or until a certain condition is met.
  • goto: Transfers control to another labeled statement within the same function.
  • if...else if...else: Conditional statements used for making decisions based on conditions.
  • int: Defines an integer data type that can store whole numbers.
  • long: Defines a long integer data type, which can store larger integers.
  • register: Declares a variable that should be stored in the CPU's register for faster access.
  • return: Terminates the execution of a function and returns control to the calling function or program.
  • short: Defines a short integer data type, which can store smaller integers.
  • signed: Specifies that a variable can store both positive and negative values.
  • sizeof: Returns the size (in bytes) of a data type or variable.
  • static: Declares a local variable that retains its value between function calls or initializes it only once.
  • struct: Defines a structure data type, as discussed earlier in this lesson.
  • switch: Executes code based on the match of an expression with case labels.
  • typedef: Allows us to create new names for existing data types.
  • union: Defines a union data type, as discussed earlier in this lesson.
  • unsigned: Specifies that a variable can only store positive values or zero.
  • void: Represents the absence of any value or data type.
  • volatile: Declares a variable whose value may be modified by hardware or other means outside the control of the program.
  • while: Defines a loop that continues as long as a certain condition is true.

Worked Example

Let's create a simple program that demonstrates the use of structures and unions:

#include <stdio.h>

struct Student {
int roll_number;
char name[50];
float marks;
};

union Data {
int i;
float f;
char str[20];
};

int main() {
struct Student student1 = {1, "John Doe", 85.5};
union Data data1 = {8765432};

printf("Student Information:\n");
printf("Roll Number: %d\n", student1.roll_number);
printf("Name: %s\n", student1.name);
printf("Marks: %.2f\n", student1.marks);

printf("\nUnion Data:\n");
printf("Integer Value: %d\n", data1.i);
printf("Float Value: %.2f\n", (float)data1.i / 360360.0);
printf("String Value: %s\n", data1.str);

return 0;
}

Save this code in a file named user_defined_types.c, and compile it using the command gcc user_defined_types.c -o user_defined_types. Run the compiled program with the command ./user_defined_types. You should see output similar to the following:

Student Information:
Roll Number: 1
Name: John Doe
Marks: 85.50

Union Data:
Integer Value: 8765432
Float Value: 2423.11
String Value: 21345678

Common Mistakes

  • Forgetting to include the necessary header files (e.g., #include ) can lead to compile errors.
  • Failing to properly initialize structure or union variables can result in undefined behavior or runtime errors.
  • Using a member of an uninitialized structure or union can also lead to undefined behavior or runtime errors.
  • Mixing up the members of a union can cause unexpected results, as only one member should be accessed at any given time.
  • Incorrectly using sizeof with structures or unions can lead to incorrect memory allocation or size calculations.

Practice Questions

  1. Create a structure named Book that includes members for the book title, author, publisher, and publication year. Write a program that declares an array of five Book structures and prints the details of each book.
  2. Define a union named Measurement that can store either length in meters or weight in kilograms. Write a program that initializes a Measurement variable with a length value, converts it to weight (multiplying by 75), and then prints both the original and converted values.
  3. Create an enumerated data type named Status with three possible values: AVAILABLE, SOLD, and RENTED. Write a program that declares an array of ten Status variables and counts the number of each status in the array.

FAQ

Structures group related variables together under a single name, allowing multiple variables to occupy separate memory locations. Unions, on the other hand, allow multiple variables to share the same memory location.

How do I declare an array of structures or unions in C?

To declare an array of structures, you can use the following syntax: struct Student students[5];. For unions, it's similar: union Data data[3];.

Why should I use typedef in my code?

Using typedef allows you to create new names for existing data types, making your code more readable and easier to understand.

What is the purpose of the volatile keyword in C?

The volatile keyword tells the compiler that a variable's value may be modified by hardware or other means outside the control of the program, ensuring that the variable is always reloaded from its memory location instead of being optimized away.