Integer literal assigned to an unsigned variable without a U suffix

ID

c.maintainability.unsigned_literal_suffix

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Numeric Literal

Language

C / C++

Description

This integer literal is stored into an unsigned variable but has no U suffix, so the literal itself is signed and the compiler has to convert it. That mismatch between the literal’s own type and the variable’s type can silently change behavior once the literal grows past INT_MAX or is used in mixed signed/unsigned arithmetic. Add a U suffix, e.g. 5u, so the literal’s type already matches the variable it initializes.

Rationale

This integer literal is stored into an unsigned variable but has no U suffix, so the literal itself is signed and the compiler has to convert it. That mismatch between the literal’s own type and the variable’s type can silently change behavior once the literal grows past INT_MAX or is used in mixed signed/unsigned arithmetic. Add a U suffix, e.g. 5u, so the literal’s type already matches the variable it initializes.

The following code illustrates the pattern detected by this rule:

void configure(void)
{
    // FLAGGED: Integer literal assigned to an unsigned variable without a U suffix
    unsigned int retries = 5;
    unsigned int b;
    // FLAGGED: Integer literal assigned to an unsigned variable without a U suffix
    b = 5;

    unsigned int suffixed = 5u;

    unsigned int upper = 5U;

    unsigned int zero = 0;

    unsigned long timeout;
    // FLAGGED: Integer literal assigned to an unsigned variable without a U suffix
    timeout = 1000;

    unsigned long timeoutOk = 1000UL;

    int signedCount = 5;
    (void)retries;
    (void)signedCount;
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved