简体   繁体   English

scanf 如何存储价值?

[英]How does scanf store value?

I'm new to C. Given the following code,我是 C 的新手。鉴于以下代码,

int main(void)
{
 int a;
 scanf("%d",&a);
 scanf("\n"); // consume trailing newline
}

As I understand it, the first declaration statement tells the compiler to allocate memory for an integer type variable with an identifier 'a' initialized to 0(as 'a==0').据我了解,第一个声明语句告诉编译器为整数类型变量分配内存,其标识符“a”初始化为 0(如“a==0”)。 Scanf needs a valid memory address to store the user input(say 5) so &a being the memory address of 'a' is where the user input is stored as 'a==5'. Scanf 需要一个有效的内存地址来存储用户输入(比如 5),因此 &a 是“a”的内存地址,用户输入存储为“a==5”。 Is this the case or 'a' is stored as a variable name for the memory address and the contents of the address are simply 5?是这种情况还是'a'被存储为内存地址的变量名并且地址的内容只是5?

Is this the case or 'a' is stored as a variable name for the memory address是这种情况还是“a”被存储为内存地址的变量名

&a is a pointer to the memory reserved for a . &a是指向为a保留的内存的指针。 The name “a” is not involved.不涉及名称“a”。

... initialized to 0 ... 初始化为 0

Automatic objects are not initialized by default.默认情况下不初始化自动对象。 Any object declared inside a function without static , extern , or _Thread_local has automatic storage duration.在没有staticextern_Thread_local的函数内声明的任何对象都具有自动存储持续时间。

The value of an uninitialized object is indeterminate , meaning it has no fixed value;未初始化对象的值是不确定的,这意味着它没有固定值; it may behave as if it has a different value each time it is used.每次使用它时,它可能表现得好像它具有不同的值。 (If you explicitly initialize any part of an automatic object, such as one element of an array or one member of a structure, the entire object will be initialized, defaulting to zeros for numeric types and null pointers for pointer types.) (如果显式初始化自动对象的任何部分,例如数组的一个元素或结构的一个成员,整个对象将被初始化,对于数字类型默认为零,对于指针类型默认为空指针。)

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

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