Reputation: 785
I want to define a very long array in C and at the mean time write the elements not side by side but in a vertical manner. The code block I typed will illustrate the situation. Which character should I use at the end of the line in order to continue the definition of the array "/" works but the preceding and following elements are not printed and a zero or a one is printed instead. How can I accomplish this?
int i, grades[40] = {49, 80, 84, 73, 89, 78, 78, 92, 56, 85, 10, 84, 59, 56
62, 53, 83, 81, 65, 81, 69, 69, 53, 55, 77, 82, 81, 76, 79, 83, 74, 86
78, 55, 66, 60, 68, 92, 87, 86};
Thanks for your contribution. I am on Ubuntu 12.04 by the way (thought the end line character may be different for Ubuntu and Windows).
Edit: Comma was the culprit. Sorry to take your time.
Upvotes: 0
Views: 116
Reputation: 1817
As discussed in the other answer the compiler is smart enough to recognize if a statement extends over several lines.
However, there actually is an explicit way to tell the compiler that the statement continues in the next line : \
.
In your example you used a slash (/
) instead of a backslash and forgot the ,
. This lead to an integer division resulting in the 0
and 1
you observed.
If you want to use the '\' you could write the code like this:
int i, grades[40] = {49, 80, 84, 73, 89, 78, 78, 92, 56, 85, 10, 84, 59, 56, \
62, 53, 83, 81, 65, 81, 69, 69, 53, 55, 77, 82, 81, 76, 79, 83, 74, 86, \
78, 55, 66, 60, 68, 92, 87, 86};
Upvotes: 1
Reputation: 1576
In most cases, C compilers don't care about whitespace ( except for strings, etc. ). That means that as long as the syntax is still valid, you can have as much whitespace as you'd like. The only constraint is that each token has to be separated by at least one whitespace.
So, in short, you can write out the array with as many spaces, tabs, and newlines as you'd like, provided there is a comma after every element before the last one.
Upvotes: 1
Reputation: 8053
Escaping new-lines is only necessary in pre-processor directives and inside strings. Just enter it with all the white-space you like. As long as the compiler doesn't complain you should be good.
Upvotes: 5