C - conflicting error types for function

I am new to C. I am trying to get a lot of text from the user and count the number of words, characters, lines, spaces and letters. This is what I did:

#include <ctype.h> #include <stdio.h> int main(void) { char c = getchar(); char previousc; int charcount = 0; int wordcount = 0; int whitespacecount = 0; int linecount = 0; int lettercount = 0; while(c != EOF) { if(isLetter(c) == 1) lettercount++; if(isWhitespace(c) == 1) { whitespacecount++; if(isWhitespace(previousc) == 0) wordcount++; } if(c == "\n") linecount++; previousc = c; c = getchar(); charcount++; } printf("Character Count: %d\n Word Count: %d\n Whitespace Count: %d\n Letter Count: %d\n Line Count: %d\n", charcount, wordcount, whitespacecount, linecount, lettercount); } int isLetter(char c) // 1 for true, 0 for false. { // instead of writing tons of if's if(isalpha(c) > 0) return 1; return 0; } int isWhitespace(char c) // 1 for true, 0 for false. { if(c == "\n" || c == " " || c == " ") return 1; return 0; } 

But I have so many errors and warnings that I just got lost ...

 program2.c: In function 'main': program2.c:20: warning: comparison between pointer and integer program2.c: At top level: program2.c:28: error: conflicting types for 'isLetter' program2.c:28: note: an argument type that has a default promotion can't match an empty parameter name list declaration program2.c:14: error: previous implicit declaration of 'isLetter' was here program2.c:35: error: conflicting types for 'isWhitespace' program2.c:35: note: an argument type that has a default promotion can't match an empty parameter name list declaration program2.c:15: error: previous implicit declaration of 'isWhitespace' was here program2.c: In function 'isWhitespace': program2.c:36: warning: comparison between pointer and integer program2.c:36: warning: comparison between pointer and integer program2.c:36: warning: comparison between pointer and integer 

I searched for various errors, but did not find solutions that solve my problem.

Can you help me a little?

Thanks.

+7
c
source share
5 answers

For

program2.c: 20: warning: comparison between pointer and integer

Change

  if(c == "\n") 

to

  if(c == '\n') 

For

program2.c: 28: error: conflicting types for 'isLetter
program2.c: 28: note: the type of the argument, for which the default prompt is used by default, corresponds to the declaration of a list of empty parameters. program2.c: 14: error: previous implicit declaration of 'isLetter was here
program2.c: 35: error: conflicting types for 'isWhitespace
program2.c: 35: note: the type of the argument, which by default does not support promotion, corresponds to the declaration of the empty parameter list program2.c: 15: error: there was an implicit declaration of 'isWhitespace

Define prototypes for your functions.

 int isLetter(char c); int isWhitespace(char c); 

For

program2.c: In the function 'isWhitespace:
program2.c: 36: warning: comparison between pointer and integer program2.c: 36: warning: comparison between pointer and integer program2.c: 36: warning: comparison between pointer and integer

Change

 if(c == "\n" || c == " " || c == " ") return 1; 

to

 if(c == '\n' || c == ' ' || c == '\t') 
+4
source share

Start with the first error / warning, fix it, and then make your way one by one, always compiling after each change. You will often find that getting rid of an error / warning on the line also gets rid of the others in the following lines.

Line 20:

  if(c == "\n") linecount++; 

gives a warning

 program2.c:20: warning: comparison between pointer and integer 

c is a char (internally converted to an integer before comparison); "\n" is an array [2] of char (internally converted to char * before comparison).
Therefore, the compiler complains about comparing an integer and a pointer.

You need to compare c with the character (both will be internally converted to integers)

  if(c == '\n') linecount++; 
+3
source share
  • Declare the following functions before , calling them (i.e. above the main function):

    • int isLetter(char c);
    • int isWhitespace(char c);

  • In the main function:

    • Replace char c variable declaration char c int c
    • Replace isLetter(c) call isLetter(c) isLetter((char)c)
    • Replace isWhitespace(c) call isWhitespace(c) isWhitespace((char)c)
    • Replace previous = c with previous = (char)c
    • Replace conditional operator if (c == "\n") with if ((char)c == '\n')

The reason for int c is that the getchar function returns an int to support the EOF indicator.


  • In the isWhitespace function isWhitespace change the conditional statement to:

    • if (c == ' ' || c == '\n' || c == '\r' || c == '\t')
+2
source share

EOF is an integer value indicating the end of the input. This value is such that for any character ch , ch == EOF always false. Therefore, you should always compare a value of type int with type EOF , not char . It works on your computer because the char type is implemented as a signed char , but on machines where the char type is unsigned char , it will not.

Now let's get started with warnings and errors.

  • The scope of the function is from the point of its definition or declaration to the end of the program. You call functions like isLetter in main before they are declared.

  • "\n" is a string literal, not a character. So the " " and " " . The string literal here evaluates a pointer to its first element, and you compare this pointer to a character - a different type. Instead, you should compare with '\n' , ' ' , '\t' respectively.

0
source share

you need to declare the function header before using it basically, for example:

 int isLetter(char c); int main(void){ char c = getchar(); char previousc; int charcount = 0; int wordcount = 0; int whitespacecount = 0; int linecount = 0; int lettercount = 0; while(c != EOF) { if(isLetter(c) == 1) lettercount++; if(isWhitespace(c) == 1) { whitespacecount++; if(isWhitespace(previousc) == 0) wordcount++; } if(c == "\n") linecount++; previousc = c; c = getchar(); charcount++; } printf("Character Count: %d\n Word Count: %d\n Whitespace Count: %d\n Letter Count: %d\n Line Count: %d\n", charcount, wordcount, whitespacecount, linecount, lettercount);} 

which will fix the error of conflicting types. but also you will have to change "to" "if you are checking characters.

0
source share

All Articles