简体   繁体   English

c 编程中长数据类型的默认值

[英]default values for long datatype in c programming

I am trying to add two long variables with default values.我正在尝试添加两个具有默认值的长变量。 But one of the variable is getting assigned to value 1. But As per my knowledge it should get assigned with 0. why it was happening?但是其中一个变量被赋值为 1。但据我所知,它应该被赋值为 0。为什么会发生这种情况?

#include <stdio.h>

long add(long, long);

main()
{
    long xno;
    long zno;
    long sum;

    printf("xno = %d, zno = %d \n", xno, zno);

    sum = add(xno, zno);

    printf("sum = %d", sum);
}

long add(long x, long y) {
    printf("x = %d, y = %d \n", x, y);
   return x + y;
}

result: 
xno = 0, zno = 1
x = 0, y = 1
sum = 1

result结果

Objects with automatic storage duration are not initialized by default.默认情况下不会初始化具有自动存储持续时间的对象。 The C standard specifies they have “indeterminate” value, which means they do not have any fixed value. C 标准指定它们具有“不确定”值,这意味着它们没有任何固定值。 Essentially, it means your program is broken.从本质上讲,这意味着您的程序已损坏。

In a program where the objects had been initialized or had been assigned values, the compiler would manage their values properly: If it needed the value of xno and did not have it in a processor register already, the compiler would generate a load instruction to bring it into a register.在对象已被初始化或已分配值的程序中,编译器将正确管理它们的值:如果它需要xno的值但处理器寄存器中没有它,编译器将生成一条加载指令以将它进入一个寄存器。 If it had it in a register, it would use that register for the value of xno .如果它在寄存器中,它将使用该寄存器作为xno的值。 So, in a working program, the compiler generates appropriate instructions.因此,在一个工作程序中,编译器会生成适当的指令。

What you are seeing is that, since xno and zno were never initialized or assigned values, the compiler is omitting the instructions that would load their values.您所看到的是,由于xnozno从未初始化或分配过值,因此编译器省略了加载它们的值的指令。 Instead of filling a register with the value loaded from memory, the program executes using whatever data was in the register from some prior use.该程序不是使用从内存加载的值填充寄存器,而是使用寄存器中先前使用的任何数据来执行。 Further, the register the compiler uses for that may be different each time xno or zno is used.此外,每次使用xnozno时,编译器使用的寄存器可能不同。 So a program could behave as if it is using different values for xno and zno each time they are used.因此,程序的行为就像每次使用xnozno时都使用不同的值xno

Further, in some circumstances, the C standard specifies that using an uninitialized automatic object has undefined behavior.此外,在某些情况下,C 标准规定使用未初始化的自动对象具有未定义的行为。 (This occurs when the address of the object is never taken.) In this case, the C standard not only does not specify what value the object has, it does not specify what the behavior of the program is at all. (当对象的地址从未被获取时会发生这种情况。)在这种情况下,C 标准不仅没有指定对象具有什么值,而且根本没有指定程序的行为是什么。

There is no default value for non static local variables in the c language. c语言中非静态局部变量没有默认值。 You can think of having a non fixed 'random garbage value' for those declared variables.您可以考虑为那些声明的变量设置一个非固定的“随机垃圾值”。 Also, in printf for printing long you should use %ld rather than %d此外,在 printf 中进行长时间打印,您应该使用%ld而不是%d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM