Skip to main content
C Programming Basics
CHAPTER 28 Beginner

Typecasting and Memory Manipulation

Updated: May 17, 2026
5 min read

# CHAPTER 28

Typecasting and Memory Manipulation

1. Introduction

Sometimes you need a float to behave like an int, or a structure pointer to behave like a byte array. Typecasting allows you to force the compiler to treat a variable as a different type. Furthermore, C provides functions to directly manipulate blocks of memory at high speed, bypassing loops entirely.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Perform Implicit and Explicit Typecasting.
  • Understand the dangers of downcasting.
  • Use memset() to initialize memory blocks.
  • Use memcpy() to copy memory at high speed.
  • Use memmove() to safely handle overlapping memory.

3. Implicit Typecasting (Automatic)

The C compiler automatically promotes smaller data types to larger ones to prevent data loss.
c
12
int a = 10;
float b = a; // Implicit cast from int to float. b is now 10.000000

However, converting a float to an int silently truncates the decimal.

c
12
float price = 9.99;
int dollars = price; // Implicit downcast. dollars is 9. (Warning often given).

4. Explicit Typecasting (Manual)

You can force a conversion by placing the target data type in parentheses before the variable.
c
1234
int sum = 17, count = 5;
// Without casting, 17/5 = 3 (Integer division)
// With casting, (float)17 / 5 = 3.4
float average = (float) sum / count; 

5. Memory Manipulation Functions

The <string.h> library contains functions that operate on raw memory (bytes) rather than specific data types. These are highly optimized by the compiler.

#### 1. memset() - Memory Set Fills a block of memory with a specific byte value. Excellent for initializing arrays or structs to 0.

Syntax: memset(pointer, value, sizeinbytes);

c
123456789101112
#include <stdio.h>
#include <string.h>

int main() {
    int arr[10];
    
    // Set all 10 integers (40 bytes) to 0
    memset(arr, 0, sizeof(arr)); 
    
    printf("%d\n", arr[5]); // Prints 0
    return 0;
}

#### 2. memcpy() - Memory Copy Copies a block of memory from a source to a destination. Much faster than using a for loop to copy an array.

Syntax: memcpy(destination, source, sizeinbytes);

c
12345
int source[5] = {1, 2, 3, 4, 5};
int dest[5];

// Copies 20 bytes from source to dest
memcpy(dest, source, sizeof(source)); 

#### 3. memmove() - Memory Move (Safe Copy) If the source and destination memory blocks *overlap*, memcpy can corrupt the data. memmove detects overlap and copies the data safely (usually by using a temporary buffer).

c
1234
char str[] = "abcdefgh";
// Move "abcd" forward by 2 spaces (overlapping occurs!)
memmove(str + 2, str, 4); 
// Result: "ababcdgh"

6. Casting Pointers (Advanced)

You can cast pointers to manipulate data byte-by-byte.
c
1234567891011121314
#include <stdio.h>

int main() {
    int num = 300; 
    // 300 in binary (4 bytes): 00000000 00000000 00000001 00101100

    // Cast the int pointer to a char pointer (1 byte at a time)
    char *ptr = (char*)&num; 

    // Prints the first byte (00101100 -> 44)
    printf("First byte: %d\n", *ptr); 

    return 0;
}

7. Common Mistakes

  • Using memset for values other than 0 or -1 (with ints): memset works byte-by-byte. If you do memset(arr, 1, sizeof(arr)); on an integer array, it sets *every byte* to 1 (00000001 00000001 00000001 00000001), which equals the integer 16843009, not 1.
  • Forgetting sizeof in memcpy: If you do memcpy(dest, src, 5) expecting to copy 5 integers, you only copied 5 bytes (1 integer and a quarter). It must be 5 * sizeof(int).

8. Exercises

  1. 1. Define a large struct and initialize all its members to 0 using a single memset call.
  1. 2. Prove that integer division truncates decimals by writing a program that divides two ints, first without casting, then with (float) casting.

9. MCQ Quiz with Answers

Question 1

What is the syntax for explicit typecasting?

Question 2

Which library contains memory manipulation functions like memcpy?

Question 3

What does memset do?

Question 4

What is the safest function to use when copying overlapping memory blocks?

Question 5

Why does float x = 5 / 2; result in 2.0 instead of 2.5?

Question 6

What happens if you cast a float to an int?

Q7. Can you cast a void* pointer to an int* pointer? a) Yes b) No Answer: a) Yes (This is exactly how malloc works!)
Question 8

If you use memset(arr, 1, sizeof(arr)) on an int array, what is the result?

Question 9

Is implicit typecasting from int to float safe?

Question 10

How does the CPU handle memory manipulation functions?

10. Interview Questions

  • Q: Explain the exact difference between memcpy and memmove.
  • Q: Why is casting void* essential in C?
  • Q: What is "Endianness" and how does casting an int* to a char* help determine if a machine is Little Endian or Big Endian?

11. Summary

Typecasting allows you to convert data types manually to prevent truncation during math or pointer assignment. Memory manipulation functions (memset, memcpy, memmove) operate directly on byte blocks, offering extreme performance over manual loops, provided you calculate the byte sizes (sizeof) correctly.

12. Next Chapter Recommendation

In Chapter 29: Multi-file Projects and Makefiles, we will leave the single main.c file behind and learn how to structure large software projects and automate the compilation process.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·