📊 Data Types

Primitive and User-Defined in C

🔹 1. What Are Data Types?

Data types define the kind of data a variable can hold. Think of it as labeling boxes: each box (variable) can only store certain kinds of items (data).

🔹 2. Two Main Categories of Data Types in C
Category Description
Primitive Data Types Basic built-in types (numbers, characters)
User-Defined Data Types Custom types created by the programmer

Article Algo

✨ Primitive Data Types

These are the basic types that come with C by default:

Data Type Size (Typical) Description Example
int 4 bytes Stores whole numbers (integers) int age = 20;
float 4 bytes Stores decimal numbers (single precision) float pi = 3.14;
double 8 bytes Stores decimal numbers (double precision) double price = 99.99;
char 1 byte Stores a single character char grade = 'A';
void 0 bytes Represents no value or empty return void function();

Article Algo

🔍 Example Using Primitive Types:

#include <stdio.h>
int main() {
int age = 25;
float temperature = 36.6;
char initial = 'J';
printf("Age: %d\n", age);
printf("Temperature: %.1f\n", temperature);
printf("Initial: %c\n", initial);
return 0;
}
Output:
Age: 25
Temperature: 36.6
Initial: J

Article Algo

🏗️ User-Defined Data Types

Sometimes, you need to create your own data types to organize data better. C allows this with:

  • struct (structure)
  • union
  • enum (enumeration)
  • typedef (aliasing types)

Article Algo

1. struct — Structure

Used to group different variables under one name.

struct Person {
char name[50];
int age;
float height;
};
Example:
#include <stdio.h>
struct Person { char name[50]; int age; float height; };
int main() { struct Person p1 = {"Alice", 30, 5.6};
printf("Name: %s\n", p1.name); printf("Age: %d\n", p1.age); printf("Height: %.1f\n", p1.height);
return 0; }
2. union

Like struct but all members share the same memory location.

3. enum — Enumeration

Defines a set of named integer constants.

enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
4. typedef

Creates a new name (alias) for an existing type.

typedef unsigned int uint; uint age = 25;

Article Algo

🔑 Summary Table

Data Type Category Purpose Example
Primitive Basic data types int, char, float
User-defined (struct) Group related variables struct Person { ... };
User-defined (enum) Named constants enum Color {Red, Green};
User-defined (union) Memory-efficient data structures union Data { ... };
User-defined (typedef) Aliasing types typedef int MyInt;

Article Algo

🚀 Why Does This Matter?

  • Correct data types help manage memory efficiently.
  • They allow you to write clearer and more organized code.
  • User-defined types help model real-world objects.

Frequently Asked Questions – Data Types in C

Q: What is a data type in C?

A: A data type defines the kind of data a variable can store, like integers, floats, characters, or user-defined structures.

Q: What are the two main categories of data types in C?

A: The two main categories are Primitive Data Types and User-Defined Data Types.

Q: What are primitive data types in C?

A: Primitive types are built-in data types like int, float, char, double, and void.

Q: What is the size of an int in C?

A: Typically, an int occupies 4 bytes in most modern systems.

Q: What is the use of the void data type?

A: void represents no value and is often used as a return type for functions that do not return anything.

Q: What is a user-defined data type?

A: User-defined data types are custom types created by the programmer using struct, union, enum, or typedef.

Q: What is a struct in C?

A: A struct is used to group variables of different data types under one name for better data organization.

Q: How is a union different from a struct?

A: In a union, all members share the same memory space, so only one member can be used at a time.

Q: What is an enum in C?

A: An enum defines a set of named integer constants for better code readability.

Q: What is typedef used for?

A: typedef creates an alias for an existing data type, making the code cleaner and easier to maintain.

Q: Can I create my own data types in C?

A: Yes, using struct, union, enum, and typedef, you can define custom data types tailored to your application.

Q: Why use user-defined data types?

A: They help model real-world entities, simplify complex data, and enhance code organization.

Q: What is the default value of an uninitialized variable?

A: C does not initialize variables by default; uninitialized variables contain garbage values.

Q: How do I print values of different data types in C?

A: Use format specifiers like %d for int, %f for float, %c for char, and %s for strings in printf.

Q: Can I nest structs in C?

A: Yes, you can include one struct as a member of another struct for hierarchical data modeling.

Q: What are the advantages of using typedef?

A: It improves readability, reduces complexity, and allows easier modification of type names.

Q: How is memory managed in a union?

A: Memory is allocated based on the largest member, and all members share that memory space.

Q: What are common use cases of enum?

A: enum is commonly used for defining states, days, directions, or modes to improve code clarity.

Q: Can typedef be used with struct?

A: Yes, typedef is often used with struct to simplify declarations. Example: typedef struct { int x; } Point;

Q: Why is understanding data types important?

A: It ensures correct data usage, efficient memory management, and helps avoid logic and runtime errors.

Article Algo