# 2.74 Sub OK

**Problem**:

Write a function with the following prototype:

```c
/* Determine whether arguments can be subtracted without overflow */
int tsub_ok(int x, int y);
```

This function should return 1 if the computation `x-y` does not overflow.

Code:

```c
#include <stdio.h>
#include <assert.h>
#include <limits.h>

int tsub_ok(int x, int y) {
    int sub = x - y;
    int pos_flag = x >= 0 && y < 0 && sub < 0;
    int neg_flag = x < 0 && y >= 0 && sub >=0;
    return !pos_flag && !neg_flag;
}

int main() {
    assert(!tsub_ok(INT_MIN, 1));
    assert(!tsub_ok(INT_MAX, -1));
    assert(tsub_ok(0, 0));
    assert(!tsub_ok(INT_MIN, INT_MAX));
    assert(!tsub_ok(INT_MAX, INT_MIN));
    assert(tsub_ok(10, 8));
    return 0;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://valineliu.gitbook.io/deuterium-wiki/reading/cs-jing-dian-shu-ji/csapp-3e-homework-solution/2.-representing-and-manipulating-information/2.74-sub-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
