Fundamentals 18 min read

Master C Language Data Types, Structures, and Pointers: A Complete Beginner’s Guide

Explore the essential C programming concepts—from basic data types, constants, and variables to complex structures, unions, enums, and pointers—through clear explanations, practical examples, and code snippets, helping beginners understand memory allocation, type definitions, and safe coding practices.

AI Cyberspace
AI Cyberspace
AI Cyberspace
Master C Language Data Types, Structures, and Pointers: A Complete Beginner’s Guide

Preface

Cherry Goh , an intelligent‑hardware developer with years of C programming experience, authored this first article of the "C Language Introduction" series, originally published on Zhihu.

Python is often called a "glue language" and can be combined with C for high‑performance solutions in finance, big data, and AI. If you already know Python, it is worthwhile to learn C and how the two languages interoperate.

C Language Data Types

Data types are the foundation of any programming language. Historically they existed to let programs use scarce memory efficiently; today they also define the size, range, and allowed operations of data objects.

Determine the memory size occupied by a data object

Determine the numeric range of a data object

Specify the operations that can be performed on a data object

Example: A basic integer occupies 4 bytes (32 bits) and can hold values from 0x0 to 0xFFFFFFFF, supporting modulo operations.

Note: The actual size and range may also depend on the compiler, not only on the C standard.

The four major C data type categories:

Basic data types

Derived data types

Pointer types

Void type

Constants and Variables

In C/C++, data can appear as constants (values that cannot change during program execution) or variables (values that can change).

Constants : integer constants, floating‑point constants, character constants, string literals, symbolic constants (defined with const or #define).

Variables : have a name, a type, and a stored value. The three essential elements are the variable name, its data type, and its current value.

Basic Data Types

The basic data types are illustrated in the following diagram:

Example: Declare and initialize a signed integer variable: int aInt = 1; // signed integer Example: Declare and initialize an unsigned integer variable: unsigned int uaInt = 1; // unsigned integer Difference between signed and unsigned integers:

Both occupy the same memory size but have different numeric ranges.

Signed/unsigned modifiers apply only to integer or character types.

Example: unsigned char ranges from 0 to 255, while signed char ranges from –128 to 127 because the most‑significant bit is used as a sign bit.

Derived Data Types – Arrays

Definition: An ordered collection of variables of the same type.

Characteristics:

Orderliness: Elements have a fixed sequence.

Indexability: Elements can be accessed via the array name and an index.

Syntax for a one‑dimensional array: data_type array_name[constant_expression]; Example: int aArray[5] = {1, 2, 3, 4, 5}; Note: The size must be a constant expression; arrays cannot be resized dynamically in C.

Boundary checking: C does not perform automatic bounds checking, so programmers must manually verify indices to avoid undefined behavior.

if (idx >= sizeof(aArray)/sizeof(int)) {
    printf("Err: idx out of range! Max idx = %d, idx = %d
", sizeof(aArray)/sizeof(int), idx);
}

Structures

Definition: A collection of variables (members) that may have different types.

Syntax for defining a struct type:

struct StructName {
    data_type member1;
    data_type member2;
    // ...
};

Example:

struct student {
    int num;
    char name[20];
    float score;
};

Three ways to create struct variables:

Direct creation: Define the type and variables in one statement.

struct student { int num; char name[20]; float score; } zhao, wang, li;

Indirect creation: Define the type first, then declare variables later.

struct student; // forward declaration
struct student zhao, wang, li;

Anonymous creation: Omit the struct tag when defining variables.

struct { int num; char name[20]; float score; } zhao, wang, li;

Accessing members: Use the dot operator ( .)

zhang.name

Unions

Definition: A union (also called a common block) allows several members of different types to share the same memory location. Syntax for defining a union type:

union UnionName {
    type1 member1;
    type2 member2;
    // ...
};

Example:

union SizeUnion {
    char a8;
    int  b32;
    long int c64;
};
SizeUnion TransDatas;

Accessing union members uses the same dot operator ( . ) for a variable and the arrow operator ( -> ) for a pointer.

TransDatas.c32 = 0x90987654;
printf("c32 = 0x%x
", TransDatas.c32);
TransDatas.b16 = 0x1111;
printf("b16 = 0x%x
", TransDatas.b16);
TransDatas.a8 = 0x22;
printf("a8 = 0x%x
", TransDatas.a8);

Note: Only one member of a union holds a valid value at any time; assigning to another member overwrites the previous value. Unions cannot be passed by value to functions, but pointers to unions can.

Enumerations

Definition: An enumeration is a set of named integer constants. Syntax for defining an enum type:

enum EnumName {
    ENUM_VAL1 = 1,
    ENUM_VAL2 = 2,
    ENUM_VAL3, // gets value 3 automatically
    // ...
};

Example:

enum StatusEnum { A_Status = 1, B_Status = 2, C_Status, D_Status, E_Status };
int main(void) {
    enum StatusEnum status = A_Status;
    printf("Status = %d, A_Status = %d
", status, A_Status);
    status = B_Status;
    printf("Status = %d, B_Status = %d
", status, B_Status);
    // ...
    return 0;
}

Note: If no explicit value is given, the compiler assigns successive integers starting from 0. Enum constants are immutable and must have unique names.

Pointers

Definition: A pointer variable stores the memory address of another variable.

Pointer variable: Holds an address.

Direct access: Use the variable name.

Indirect access: Use the pointer (dereference) operator *.

Operators:

Address‑of operator & : Retrieves the address of a variable.

Dereference operator * : Retrieves the value stored at the address pointed to.

Example 1 – Basic pointer usage:

int a = 2;          // variable a
int *p = &a;        // p holds the address of a
printf("a's address = %p
", (void *)&a);
printf("a = %d
", a);
printf("p's address = %p
", (void *)&p);
printf("p = %p
", (void *)p);
printf("*p = %d
", *p);

Note: The address‑of and dereference operators are inverses of each other.

Void Type

Definition: Represents an unknown or empty type; it cannot be used to declare a real variable. Uses:

Specifying a function that returns no value ( void function(void)).

Specifying a function that takes no parameters ( void function(void)).

Void pointer ( void * ) can point to any data type.

By 吳秀華 – IoT & Smart‑Hardware Explorer

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TutorialC programmingpointersenumerationsstructuresunions
AI Cyberspace
Written by

AI Cyberspace

AI, big data, cloud computing, and networking.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.