কিছু C/C++ কোড রানটাইম এরর বা অনির্দিষ্ট ফলাফল দেয়। নিচে কিছু উদাহরণ:
১. Divide by Zero
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 0;
int z = x / y;
cout << "Done" << endl;
}
আউটপুট
Runtime error for divide by zero operation
২. অনিয়ালাইজড ভেরিয়েবল ব্যবহার
#include <iostream>
using namespace std;
int main() {
bool x;
if (x == true) cout << "true value";
else cout << "false value";
}
আউটপুট
false value (কম্পাইলারের উপর নির্ভর করে ভিন্ন হতে পারে)
৩. NULL পয়েন্টার ডিরেফারেন্স
#include <iostream>
using namespace std;
int main() {
int *ptr = NULL;
cout << "The pointer value is: " << *ptr;
}
আউটপুট
Runtime error for accessing null pointer values
৪. অ্যারে বাউন্ড চেক না করা
#include <iostream>
using namespace std;
int main() {
int array[10];
for (int i = 0; i <= 10; i++)
cout << array[i] << endl;
}
আউটপুট
Runtime error বা আঁটপুট মান (কম্পাইলারের উপর নির্ভর)
৫. সাইনড ইন্টিজার ওভারফ্লো
#include <iostream>
#include <climits>
using namespace std;
int main() {
int x = INT_MAX;
cout << "x + 1: " << x + 1;
}
আউটপুট
x + 1: -2147483648 (মিনিমাম ইন্টে ঘুরে যায়)
৬. স্ট্রিং লিটারেল মডিফাই করা
#include <iostream>
using namespace std;
int main() {
char *str = "Hello World";
str[2] = 'x';
cout << str;
}
আউটপুট
Runtime error (কনস্ট্যান্ট মেমরি মডিফাই করার চেষ্টা)