abs

絶対値を取得し,結果を int 型で返します.

#include <stdlib.h>
int abs(
    int j
);

abs 関数は整数 j の絶対値を計算し,結果を int 型で返します.結果が表現できないときの動作は未定義です.

機能がよく似た関数に labs 関数,llabs 関数があります.

戻り値

  • 絶対値

C言語サンプルプログラム

abs 関数を使用して絶対値を求めるサンプルプログラムを以下に示します.

/* header files */
#include <stdio.h>
#include <stdlib.h>

/* main */
int main(void) {
    int positive = 100;
    int negative = -100;

    printf("abs(%d) = %d\n", positive, abs(positive));
    printf("abs(%d) = %d\n", negative, abs(negative));

    return EXIT_SUCCESS;
}

実行例

サンプルプログラムの実行結果は以下のようになります.

abs(100) = 100
abs(-100) = 100