
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What are all the common undefined behaviours that a C++ programmer should know about?
In C++, there are some undefined behaviors. These are identified by doing some tasks in C++. There are no such direct definitions. These few things should be known to all of the programmers, who want to use C++ for different purposes.
Here we will see some C++ Codes. and try to guess the results. The codes will generate some runtime errors.
The Divide By Zero error is undefined.
Example Code
#include <iostream> using namespace std; int main() { int x = 10, y = 0; int z = x / y; cout << "Done" << endl; }
Output
Runtime error for divide by zero operation
Trying to use uninitialized variable.
Example Code
#include <iostream> using namespace std; int main() { bool x; if(x == true) cout << "true value"; else cout << "false value"; }
Output
false value (This may differ in different compilers)
Trying to access null pointer values.
Example Code
#include <iostream> using namespace std; int main() { int *ptr = NULL; cout << "The pointer value is: " << *ptr; }
Output
Runtime error for accessing null pointer values
Trying to access null pointer values.
Example Code
#include <iostream> using namespace std; int main() { int array[10]; for(int i = 0; i<=10; i++) { cout << array[i] << endl; } }
Output
Runtime error for accessing item out of bound. Some compiler may return some arbitrary value, not return any error Going beyond limit of signed int.
Example Code
#include <iostream> using namespace std; int main() { int x = INT_MAX; cout << "x + 1: " << x + 1; }
Output
x + 1: -2147483648 circulate to the minimum number of signed int
Trying to change some character in String literals.
Example Code
#include <iostream> using namespace std; int main() { char *str = "Hello World"; str[2] = 'x'; cout << str; }
Output
Runtime error because we are trying to change the value of some constant variables.
- Related Articles
- Common undefined behaviours in C++ programming
- What you should know about VPN passwords?
- What You Should Know About Vasectomy Side Effects
- What are some facts about forests in India which everyone should know?
- What You Should Know About Getting 6-Pack Abs
- What Is the most important thing I should know about now?
- Everything You Should Know About Cloud Mining
- 10 Facts About HIV/AIDS Everyone Should Know
- What are the things to know about new borns?
- What Are the Common Myths About Digital Marketing?
- 6 Unusual Signs of Dehydration You Should Know About
- All you need to know about Chromecast
- What are the best new products or inventions that most people don't know about?
- What is the query to know about all character sets supported by MySQL?
- All you need to know about Network Neutrality

Advertisements