Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Interesting Facts in C Programming | Set 2
Next article icon

C Program to Show Runtime Exceptions

Last Updated : 03 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Runtime exceptions occur while the program is running. Here, the compilation process will be successful.  These errors occur due to segmentation faults and when a number is divided by a division operator or modulo division operator.

Types of Runtime Errors:

  1. An invalid memory access 
  2. Dividing by zero. 
  3. Segmentation faults  
  4. Large memory allocation/Large Static Memory Allocation 
  5. Making common errors. 

1. During runtime, an invalid memory access  

An invalid memory access error occurs during runtime when the array’s index is assigned a negative value. 

Note: The output might change every time we run this program because it gives a junk value in return for the invalid location.

C




// C program to demonstrate
// an invalid memory access error
#include <stdio.h>
int a[5];
int main()
{
    int s = a[-11];
    printf("%d", s);
    return 0;
}
 
 
Output
32746

2. Dividing by zero

When we are trying to divide any number by zero then we get this type of error called floating-point errors.

C




// C program to demonstrate
// an error occurred by dividing 0
#include <stdio.h>
 
int main()
{
    int a = 5;
    printf("%d", a / 0);
    return 0;
}
 
 

Output:

Dividing by Zero Error Output

Error of dividing by zero

3. Segmentation Faults

Let us consider an array of length 5 i.e. array[5], but during runtime, if we try to access 10 elements i.e array[10] then we get segmentation fault errors called runtime errors. Giving only an array length of 5

C




// C program to demonstrate
// an error of segmentation faults
#include <stdio.h>
 
int main()
{
    int GFG[5];
    GFG[10] = 10;
    return 0;
}
 
 

But in output trying to access more than 5 i.e if we try to access array[10] during runtime then we get an error 

Output:

Segmentation Fault Error

Segmentation Fault Error

4. Large memory allocation/Large Static Memory Allocation : 

In general, all online judges allow up to 10^8. However, to be on the safe side, use up to 10^7 unless it is required. In the below example we have mentioned more than 10^8 so, it will cause an error due to large memory allocation.

C




// C program to demonstrate
// an error of large memory allocation
#include <stdio.h>
 
int main()
{
    int GFG[10000000000];
    printf("%d", GFG);
    return 0;
}
 
 

Output: 

Output of Large Memory Allocation Error

Large Memory Allocation Error

5. Making common errors: 

The below code gives runtime error because we have declared a variable as long int but in scanf we have used %d instead of %ld. So it will give an error.

C




// C program to demonstrate
// a common error
#include <stdio.h>
 
int main()
{
    long int GFG;
    scanf("%d", &GFG);
    return 0;
}
 
 

Output: 

Output of Common Error

Common Error



Next Article
Interesting Facts in C Programming | Set 2

L

laxmigangarajula03
Improve
Article Tags :
  • C Language
  • C Programs
  • C Error Handling Programs

Similar Reads

  • C Program to Show Types of Errors
    Here we will see different types of errors using a C program. In any programming language errors are common. If we miss any syntax like parenthesis or semicolon then we get syntax errors. Apart from this we also get run time errors during the execution of code. There are 5 types of error in C: Synta
    4 min read
  • C Program to Show Unreachable Code Error
    Here, we will see how to show unreachable code errors using the C program. Unreachable statements are statements that will not be executed during the program execution. These statements may be unreachable due to the following reasons: Return statement before print statement.Infinite loop before stat
    3 min read
  • C Program to Hide a Console Window On Startup
    Here in this article, we have created a console application that will write into a file. This console application will work in the background by hiding the console window. To ensure that the program or the console application is still running, we can see the live data appended in the text file creat
    4 min read
  • Using goto for Exception Handling in C
    Exceptions are runtime anomalies or abnormal conditions that a program encounters during its execution. C doesn’t provide any specialized functionality for this purpose like other programming languages such as C++ or Java. However, In C, goto keyword is often used for the same purpose. The goto stat
    4 min read
  • Interesting Facts in C Programming | Set 2
    Below are some more interesting facts about C programming: 1. Macros can have unbalanced braces: When we use #define for a constant, the preprocessor produces a C program where the defined constant is searched and matching tokens are replaced with the given expression. Example: [GFGTABS] C #include
    2 min read
  • Your First C Program
    Like in most of the programming languages, program to write the text "Hello, World!" is treated as the first program to learn in C. This step-by-step guide shows you how to create and run your first C program. Table of Content Setting Up Your EnvironmentCreating a Source Code FileNavigating to the S
    5 min read
  • C Program to Handle Divide by Zero
    In C programming, there is no built-in exception handling like in other high-level languages such as C++, Java, or Python. However, you can still handle exceptions using error checking, function return values, or by using signal handlers. There are 2 methods to handle divide-by-zero exception mentio
    3 min read
  • C Program to Print the Program Name and All its Arguments
    The command-line argument (CLA) is the parameter provided in the system upon request. Command-line conflict is an important concept in system C. It is widely used when one needs to control your system from the outside. Command-line arguments are transferred to the main () path. Argc calculates the n
    2 min read
  • How to Detect Memory Leaks in C?
    In C, memory leaks occur when programmers allocate memory by using functions like malloc, calloc, realloc etc., but forget to deallocate the memory by using free function. In this article, we will learn how to detect memory leaks in C. Detecting Memory Leaks in C ProgramsDetecting memory leaks in C
    3 min read
  • How to Write a Command Line Program in C?
    In C, we can provide arguments to a program while running it from the command line interface. These arguments are called command-line arguments. In this article, we will learn how to write a command line program in C. How to Write a Command Line Program in C? Command line arguments are passed to the
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences