Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Unary minus in C

Unary minus negates an arithmetic operand after integer promotions. If the result type is unsigned, negation uses modular arithmetic. If it is a signed integer type, whether the negated value is representable depends on both the operand’s value and the type’s range.

Version: ISO C23; implementation support may vary

Syntax

-operand
-(expression)

-x uses unary minus: a prefix operator with one arithmetic operand. It does not modify x. By contrast, a - b subtracts one operand from another. In -1, - is an operator applied to the integer constant 1, not part of the constant.

Integer promotions apply, and the result has the promoted type. unsigned char promotes to int if int can represent all its values, otherwise to unsigned int. Ordinary _BitInt values are exempt from these promotions; _BitInt bit-fields have a separate rule. If the resulting type is unsigned, negation is modulo that type's maximum plus one: -1u == UINT_MAX. In C23, -INT_MIN has undefined behavior because the opposite of INT_MIN cannot fit in int. INT_MIN, INT_MAX, and UINT_MAX come from <limits.h>.

qsort from <stdlib.h> takes a comparator callback of type int (*)(const void *, const void *). Its arguments point to the elements being compared; for an int array, read them through const int *. Return a negative value, zero, or a positive value when the first element is less than, equal to, or greater than the second, respectively. a - b can overflow int, for example with INT_MAX and INT_MIN; using comparisons to return -1, 0, or 1 avoids that. C does not guarantee that qsort is stable: the order of elements that compare equal is unspecified.

The example's lookup returns a nonnegative index or uses -1 to mean not found. NULL terminates the string-pointer array. Checking i < INT_MAX before accessing the array bounds the search and prevents overflow of the signed int counter. The array must supply an accessible NULL terminator or at least INT_MAX accessible entries.

Examples

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int compare_int(const void *left, const void *right)
{
    const int a = *(const int *)left;
    const int b = *(const int *)right;
    return a < b ? -1 : a > b ? 1 : 0;
}

static int find_token(const char *const tokens[], const char *key)
{
    for (int i = 0; i < INT_MAX && tokens[i] != NULL; ++i) {
        if (strcmp(tokens[i], key) == 0) {
            return i;
        }
    }
    return -1;
}

int main(void)
{
    int values[] = {INT_MAX, 0, INT_MIN};
    const char *const tokens[] = {"red", "blue", NULL};

    printf("extremes: %d\n", compare_int(&values[2], &values[0]));
    qsort(values, sizeof values / sizeof values[0], sizeof values[0],
          compare_int);
    printf("sorted: %d\n",
           values[0] == INT_MIN && values[1] == 0 && values[2] == INT_MAX);
    printf("found: %d\n", find_token(tokens, "blue"));
    printf("missing: %d\n", find_token(tokens, "green"));
    return 0;
}

Result

extremes: -1
sorted: 1
found: 1
missing: -1

Comparing the extremes returns `-1` without subtraction. `sorted: 1` confirms the expected order. `blue` is at index `1`, and the missing `green` yields `-1`.

Related lessons

  • C · Lesson 88 · Advanced · Version: 3
  • C · Lesson 90 · Advanced · Version: 3

Sources