Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
717 views
in Technique[技术] by (71.8m points)

language lawyer - Initialization vs Assignment in C

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

  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; is a declaration and an initialization, and is really just convenient shorthand for

int x; x=5;

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)
",++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)
",++x[0]);
}

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

Its output is like this:

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)

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)?

Edit:

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.

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; is a declaration and an initialization, and is really just convenient shorthand for int x; x=5;

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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".

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...