预期标识符或'('''''标记之前

时间:2014-04-08 23:03:11

标签: c gcc compilation

我写了以下代码:

#include <stdio.h> 
//To use fgets and printf I need to include stdio header

#include <string.h>
//To use strlen I need to include string header

#define MAXLINE 100 /* maximum input line length */

int suffix(char str[], char c);

/*
  The function suffix prints all the substrings that 
  (1) starts with the letter stored in the variable 'c' 
  (2) included in the string str[]

  Function should return the number of substrings.

 */


int main()
{
    char user_input[MAXLINE]; /* current input line */
    char ch = 0;
    int counter = 0;


    printf(" Please enter a string (up to 100 characters) and then press enter ");

    /*
    This will store a maximum of MAXLINE characters into the string from the standard input 
    and it will ensure it is null-terminated. 
    The fact that we've limited the input to the size of the array declared earlier 
    ensures that there's no possibility of buffer overruns.
    */
    fgets(user_input, MAXLINE, stdin);

    //Then let's clear the pesky newline that's been entered into the string using strlen:
    user_input[strlen(user_input)-1] = '\0';

    printf(" Please enter a character and then press enter ");
    ch = getchar();
    while ( getchar() != '\n' );

    counter = suffix(user_input,ch);

    return 0;
}



int suffix(char str[], char c);
{
  int i=0,j=0;

  for (i=0 ; (i < MAXLINE-1) ; i++)
  {
    if (str[i] == c)
      for (j=i ; (j < MAXLINE-1) ; j++)
    printf("%c", str[j]);
    count++;
    printf("\n");
  }

  return count;

}

我还写了以下make文件:

mystr: my_str.o
    gcc -g -ansi -Wall my_str.o -o mystr

my_str.o: my_str.c
    gcc -c -ansi -Wall my_str.c -o my_str.o

但是,我收到以下编译错误:

VirtualBox:~/Desktop/Exercises/Assignments/my_str$ make
gcc -c -ansi -Wall my_str.c -o my_str.o
my_str.c:2:1: error: expected identifier or ‘(’ before ‘/’ token
my_str.c:5:1: error: expected identifier or ‘(’ before ‘/’ token
my_str.c: In function ‘main’:
my_str.c:38:5: error: expected expression before ‘/’ token
my_str.c:38:15: warning: character constant too long for its type [enabled by default]
my_str.c:45:5: warning: implicit declaration of function ‘suffix’ [-Wimplicit-function-declaration]
my_str.c:25:9: warning: variable ‘counter’ set but not used [-Wunused-but-set-variable]
my_str.c: At top level:
my_str.c:52:5: error: conflicting types for ‘suffix’
my_str.c:52:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
my_str.c:45:15: note: previous implicit declaration of ‘suffix’ was here
my_str.c:53:1: error: expected identifier or ‘(’ before ‘{’ token
make: *** [my_str.o] Error 1
VirtualBox:~/Desktop/Exercises/Assignments/my_str$ 

请协助“预期标识符或'(''''令牌'错误。

为什么冲突类型的后缀?为什么隐式声明(C中的每个函数都必须在程序开头声明,所以我不清楚这里有什么问题)?

由于

3 个答案:

答案 0 :(得分:8)

您正在使用-ansi进行编译,根据man gcc禁用C ++样式注释(//):

  -ansi
       In C mode, support all ISO C90 programs.  In C++ mode, remove GNU
       extensions that conflict with ISO C++.

       This turns off certain features of GCC that are incompatible with
       ISO C90 (when compiling C code), or of standard C++ (when compiling
       C++ code), such as the "asm" and "typeof" keywords, and predefined
       macros such as "unix" and "vax" that identify the type of system
       you are using.  It also enables the undesirable and rarely used ISO
       trigraph feature.  For the C compiler, it disables recognition of
       C++ style // comments as well as the "inline" keyword.

答案 1 :(得分:2)

除了clcto所说的内容之外,还需要从函数定义的开头删除分号:

int suffix(char str[], char c)
{
    ...
}

您需要在使用之前声明变量count

    int i=0,j=0;
    int count = 0;

然后你的代码将编译(虽然它可能没有你期望的那样)。

答案 2 :(得分:0)

我应用了你所有的建议+做了一些额外的修复,现在它就像一个魅力。

#include <stdio.h> 

/*To use fgets and printf I need to include stdio header*/

#include <string.h>
/*To use strlen I need to include string header*/

#define MAXLINE 100 /* maximum input line length */

int suffix(char str[], char c);

/*
  The function suffix prints all the substrings that 
  (1) starts with the letter stored in the variable 'c' 
  (2) included in the string str[]
  (3) returns the number of substrings
 */


int main()
{
    char user_input[MAXLINE]; /* current input line */
    char ch = 0;
    int counter = 0;
    int i = 0;

    for (i=0; i < MAXLINE-1 ; ++i)
      user_input[i] = 0;


    printf(" \nPlease enter a string (up to 100 characters) and then press enter\n ");


    for (i=0; i < MAXLINE-1 && ( ch=getchar())!= EOF && ch !='\n'; ++i)
      user_input[i] = ch;

    user_input[i] ='\0';

    printf(" \nPlease enter a character and then press enter\n");
    scanf(" %c", &ch);

    counter = suffix(user_input,ch);

    printf("substring counter is = %d ", counter);

    printf("\n");

    return 0;
}



int suffix(char str[], char c)
{
  int i=0,j=0;
  int count = 0;

  printf("\n########################\n\n");
  printf("Substrings are as follows: \n");

  for (i=0 ; (i < MAXLINE-1) ; i++)
  {
    if (str[i] == c)
    {
      for (j=i ; (j < MAXLINE-1) ; j++)
    printf("%c", str[j]);

      count++;
      printf("\n");
    }
  }

  if (count == 0)
      printf("There are no substrings which begin with the specified character\n");

  return count;

}