여기에서 몇 가지 C 및 C++ 코드를 볼 수 있습니다. 그리고 결과를 추측해 보세요. 코드는 일부 런타임 오류를 생성합니다.
1. 0으로 나누기 오류가 정의되지 않았습니다.
예시 코드
#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
2. 초기화되지 않은 변수를 사용하려고 합니다.
예시 코드
#include <iostream>
using namespace std;
int main() {
bool x;
if(x == true)
cout << "true value";
else
cout << "false value";
} 출력
false value (This may differ in different compilers)
3. 널 포인터 값에 액세스하려고 합니다.
예시 코드
#include <iostream>
using namespace std;
int main() {
int *ptr = NULL;
cout << "The pointer value is: " << *ptr;
} 출력
Runtime error for accessing null pointer values
4. 널 포인터 값에 액세스하려고 합니다.
예시 코드
#include <iostream>
using namespace std;
int main() {
int array[10];
for(int i = 0; i<=10; i++) {
cout << array[i] << endl;
}
} 출력
Runtime error for accessing item out of bound. Some compiler may return some arbitrary value, not return any error
5. 로그인 한도를 초과합니다.
예시 코드
#include <iostream>
using namespace std;
int main() {
int x = INT_MAX;
cout << "x + 1: " << x + 1;
} 출력
x + 1: -2147483648 circulate to the minimum number of signed int
6. 문자열 리터럴에서 일부 문자를 변경하려고 합니다.
예시 코드
#include <iostream>
using namespace std;
int main() {
char *str = "Hello World";
str[2] = 'x';
cout << str;
} 출력
Runtime error because we are trying to change the value of some constant variables.