这些变量是哪些C语言的数据类型?

发布于 2025-02-09 15:19:02 字数 1255 浏览 2 评论 0原文

现在我正在学习 c语言,并且有两个任务。定义变量并在此处打印是我的代码:

    #include <stdio.h>

int main()
{
    printf("Excersise 1\n\n");
    int cVarOne = -250;
    int cVarTwo = 250;
    int cVarThree = 4589498;
    double cVarFour = 10000000000000000000;
    double cVarFive = -9000000000000000000;
    
    printf("%d\n%d\n%d\n%.0lf\n%.0lf\n\n", cVarOne,cVarTwo,cVarThree,cVarFour,cVarFive);
    
    printf("Excersise Two\n\n");
    unsigned short cVarSix = 43112;
    int cVarSeven = -1357674;
    int cVarEight = 1357674;
    int cVarNine = -1357674000;
    unsigned int cVarTen = 3657895000;

    printf("%u\n%d\n%d\n%d\n%lu\n", cVarSeven, cVarEight, cVarNine, cVarTen);
    return 0;
}

问题是要为练习1出现以下消息:

expersise 1

和练习两个相同的数字设置为变量,并未预测打印。这是结果:

excersise 2

我尝试使用不同的类型,但没有结果。我在哪里错了?应该使用哪些数据类型和指定符,为什么会发生这些错误?

我正在使用此在线编译器: https://www.onlinegdb.com/onlinegdb.com/online_c_compiler

感谢。

Now I am learning the C language and I have two tasks. Define variables and print them Here is my code:

    #include <stdio.h>

int main()
{
    printf("Excersise 1\n\n");
    int cVarOne = -250;
    int cVarTwo = 250;
    int cVarThree = 4589498;
    double cVarFour = 10000000000000000000;
    double cVarFive = -9000000000000000000;
    
    printf("%d\n%d\n%d\n%.0lf\n%.0lf\n\n", cVarOne,cVarTwo,cVarThree,cVarFour,cVarFive);
    
    printf("Excersise Two\n\n");
    unsigned short cVarSix = 43112;
    int cVarSeven = -1357674;
    int cVarEight = 1357674;
    int cVarNine = -1357674000;
    unsigned int cVarTen = 3657895000;

    printf("%u\n%d\n%d\n%d\n%lu\n", cVarSeven, cVarEight, cVarNine, cVarTen);
    return 0;
}

The problem is that for exercise 1 the following message appears:

Excersise 1

and for exercise two the same numbers that are set as variables are not printed propetly. Here is the result:

Excersise 2

I tried to use different types, but without result. Where am I wrong? What data types and specifiers should be used and why do these errors occur?

I am using this online compiler: https://www.onlinegdb.com/online_c_compiler

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在看孤独的风景 2025-02-16 15:19:02

练习2中的警告是自我解释的,简单地使用正确的转换说明符,以供无符号INT,如您最喜欢的C书的第1章所教授的那样。该答案的其余部分是关于练习1:


为什么要警告:

整数常数,这些内容:123,具有类似于变量的类型。

10000000000000000000不是浮点类型,因为它不会以。 float),但一个固定点签名的整数。编译器将尝试找到该值最小的整数类型,但根本找不到一个。

原因是因为10,000,000,000,000,000,000 = 10^19比最大的64位整数大,只能保持2^63-1 = 9.22*10^18。

但是,编译器指出,该值可能适合64位无符号整数,因为2^64 -1 = 18.44*10^18,足够大。因此,它尝试了一下,但会给您警告编写这种臭代码。


解决方案:

使用1000000000000000000000.0类型double> double
或者更好,请使用10E18,它也是double,但人类可读。


一般最佳实践:

  • 切勿将浮点和固定点混合在同一表达式中。
  • 切勿混合floatdouble在同一表达式中。 (请远离float完全在PC等大多数主流系统上。)
  • 当大小和可移植性很重要时,切勿使用默认的整数类型,例如intlong /code>等。代替使用stdint.h的类型。

The warning in Exercise 2 is self-explanatory, simple use the correct conversion specifier for unsigned int, as taught in chapter 1 of your favourite C book. The rest of this answer is regarding Exercise 1:


Why you get the warning:

Integer constants, these things: 123, have a type just like variables.

10000000000000000000 is not a floating point type since it doesn't end with . (double) or .f (float), but a fixed point signed integer. The compiler will try to find the smallest signed integer type where the value will fit, but it fails to find one at all.

