-1

I got the following code from "The C Programming Language" written by Brian Kernighan and Dennis Ritchie. However, it won't compile with gcc:

#include <stdio.h>
#include <string.h>
#define MAXLINE 1000

int getline(char *line, int max);

{
 char line[MAXLINE];
 int found = 0;

 if (argc != 2)
  printf("Usage: find pattern\n");
 else
  while (getline(line, MAXLINE) > 0)
   if (strstr(line, argv[1])  != NULL) {
      printf("%s", line);
      found++;
   }
  return found;
}

All I get is: error: expected identifier or '(' before '{'.

What am I doing wrong?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Tensigh
  • 1,030
  • 5
  • 22
  • 44

4 Answers4

2

As this answer says, you omitted the line that declares the main function.

This line:

int getline(char *line, int max);

is a declaration of the getline function, which must be defined elsewhere. (If you dropped the ;, it could also be the first line of a full definition of getline, but that doesn't seem to be the intent.)

In this case, the intent appears to be for it to be defined in a different source file. You'll need to compile both that source file and this one, and then use the linker to combine them into an executable program.

You may run into another problem, though. Some implementations provide their own non-standard function called getline, and it's not compatible with the way you've declared it. If you're using gcc, you'll need to compile with an option that inhibits that non-standard definition, such as -ansi or -std=c99. For simplicity, you might consider using a name other than getline; get_line should be ok.

And of course you'll need to define the getline or get_line function somewhere. (You can put the definition in the same source file if you like, but I suspect the point of this exercise is to build programs from multiple source files.)

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • I see. That wasn't in the original code so I thought it might have been part of stdio.h, but apparently it's not. – Tensigh Oct 03 '13 at 11:15
1

I think you missed this line:

main(int argc, char *argv[])

Arguments in main() ignored when debugging in Visual C++

Community
  • 1
  • 1
FatihC
  • 106
  • 1
  • 4
0
int getline(char *line, int max);   // <-- Delete that semi-colon
{
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • Okay, I removed it. Now I get this: sting.c: In function ‘getline’: sting.c:8: error: ‘line’ redeclared as different kind of symbol sting.c:5: note: previous definition of ‘line’ was here sting.c:11: error: ‘argc’ undeclared (first use in this function) sting.c:11: error: (Each undeclared identifier is reported only once sting.c:11: error: for each function it appears in.) sting.c:15: error: ‘argv’ undeclared (first use in this function) – Tensigh Oct 02 '13 at 22:32
  • I typed this directly from the book so I'm just curious what's different between ANSI C and compiling it with GCC. – Tensigh Oct 02 '13 at 22:34
  • The code you typed in just doesn't make much sense at all. Please verify that you entered it correctly from your book. In particular: you are missing the declaration of main, which should look like: `int main(int argc, char** argv)`, and you're missing a function body (it is hard to tell if you are missing `getline` or `main`, but something is definitely missing!) – abelenky Oct 03 '13 at 14:32
  • That's kind of what I suspected - I couldn't tell if those were built in functions of stdio.h or not (guess they're not!). I'll double check it. – Tensigh Oct 03 '13 at 21:50
  • Not a good fix for this problem I'm afraid. – chqrlie Mar 23 '23 at 16:35
0

Remove the semicolon:

int getline(char *line, int max)

austin
  • 5,816
  • 2
  • 32
  • 40
  • Not a good fix for this problem I'm afraid. – chqrlie Mar 23 '23 at 16:35
  • @chqrlie What a strange and cryptic comment for a post from ***10 years ago!***. – abelenky Mar 24 '23 at 15:32
  • @abelenky: it is never too late to fix an incorrect answer. Removing the `;` does not fix the problem, it makes it worse. The OP just forgot to copy the `main(int argc, char *argv[])` line from the book. – chqrlie Mar 24 '23 at 15:56