Thread: Seemingly correct printf statements bringing up errors

  1. #1
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33

    Seemingly correct printf statements bringing up errors

    Hello,

    I am having trouble with seemingly correct printf statements being reported by the compiler (Bloodshed Dev C++) as invalid. I am writing in C, and yes, the file type is set to C as well.

    The statements are:
    22. printf("This document contains");
    23. printf("%d",words);
    24. printf("words");

    And the errors:
    22 wordcount.c syntax error before string constant
    22 wordcount.c conflicting types for 'printf'
    22 wordcount.c a parameter list with an ellipsis can't match an empty parameter name list declaration
    22 wordcount.c conflicting types for 'printf'
    22 wordcount.c a parameter list with an ellipsis can't match an empty parameter name list declaration
    22 wordcount.c [Warning] data definition has no type or storage class
    23 wordcount.c syntax error before string constant
    23 wordcount.c [Warning] data definition has no type or storage class
    24 wordcount.c syntax error before string constant
    24 wordcount.c [Warning] data definition has no type or storage class

    I honestly don't understand the problems here. Initially, I could understand it was reporting problems as I had the 'words' variable on line 23 declared as a char earlier in the program. But I changed that to int, and added the %d conversion character to enforce that. I have the two strings in lines 22 and 24 wrapped in double quotes. I don't understand the error about the definition type, and there are no syntax errors, at least as far as I can see with my admittedly limited knowledge of C.

    If anyone can help here I'd be grateful. I've tried looking through 'C by Example' and 'C in a Nutshell' and the syntax here matches the examples in those books.

    Thanks in advance.

    Hussein.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If this doesn't help, you should post the code. Your summary is nice - but the doctor may need to see the actual patient - not just a drawing of that patient.

    1) Did you #include <stdio.h>
    2) Are you using C and not forcing C++ syntax?

    If your IDE requires a namespace, be sure your code sets it to standard.

    Adak

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I honestly don't understand the problems here.
    Look a few lines up. Often, if you can't find a problem with the reported line, the real problem is located prior to it. Sometimes errors can go unnoticed for several lines, and the result is a cryptic error on apparently correct code.
    My best code is written with the delete key.

  4. #4
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Hi there.

    Thanks for your replies. yes, I did include <stdio.h>. Not sure about the namespace, i think Bloodshed Dev++ is a straight out of the box compiler.

    Here's the code in case it helps:

    Code:
    #include <stdio.h>
    const char filename[] = "dump.txt";
    int words = 0;
    // Declare filenames and word count
    
    int main()
    {
        FILE * f = fopen(filename, "r");
    // Open the test file dump.txt
        int c = fgetc(f);
    // use fgetc to read characters from the file
            if(c != "\crlf") [
                printf("%x",c);
    // Cycle through file, if alpha characters print
                } else {
                    printf("\n");
    // Otherwise if spaces or line break print new line
                    words++;
    // Increment word count based on spaces and line break
                    }
        fclose(f);            
            printf("This document contains"); 
            printf("%d",words);
            printf("words");
        return 0;            
                    }
    The program is one to read from a text file and print out a list of the words, using spaces and line breaks as separaters.

    There seem to be some errors around lines 12, 17 and 21, relating to the pointer and to undefined variables, but I can't find the variables that are apparently causing the error. That having been said, i don't see what any of the above has to do with the printf statements..

    Thanks again for taking a look at this, I appreciate it and am happy to learn.

    Hussein.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    > if(c != "\crlf") [
    > printf("%x",c);


    What does [ do in c? I think this should be {.

    EDIT: Answer: Array subscripts

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Obviously, the square bracket is an error.

    Isn't a CRLF in C, just a '\n', and identified as conceptually just one special char, (even though it's really two)?

    Adak

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, he's comparing the fgetc()-ed character to a garbage pointer value. Good for him.

    There is no loop in the program, although the problem requirements ask for one (or some equivalent). No EOF checking. Speaking of checking, he never makes sure the file was opened correctly in the first place.

    He's not checking for other space characters, either. Like ' ', '\t' and '\v'. I doubt he's heard of isspace(), but I can't be sure.

    He's using %x in printf, the hex format character. It should be %c for characters. Maybe we should tell him, eh...
    Last edited by jafet; 11-08-2006 at 05:41 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Hello,

    Thanks very much for that 'embarassed'. Can't believe I didn't spot the error with the brackets. Also, I can see where the problem with the hex conversion character lies.

    What I don't understand is why the %x error on that line also affects the printf statements further down, which are not within the loop? Does an error with one use of a command (in this case printf) really carry through to all future uses of that command, even if they are completely separate?

    Finally, I'm still getting an error on line 12 with a comparison between pointer and integer. I tried replacing the \clrf with \n but it still makes no difference. All I'm trying to do is detect if there is either a space or a new line, I don't understand where pointers come in at that point in the program?

    Thanks again.

    Hussein.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't compare strings with equality operators. You have to use something like strcmp. You might try:
    Code:
    if( c != ' ' && c != '\n' )
    {
        ... do whatever ...
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Hi. thanks for that. I finally got the hang of it, a combination of syntax errors, quotes in the wrong place and yes, trying to compare and print something that wasn't there.

    So I learnt:
    1. the correct syntax for printf with operators and escape characters
    2. The method for printing the contents of strings using a for loop
    3. How to print the integer value (ascii) of chars as well as their position in memory using %p.

    All in all, a good day wouldn't you say?

    Cheers again.

    Hussein.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  2. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  3. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM