Different sums for 1 to 10 and 1 to 5
PS C:\Source>gcc sumVersion6.c PS C:\Source>a sum of 1..10: 55 sum of 1..5: 15 PS C:\Source>
Comments:
The identifier sum
in the function has block scope and
corresponds to a local variable for that function.
The same identifier used in main()
has block scope and
corresponds to a different variable.
The variables are local (automatic) and should be initialized.
/* --- sumVersion5.c --- */ #include <stdio.h> int sumInts( int limit ) { int j;int sum = 0;
for ( j=1; j<=limit; j++ ) sum += j; return sum; } void main() {int sum = sumInts( 10 );
printf( "sum of 1..10: %d\n", sum ); sum = sumInts( 5 ); printf( "sum of 1..5: %d\n", sum ); }