The reason is because 10,000,000,000,000,000,000 = 10^19 is larger than the largest signed 64 bit integer, which can only hold values up to 2^63 - 1 = 9.22*10^18.

The compiler notes however that the value might fit in a 64 bit unsigned integer since 2^64 -1 = 18.44*10^18, large enough. Hence it tries that, but gives you the warning for writing such smelly code.


Solution:

Use 10000000000000000000.0 which is of type double.
Or better yet, use 10e18 which is also double but readable by humans.


General best practices:

  • Never mix floating point and fixed point in the same expression.
  • Never mix float and double in the same expression. (Stay clear of float entirely on most mainstream systems like PC.)
  • When size and portability matters, never use the default integer types of C like int, long etc. Use the types from stdint.h instead.
瑾兮 2025-02-16 15:19:02

每种整数类型在字节中具有固定的大小,用于值的内部表示。结果,可以将最大值和最小值的限制存储在整数类型中。

例如,对于类型uintmax_t,可以由编译器提供以下

#include <stdio.h>
#include <inttypes.h>

int main( void )
{
    uintmax_t x = UINTMAX_MAX;

    printf( "sizeof( uintmax_t ) = %zu and the maximum value is %"
            PRIuMAX "\n", sizeof( x ), x );

}

sizeof( uintmax_t ) = 8 and the maximum value is 18446744073709551615

特征 常数的第一个签名整数类型,可以代表这样的常数。但是,两项签名的整数类型都不包括长长int可以表示此常数。因此,编译器会发出一条消息,即这样的常数只能以无符号整数类型表示。

您可以使用像后缀这样的后缀使用

double cVarFour = 10000000000000000000llu;

常数确定常数具有类型无签名的长长int

至于第二个消息,您必须使用参数正确的转换说明符。

转换说明器lu旨在输出类型无签名的长int的对象的值。但是,printf的呼叫的相应参数

printf("%u\n%d\n%d\n%d\n%lu\n", cVarSeven, cVarEight, cVarNine, cVarTen);

具有类型unigned int

unsigned int cVarTen = 3657895000;

因此要么使用转换规范符u喜欢

printf("%u\n%d\n%d\n%d\n%u\n", cVarSeven, cVarEight, cVarNine, cVarTen);

或将参数施加到类型nosigned ling

printf("%u\n%d\n%d\n%d\n%lu\n", cVarSeven, cVarEight, cVarNine, ( unsigned long )cVarTen);

也喜欢此格式规范的长度修饰符L %。0LF没有效果。您可以写%。0F

Each integer type has a fixed size in bytes for internal representations of values. As a result there is a limit for the maximum and a minimum value that can be stored in an integer type.

For example for the type uintmax_t the following characteristics can be provided by the compiler

#include <stdio.h>
#include <inttypes.h>

int main( void )
{
    uintmax_t x = UINTMAX_MAX;

    printf( "sizeof( uintmax_t ) = %zu and the maximum value is %"
            PRIuMAX "\n", sizeof( x ), x );

}

The program output is

sizeof( uintmax_t ) = 8 and the maximum value is 18446744073709551615

When you are using integer constant without suffix like this 10000000000000000000 then the compiler selects the type of the constant the first signed integer type that can represent such a constant. However neither signed integer type including long long int can represent this constant. So the compiler issues a message that such a constant can be represented only in an unsigned integer type.

You could use a constant with a suffix like

double cVarFour = 10000000000000000000llu;

The suffix determines that the constant has the type unsigned long long int.

As for the second message then you have to use correct conversion specifiers with arguments.

The conversion specifier lu is designed to output values of objects of the type unsigned long int. However the corresponding argument of the call of printf

printf("%u\n%d\n%d\n%d\n%lu\n", cVarSeven, cVarEight, cVarNine, cVarTen);

has the type unsigned int

unsigned int cVarTen = 3657895000;

So either use the conversion specifier u like

printf("%u\n%d\n%d\n%d\n%u\n", cVarSeven, cVarEight, cVarNine, cVarTen);

or cast the argument to the type unsigned ling like

printf("%u\n%d\n%d\n%d\n%lu\n", cVarSeven, cVarEight, cVarNine, ( unsigned long )cVarTen);

Also the length modifier l in this format specification %.0lf has no effect. You could just write %.0f

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文