mwt said:
Hello. Today I wrote my first program in C. It adds up the elements in
an array. I am just beginning to learn this language. Any tips or
pointers about better ways to write/structure/format/etc. this code
would be much appreciated. Thanks.
mwt.
If this is your first C program, it's an excellent start. I'll offer
a few minor suggestions.
#include <stdio.h>
int add_array(int arr[], int arr_size)
Keep in mind that, in a parameter declaration "int arr[]" is exactly
equivalent to "int *arr"; in other words, you're really passing a
pointer, not an array. Section 6 of the comp.lang.c FAQ,
<
http://www.c-faq.com/>, covers arrays and pointers very well.
Some programmers (myself included) prefer not to use this
looks-like-an-array-but-acts-like-a-pointer declaration, but that's
just a matter of style; feel free to use whatever you're comfortable
with.
{
int j;
int total = 0;
for(j = 0; j < arr_size; j++)
total += arr[j];
return(total);
The parentheses are unnecessary. Again, this is a matter of style,
but I prefer "return total;"; a return statement isn't a function
call, and it shouldn't look like one.
This should be "int main(void)".
Consider calling this "length" rather than "size". The word "size"
implies sizeof, which gives the size of an object in bytes; the number
of elements in an array is its length. (If sizeof(int)==4, a
10-element array of int has a length of 10 and a size of 40.)
printf("Input size of array>");
Due to buffering, it's not guaranteed that this will appear
immediately. Add "fflush(stdout);" to ensure that the prompt appears
before the program starts waiting for input.
You'll find that the scanf() function has some pitfalls. In this
case, it will skip all leading whitespace, including newlines. If you
enter a valid integer, it will leave any characters *following* that
to be read by further calls; for example, if you type "123<ENTER>",
then read a single character, the character you read will be the
new-line character.
It doesn't matter in a small program like this, but it will matter
later on. One good approach is to use fgets() (*never* use gets()) to
read a full line, then use sscanf() to parse it.
Also, scanf() returns a result, which you're ignoring. What happens
if you enter something other than a valid number?
There are two C standards, C90 and C99. C99 theoretically supersedes
C90, but it's not yet universally implemented. For maximum
portability, you should consider sticking to C90 features.
Here you're using two C99-specific features (or perhaps gcc
extensions). C90 doesn't allow declarations to follow statements
within a block, though you can work around it by introducing a new
block. Also, C90 requires array lengths to be compile-time constants;
C99 introduces variable-length arrays, and gcc has supported something
very similar as an extension for some time. Burton Samograd already
suggested using malloc() instead (which also has the advantage that
you can catch allocation errors).
int i;
for(i=0; i<size; i++)
{
array = i+1;
}
printf("Sum of array elements is %d.\n", add_array(array,
size));
You should add "return 0;" here. (It's not strictly required in C99,
but it's a good idea anyway.
<OT>
To get warnings about non-standard code (i.e., to persuade gcc to act
as a conforming C compiler), you can use:
gcc -ansi -pedantic -Wall -Wextra
or, for C99:
gcc -std=c99 -pedantic -Wall -Wextra
(but see <http://gcc.gnu.org/c99status.html>).
The "-Wextra" option, equivalent to "-W", may give you more warnings
that are really appropriate.
</OT>