Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Pass By Reference C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Arrays of Structs C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example C Memory Mistakes

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C Problem Solving

C Problem Solving C Build the Logic C Edge Cases & Errors C Test Cases

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers C Command-Line Args

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Hash Tables C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C Errors


Share

Errors

Even experienced C developers make mistakes. The key is learning how to spot and fix them!

These pages cover common errors and helpful debugging tips to help you understand what's going wrong and how to fix it.


Common Compile-Time Errors

Compile-time errors are mistakes that prevent your program from compiling.

1) Missing semicolon:

Example

#include <stdio.h>

int main() {
  int x = 5
  printf("%d", x);
  return 0;
}

Result:

prog.c: In function 'main':
prog.c:5:3: error: expected ',' or ';' before 'printf'
    5 |   printf("%d", x);
      |   ^~~~~~
Try it Yourself »

Tip: The error message also tells you where the error is: prog.c:5:3 means line 5, column 3, and the ^ marks the spot. Notice that the semicolon is missing on line 4, but the error is reported on line 5, where the compiler first noticed that something was missing. If you cannot find the error on the line the compiler shows, look at the line before it.

2) Using undeclared variables:

Example

#include <stdio.h>

int main() {
  printf("%d", myVar);
  return 0;
}

Result:

error: 'myVar' undeclared
Try it Yourself »

3) Mismatched types (e.g. assigning text to an int):

Example

#include <stdio.h>

int main() {
  int x = "Hello";
  return 0;
}

Result:

error: initialization makes integer from pointer without a cast
Try it Yourself »


Common Runtime Errors

Runtime errors occur when the program compiles but crashes or behaves unexpectedly.

1) Dividing by zero:

Dividing an integer by zero usually makes the program crash:

Example

#include <stdio.h>

int main() {
  int x = 10;
  int y = 0;
  int result = x / y;
  printf("%d\n", result); // not possible
  return 0;
}

2) Accessing out-of-bounds array elements:

C does not check if the index exists, so the example below may print a random number, or make the program crash:

Example

#include <stdio.h>

int main() {
  int numbers[3] = {1, 2, 3};
  printf("%d\n", numbers[8]); // element does not exist
  return 0;
}

3) Using freed memory:

The example below may print 10, a random number, or crash. This is called undefined behavior: C does not say what will happen, so you cannot trust the result.

Example

#include <stdio.h>
#include <stdlib.h>

int main() {
  int* ptr = malloc(sizeof(int));
  *ptr = 10;
  free(ptr);
  printf("%d\n", *ptr); // Undefined behavior - accessing memory that was freed
  return 0;
}

Good Habits to Avoid Errors

  • Always initialize your variables
  • Use meaningful variable names
  • Keep your code clean and use indentation to stay organized
  • Keep functions short and focused
  • Check if loops or conditions are running as expected
  • Read error messages carefully - they often tell you exactly where the problem is

In the next chapter, you will learn how to debug your code - how to find and fix bugs/errors in your C program.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.