Thread: error listings for C

  1. #1
    Adam
    Guest

    error listings for C

    Hello,
    I'm new to C on Linux programming. I started
    by testing simple programs from book "C in 21 days". Unfortunately, some of the source code listed there gives a whole bunch of error messages that are not explained in this book.
    Is anybody aware about a book or website that lists error messages and provides descent explanation?
    Thanks

    Adam

  2. #2
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    i'm afraid most people are going to need a lot more information. Are they run time errors, compiler errors, or linker errors. What are you trying to do? Post a bit of your source code and the error messages you're getting.

  3. #3
    Adam
    Guest

    compilation errors

    Hello,
    Thanks for responding. Here is what I try to do.
    On the Linux (Red Hat 7.1) box I wrote a simple program to display listing with the line numbers (page 30 of the book "C in 21 days")
    Here is the code (copied character by character from the book), some lines were wrapped because typing window is too small.

    /*list_it.c This program displays listing with the line numbers! */
    #include <stdio.h>
    #include <stdlib.h>

    void display_usage(void);
    int line;

    main( int argc, char *argv[] )
    {
    char buffer[256];
    FILE *fp;

    if( argc < 2)
    {
    display_usage();
    exit(1);
    }

    if (( fp = open(argv[1], "r" ) == NULL )
    {
    fprintf( stderr, Error opening file, %s!", argv[1] );
    exit(1);
    }

    line = 1;
    while( fgets( buffer, 256, fp ) !=NULL )
    fprintf( stdout, "%4d:\t%s", line++, buffer );

    fclose(fp);
    return 0;
    }

    void display_usage(void)
    {
    fprintf(stderr, "\nProper Usage is: ");
    fprintf(stderr, "\n\nlist_it filename.ext\n" );
    }

    Here are the error messages in order they appeare when I run the command
    gcc -o list_it list_it.c

    list_it.c: In function 'dispalay usage'
    list_it.c:8: parse error before 'main'
    list_it.c:13: parse error before 'if'
    list_it.c:11: declaration for parameter 'fp' but no such parameter
    list_it.c:6: declaration for parameter 'line' but no such parameter
    list_it.c:13:number of arguments doesn't match prototype
    cc1: prototype declaration
    list_it.c:At the top level
    list_it.c:19: parse error before 'if'
    list_it.c:22: parse error before '1'
    list_it.c:22: conflicting types for 'exit'
    /usr/include/stdlib.h:578: previous declaration of 'exit'
    list_it.c:22: warning: data definition has no type or storage class
    list_it.c:25: warning: data definition has no type or storage class
    list_it.c:27: parse error before 'while'
    list_it.c:30: warning: parameter names (without types) in function declaration
    list_it.c:30: warning: data definition has no type or storage class
    list_it.c:31: parse error before 'return'
    list_it.c:35: redefinition of 'display usage'
    list_it.c:6: 'display usage' previously defined here

    These are all the messages, it is very discouraging for the beginner, especially that there is no explanation of theses messages (or I can't find it). I copied another simple program from the same book "multiply.c" and the the whole process (compilation & execution) was error free...
    I'd really appreciate comments and suggestions re good resource book or website.

    Cheers

    Adam

    PS When I run "gcc -o list_it list_it.c" on Solaris machine I'm getting only these messages:
    list_it.c:36: unterminated string or character constant
    list_it.c:21: possible real start of unterminated constant

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    the parse error before main is because the declaration for main is:

    int main(int argc, char *argv[])

    try a recompile and see if it makes any of those other error msgs go away.

    starX
    www.axisoftime.com

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look at the /*!! !!*/ comments carefully
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    void display_usage(void); 
    int line; 
    
    int /*!! main returns int !!*/main( int argc, char *argv[] ) { 
        char buffer[256]; 
        FILE *fp; 
    
        if(argc < 2) { 
            display_usage( ); 
            exit( 1 ); 
        } 
    
        if(( fp = /*!!open should be!!*/fopen( argv[1], "r" ))/*!!added extra )!!*/== NULL) { 
            /*!! missing ".. v !!*/
            fprintf( stderr, "Error opening file, %s!", argv[1] ); 
            exit( 1 ); 
        } 
    
        line = 1; 
        while(fgets( buffer, 256, fp ) !=NULL) 
            fprintf( stdout, "%4d:\t%s", line++, buffer ); 
    
        fclose( fp ); 
        return 0; 
    } 
    
    void display_usage(void) { 
        fprintf( stderr, "\nProper Usage is: " ); 
        fprintf( stderr, "\n\nlist_it filename.ext\n" ); 
    }

  6. #6
    Unregistered
    Guest
    Hi,
    Thanks for your help:-)
    I changed the code as suggested and it works but only on Solaris. When I copied the code from the previous posting and try to compile on Linux the error messages still appeare when I run the command:

    gcc -o list_it list_it.c


    list_it.c: In function 'dispalay usage' list_it.c:9: parse error before '{'
    list_it.c:9: declaration for parameter 'main' but no such parameter
    list_it.c:6: declaration for parameter 'line' but no such parameter
    list_it.c:9:number of arguments doesn't match prototype
    cc1: prototype declaration
    list_it.c:13: 'argc' undeclared (first use in this function)
    list_it.c:13: (Each undeclared identifier is reported only once
    list_it.c:13: for each function it appears in.)
    list_it.c:13: 'argc' undeclared (first use in this function)
    list_it.c:19: 'argv' undeclared (first use in this function)
    list_it.c:21: 'stder' undeclared (first use in this function)
    list_it.c:31: warning: 'return' with a value in function returning void
    list_it.c:At the top level
    list_it.c:35: redefinition of 'display usage'
    list_it.c:6: 'display usage' previously defined here

    Any other suggestions...?

    Cheers

    Adam

  7. #7
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Hello,
    I have checked and compiled Salem's code. I found it working fine in Linux Slackware.

    I found no OS specific function call on the code, so it does not matter what the OS is.


    I guess something wrong with your compiler or it's installation.

    Have you tried with hello world program with your current compiler?
    If not try that first.

    Note: Why don't you get registered?
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suggest you re-post your latest code, because those error messages suggest that your display_usage prototype is actually a function definition - ie

    void display_usage(void) {

    not

    void display_usage(void);


    An occasional cause of wierdness is trying to compile a DOS format file (\n\r line ends) on a *ix system which just uses \n line ends.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM