C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Introduction
  • Missing Header Files
  • Omitting Return Statements
  • Uninitialized Variables
  • Buffer Overflows

Was this helpful?

  1. Debugging

Common C Mistakes

Introduction

C programming, though powerful and efficient, leaves room for common mistakes that can lead to bugs and security vulnerabilities. This document aims to highlight some prevalent pitfalls in C programming, providing examples and guiding developers on how to avoid them.

Missing Header Files

Forgetting to include necessary header files can result in undefined behavior. Consider the following example:

// Missing the necessary header file
#include <stdio.h>

int main() {
    // Code that uses functions from the stdio library
    printf("Hello, World!");
    return 0;
}

Solution: Include the necessary header file:

#include <stdio.h>

int main() {
    printf("Hello, World!");
    return 0;
}

Omitting Return Statements

Failure to include a return statement in a function that expects a return value can lead to unpredictable behavior. Example:

int add(int a, int b) {
    int result = a + b;
    // Missing return statement
}

int main() {
    int sum = add(3, 4);
    printf("Sum: %d\n", sum);
    return 0;
}

Solution: Add the missing return statement:

int add(int a, int b) {
    int result = a + b;
    return result;
}

int main() {
    int sum = add(3, 4);
    printf("Sum: %d\n", sum);
    return 0;
}

Uninitialized Variables

Using variables before initializing them can lead to unpredictable results. Example:

#include <stdio.h>

int main() {
    int x;
    printf("Value of x: %d\n", x);  // Using uninitialized variable
    return 0;
}

Solution: Initialize the variable before using it:

#include <stdio.h>

int main() {
    int x = 0;  // Initialize variable
    printf("Value of x: %d\n", x);
    return 0;
}

Buffer Overflows

Overrunning buffers is a common mistake that can result in memory corruption. Example:

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

int main() {
    char buffer[5];
    strcpy(buffer, "Overflow!");  // Buffer overflow
    printf("Buffer: %s\n", buffer);
    return 0;
}

Solution: Use safe functions to prevent buffer overflows:

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

int main() {
    char buffer[20];
    strncpy(buffer, "No Overflow!", sizeof(buffer));  
    // Use strncpy to avoid overflow
    printf("Buffer: %s\n", buffer);
    return 0;
}

This pattern continues for the remaining examples. Each mistake is followed by an example and a suggested solution. I'll continue with the rest upon your request.

PreviousUnderstanding the Call StackNextUnderstanding Compiler Errors

Was this helpful?