簡體   English   中英

如果沒有在C中登錄,真的沒有辦法打印簽名的號碼嗎?

[英]Is there really no way to print a signed number without a sign in C?

我嘗試將其轉換為unsigned並將其聲明為unsigned。 沒用

#include <stdio.h>

int main(void)
{
    int number, modulo;
    int result = 0;

    printf("Please provide an integer: ");
    scanf("%i", &number);

    while (number != 0) {
        modulo = number % 10;   
        result += modulo;
        number = number / 10;
    }

    printf("%i", result);            
    printf("The sum of the digits of provided number is %i\n", result);

    return 0;
}

假設x是一個int變量,則printf("%d", x<0 ? -x : x)在2的補碼平台上打印除INT_MIN以外的所有值而沒有符號的x

下面的R ..建議:

printf("%u", (unsigned) (x<0 ? (-1 - x) + 1 : x);

…因為對於任何合理的平台,它也處理INT_MIN

#define ABS(x) (((x)<0)?(-(x)):(x))

(或者如正確指出的那樣,在stdlib有內置的abs() )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM