C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-29-2001, 12:29 PM   #1
Adam
Guest
 
Posts: n/a
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
  Reply With Quote
Old 11-29-2001, 05:00 PM   #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.
mix0matt is offline   Reply With Quote
Old 11-30-2001, 11:10 AM   #3
Adam
Guest
 
Posts: n/a
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
  Reply With Quote
Old 11-30-2001, 11:32 AM   #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
starX is offline   Reply With Quote
Old 11-30-2001, 11:33 AM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,699
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" ); 
}
Salem is offline   Reply With Quote
Old 12-03-2001, 08:54 AM   #6
Unregistered
Guest
 
Posts: n/a
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
  Reply With Quote
Old 01-01-2002, 02:12 AM   #7
Banned
 
zahid's Avatar
 
Join Date: Aug 2001
Posts: 532
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?
zahid is offline   Reply With Quote
Old 01-01-2002, 02:41 AM   #8
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,699
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.
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Resource ICONs gbaker Windows Programming 4 12-15-2003 07:18 AM


All times are GMT -6. The time now is 03:25 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22