Fundamentals 8 min read

Master Variable Initialization in C: Numbers, Strings, Pointers, and Structs

This guide explains how to correctly initialize numeric, character, string, pointer, and struct variables in C, covering direct assignment, the use of the memset function, common pitfalls, and best‑practice code examples for each data type.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Variable Initialization in C: Numbers, Strings, Pointers, and Structs

When writing C code, initializing variables prevents undefined values caused by the compiler. Different data types require different initialization techniques.

Numeric Variable Initialization

Integral and floating‑point variables are typically initialized to 0 at definition:

int   inum = 0;
float fnum = 0.00f;
double dnum = 0.00;

Character Variable Initialization

Character variables are usually set to the null character '\0':

char ch = '\0';

String Initialization

Since a string is a character array, its elements must all be set to '\0'. Three common methods are demonstrated:

Assign an empty string literal: char str[10] = ""; Use memset to fill the array with zeroes:

char str[10];
memset(str, 0, sizeof(str));

Write a loop that assigns '\0' to each element:

char str[10];
for (int i = 0; i < 10; ++i) {
    str[i] = '\0';
}

The second method with memset is recommended for its simplicity and efficiency.

Understanding memset

memset

fills memory byte‑by‑byte. For an int (4 bytes), setting each byte to 1 yields the decimal value 16843009 (binary 00000001 00000001 00000001 00000001), not 1. This demonstrates that memset is a byte‑level filler, not a true initializer for multi‑byte types.

Pointer Initialization

Pointers are typically initialized to NULL: int *pnum = NULL; After allocating memory with malloc or calloc, the pointer should be checked and later set back to NULL after free to avoid dangling references:

char *p = malloc(100);
if (p == NULL) {
    printf("Not Enough Memory!
");
} else {
    printf("Memory Allocated at: %p
", p);
}
free(p);
p = NULL; // avoid wild pointer

Using memset directly on a pointer variable is incorrect because sizeof(pointer) returns the size of the address (typically 4 or 8 bytes), not the size of the pointed‑to memory.

Struct Initialization

Structs can be zero‑filled with memset:

typedef struct {
    int  id;
    char name[20];
    char sex;
} student;

student stu1;
memset(&stu1, 0, sizeof(stu1));

When initializing an array of structs, sizeof(array) gives the total byte length, ensuring all elements are cleared:

student stus[10];
memset(stus, 0, sizeof(stus)); // correct

Using sizeof(student) with the address of the array only clears the first element, which is a common mistake.

Key Takeaways

Initialize numeric types to 0 and characters to '\0' at definition.

Use memset for bulk zero‑initialization of arrays, strings, and structs.

Remember that memset works byte‑wise; setting non‑zero values may produce unexpected results for multi‑byte types.

Always set pointers to NULL after freeing memory to prevent undefined behavior.

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.

CmemsetstructsVariable Initialization
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.