-1

I noticed a piece of code that made me raise a few eyebrows in the code base which I am working on. Since I am not an expert of the C language, I first tried googling initializing fixed size arrays. However, I could not find anything like that:

float array[7] = {0.0,};

To be precise, I would like to know how the comma is interpreted in C here? I doubt it is a typing error, because I find this kind of initialization all over the place and for all types of arrays.

Is there any difference to just writing:

float array[7] = {0.0};
3

1 Answer 1

1

This has nothing to do with floats. It goes for all arrays. Imagine that that rule did not exist and you you have this:

float arr[] = {
    4.5, 
    2.3,
    3.8
};

Later, you realize that you want to remove 3.8. Then you would ALSO need to remove the comma after 2.3. And the same thing goes if you want to insert a new value. Let's say that you have copied a value from somewhere. Now you cannot simply paste it. Because if the copied string has a comma, you could not simply paste it anywhere you want in the array.

The basic purpose is that you should not have to think about adding and removing commas. It's just convenience.

Imagine copying and pasting regular code and you would have to think about that for the semicolon everytime. That would be a bit annoying. That's how it is in Pascal. ;)

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but Pascal doesn't lie about the exact location of an error... ;)
@linuxfansaysReinstateMonica I beg to differ. I've had lots of situations where Turbo Pascal reported an error in the line above

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.