当前位置:首页 > 行业动态 > 正文

c语言01怎么表示正负

在C语言中,正负数的表示方法有以下几种:

1、整数表示法:

正整数:以数字0开头,后面跟着一串非零数字。+123表示正整数123。

c语言01怎么表示正负

负整数:以数字0开头,后面跟着一个减号(),再跟一串非零数字。456表示负整数456。

2、浮点数表示法:

正浮点数:以数字0开头,后面跟着一串非零数字,最后以小数点(.)分隔整数部分和小数部分。+3.14表示正浮点数3.14。

c语言01怎么表示正负

负浮点数:以数字0开头,后面跟着一个减号(),再跟一串非零数字,最后以小数点(.)分隔整数部分和小数部分。2.71表示负浮点数2.71。

以下是使用C语言表示正负数的示例代码:

#include <stdio.h>
int main() {
    int positive_integer = +123; // 正整数
    int negative_integer = 456; // 负整数
    float positive_float = +3.14; // 正浮点数
    float negative_float = 2.71; // 负浮点数
    printf("Positive Integer: %d
", positive_integer);
    printf("Negative Integer: %d
", negative_integer);
    printf("Positive Float: %f
", positive_float);
    printf("Negative Float: %f
", negative_float);
    return 0;
} 

运行以上代码将输出以下结果:

c语言01怎么表示正负

Positive Integer: 123
Negative Integer: 456
Positive Float: 3.140000
Negative Float: 2.710000