[Courses] C Programming For Absolute Beginners, Lesson 6: Moar Functions, Local, Static, Global Variables
Akkana Peck
akkana at shallowsky.com
Fri Apr 13 03:29:53 UTC 2012
Carla Schroder writes:
> I got some compiler warnings; I'm traveling so I'll give it a closer look when
> I can. Perchance someone can figure out what is causing these warnings:
>
> $ gcc -Wall -o ~/bin/take-cake take-cake.c
> take-cake.c: In function ‘main’:
> take-cake.c:37:9: warning: implicit declaration of function ‘AnnandFran_take’
> [-Wimplicit-function-declaration]
> take-cake.c:63:17: warning: unused variable ‘e’ [-Wunused-variable]
> take-cake.c:62:17: warning: unused variable ‘d’ [-Wunused-variable]
> take-cake.c:61:17: warning: unused variable ‘c’ [-Wunused-variable]
> take-cake.c:60:17: warning: unused variable ‘b’ [-Wunused-variable]
> take-cake.c:59:17: warning: unused variable ‘a’ [-Wunused-variable]
> take-cake.c:71:1: warning: control reaches end of non-void function [-Wreturn-
> type]
Cute program, Leslie! It's fun to run it with different numbers and
watch the pieces of cake disappear.
The warning about AnnandFran_take is because the earlier forward
declaration is:
int AnnandFran_takes(int, int);
notice takes rather than take. So when gcc sees a call to
AnnandFran_take(), it just assumes it's an integer function that
hasn't been previously declared, and warns about it.
The ones about a through e are coming from this:
else if (piecesleft == 0)
{
printf ( "There are no pieces left. Done!\n\n");
lastround = 1;
int a = Dan_takes (piecesleft, lastround);
int b = Mike_takes (piecesleft, lastround);
int c = Marcie_takes (piecesleft, round, lastround);
int d = Fred_takes (piecesleft, lastround);
int e = AnnandFran_take (round, lastround);
}
Those variables a through e are being declared inside the else { }
clause, but they're never used.
I don't think those lines need to be there at all -- since you're
done, you don't need to do the final takes, right?
The final one, about control reacing the end of a non-void function,
is because you declared int main() { ... } but then at the end of
main(), you don't return an int. If you put
return 0;
at the end of main(), that should go away.
My gcc also wouldn't compile the program with the strings broken up
like they were in the email. I.e. instead of:
puts (" The party is over and your last 6 guests are leaving.\n
There is leftover cake and you would like them to take it with them,\n
...
Let's see how many rounds it takes to unload all the cake.\n");
I either had to put those all on one line, like:
puts (" The party is over and your last 6 guests are leaving.\n There is leftover cake and you would like them to take it with them,\n ... Let's see how many rounds it takes to unload all the cake.\n");
Or else add a lot of extra double-quotes:
puts (" The party is over and your last 6 guests are leaving.\n"
"There is leftover cake and you would like them to take it with them,\n"
"..."
"all the cake.\n");
...Akkana
More information about the Courses
mailing list