C语言中的初始化与赋值 [英] Initialization vs Assignment in C

查看:114
本文介绍了C语言中的初始化与赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的老师最近告诉我们,用C进行数组初始化有两种方式,即:

My instructor recently told that array initialization in C happens in two ways, namely:

  1. int a[5]={1,2,3,4,5};
  2. 类似
  3. int a[5], i; for(i=0;i<5;i++) scanf("%d", &a[i]);一样使用scanf()
  1. Manually like int a[5]={1,2,3,4,5};
  2. Using scanf() like int a[5], i; for(i=0;i<5;i++) scanf("%d", &a[i]);

在我看来,第二个方式"是分配的一种方式,而不是初始化的一种方式.因此,我决定检查一下这里的人们对此有何评论.我偶然发现了这篇帖子,其中一个答案声称:

In my opinion the second "way" is a way of assignment and not initialization. So I decided to check what people over here have to say about this. I stumbled upon this post where one answer claims:

如果您要问的只是术语(*尚不清楚 从您的问题开始),从字面上看,变量的初始化"是 第一次给它分配一个值.这个词来自事实 您将变量赋予它的初始"值.

If all you are asking about is terminology (*which isn't really clear from your question), "Initialization" of a variable is, literally, the first time a value is assigned to it. This term comes from the fact that you are giving the variable it's "initial" value.

这(显然)应该在第一次使用之前发生.

This should (obviously) happen before it is used the first time.

int x=5;是一个声明和一个初始化,实际上只是 方便的简写

int x=5; is a declaration and an initialization, and is really just convenient shorthand for

int x; x=5;

如果我要遵循这个特定答案的主张,那么初始化"的第二种方法是正确的,因为在scanf()语句之前未分配任何值.但是,由于我对静态变量的了解,我的脑海中出现了新的疑问.考虑以下代码:

If I'm to follow what this particular answer claims, then the second way of "initialization" is correct, because no value was assigned prior to the scanf() statement. However, thanks to my knowledge on static variables a new doubt arises in my mind. Consider the following code:

#include <stdio.h>

void first_way(){
    static int x[2]={1,2};
    printf("first_way called %d time(s)\n",++x[0]);
}

void second_way(){
    int i;
    static int x[2];
    for(i=0;i<2;i++)scanf("%d",&x[i]);
    printf("second_way called %d time(s)\n",++x[0]);
}

int main(void){
    int i;
    for(i=0;i<3;i++)
        first_way();
    printf("\n#######\n");
    for(i=0;i<3;i++)
        second_way();
    return 0;
}

其输出是这样的:

first_way called 2 time(s)
first_way called 3 time(s)
first_way called 4 time(s)

#######
1 2
second_way called 2 time(s)
1 2
second_way called 2 time(s)
1 2
second_way called 2 time(s)

此输出再次使我想到scanf()版本更像 assignment 版本,而不是 initialization 版本,即使之前没有为x[]元素赋值scanf()语句.回到第一广场.

This output again leads me to think of scanf() version more like an assignment version rather than initialization even though no value to elements of x[] were assigned before the scanf() statement. Back to square one.

那么,第二个版本真的像我的讲师所声称的那样是初始化吗?还是仅仅是一个赋值(这是我所相信的)?

So, is the second version really an initialization like my instructor claims or merely an assignment (which is what I believe)?

在有人指出之后,我觉得我的static数组示例是一个糟糕的例子,因为static变量无论如何都隐式初始化为0.再说一次,其他人将我引向了const变量.

After someone pointed out, I feel my static array example is a poor one as static variables are implicitly initialized to 0 no matter what. Then again, someone else pointed me towards const variables.

考虑const int x = 2;此处可以初始化x,但是在初始化后不能为其分配任何值.但这与要求(我再次引用)的答案相冲突:

Consider const int x = 2; Here one can initialize x, but cannot assign any value to it after initialization. But this is conflicting with the answer that claims (I quote it again):

int x = 5;是一个声明和一个初始化,实际上只是 int x; x=5;

int x = 5; is a declaration and an initialization, and is really just convenient shorthand for int x; x=5;

那么,毕竟,scanf()版本是否可以用作初始化程序?

So, after all this, does the scanf() version qualify as an initializer?

推荐答案

在C标准中,只有选项(1)是初始化.

In the C Standard, only option (1) is initialization.

在编程术语中,两者都可以视为初始化.您的问题实际上是在询问单词的含义.

In programming jargon, both may be considered initialization. Your question is really asking about the meanings of words.

人们通常使用具有各种常见含义的单词,而不是切换特定语言的术语.另一个示例是通过引用传递". C是否通过引用传递?有些人认为它只具有按值传递,而另一些人则认为按指针传递实现了按引用传递"的概念.

It's normal for people to use words with various common meanings, instead of switching terminology for particular languages. Another example is "pass by reference". Does C have pass by reference or not? Some would argue it only has pass by value, others would argue that passing by pointer implements the concept of "pass by reference".

然后,我们可以讨论深层副本与浅层副本(C标准根本没有提及),或者术语堆栈"和堆"(C标准也没有提及,但常用) (由C程序员编写),等等.

Then we could talk about deep copy vs shallow copy (which is not mentioned by the C Standard at all), or the terms "stack" and "heap" (which are not mentioned by the C Standard either, but commonly used by C programmers), and so on.

如果您说{ int b; b = 5; }不是初始化(因为C标准说不是),那么为了保持一致,您还应该说b不是堆栈变量.

If you say { int b; b = 5; } isn't initialization (because the C Standard says it isn't) then, to be consistent, you should also say that b is not a stack variable.

这篇关于C语言中的初始化与赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