Question:
what is wrong with this c code?
2011-07-27 19:50:20 UTC
/* print_it c this program prints a listing with line numbers */
#include
#include
void do_heading(char *filename);
int line, page;
main (int argv, char *argc [])
{
char buffer [256];
FILE *fp;
if (argv <2)
{
fprintf(stderr,"\nProper Usage is: ");
fprintf(stderr, "\n\n PRINT_IT filename.ext\n" );
exit (1);
}
{
if((fp = fopen( argc [1], "r")) == NULL)
{
fprintf( stderr, "error opening file, %s!", argc[1]);
exit(1);
}
page =0;
line= 1;
do_heading ( argc[1]);
while( fgets( buffer, 256, fp )!=NULL )
{
if(line % 55 ==0)
do_heading (argc[1] );
fprintf( stdout, "%4d :\t%s", line++, buffer );
}
fprintf (stdout, "\f" );
fclose (fp);
return 0;
}
void do_heading (char *filename )
{
page ++;
if (page >1)
fprintf (stdout, "\f" );
fprintf ( stdout, "Page: %d, %s\n\n", page, filename );
}
}

I'm doing this for myself it says wehn I try to build and compile it- I am copping it from a book -teach yourself c in 21 days and using codeblocks
test.c||undefined reference to `do_heading'|
test.c||undefined reference to `do_heading'|
||=== Build finished: 2 errors, 0 warnings ===|
Three answers:
McFate
2011-07-27 19:59:44 UTC
The open-brace above the "fp = fopen" line seems to be spurious.



The second closed-brace at the end seems to be spurious.



I removed those two characters and it compiled for me.
husoski
2011-07-27 20:33:12 UTC
You have an extra opening brace ({) just before the if (fp = fopen...), and an extra closing brace at the very end.



Hint: C compilers generally try to compile as much as possible, internally patching your errors as they are found to try to help you find and fix multiple errors, not just the first one. However, this process goes wonky sometiimes, causing errors to reported that aren't really there. Moral: Fix the *first* error listed by the compiler. Look at later ones, but if you don't see what's wrong--don't worry. Just recompile with the earlier fixes. If it was a fixup error, it will go away. If it's your error, it won't.



In this case, the first error (using Code::Blocks with GCC(mingw)) was "ISO C forbids nested functions. That says that the previous function was still open when it saw the start of do_heading().



Another hint. If you have the Source Code Formmatter (AStyle) in your plugins menu, then you can select that to auto-indent your entire program. The AStyle indentation style sucks, so you probably won't want to leave it that way (Ctrl+Z or Edit>Undo will get you back), but you will see nesting problems visually.



If the book *really* has you begin the function with "main(int argc, char **argv)", instead of "int main(int argc, char **argv)", consider buying another book. Sure, learn what you can from this, but don't learn coding style. That's 30-year old Unix style from a time when computer RAM was measured in kilobytes or a very few megabytes for a large system. You can actually say what you mean, nowdays, and not worry about the source code being too big to compile.
tatsuhara
2016-12-10 17:20:43 UTC
void WriteDay(char d[]); That line broadcasts a functionality prototype. It shall we the compiler understand that there is a functionality referred to as WriteDay and what arguments might desire to be surpassed to it. It does no longer tell the compiler how the WriteDay functionality is applied. you will possibly desire to have the certainly functionality someplace so as that the compiler is conscious what it fairly is meant to do.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...