#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Error tring to user printf in conditional compilation


    header file
    Code:
    #if !defined (H_INCL)
        #define H_INCL
        #include <stdio.h>
        printf("including header\n");
    #endif
    main file
    Code:
    #include "1.h"
    
    int main()
    {
        return 0;
    }
    The compiler spits the error message
    expected declaration specifiers or ... before
    What I've researched:
    1. declaration = declaration specifier (data types, storage classes, etc.) + declarator (names)
    2. trying to execute printf is an invalid operation
    3. main begins the program execution

    My questions:
    1. I know what I'm doing in the header file is to do printf before calling main, which is an invalid action. But why does the compiler expect a declaration specifier? Is it that the compiler is interpreting the call to printf as a function prototype?
    2. Am I correct in saying that the reason for #2 above is that I would be trying to do something before the program has even started?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Executable code belongs inside of functions. Executable code is only run from within functions, starting with main(). Bare code is verboten!

    You did something totally unexpected and the compiler is trying to make sense of it. It was expecting some kind of declaration there, but got something that it couldn't recognize and so offered the best that it could. In fact, it was the programmer who wrote that part of the compiler who tried to anticipate everything that could go wrong in order to identify it. Obviously, that programmer couldn't anticipate everything.

    Verily it is written:
    Originally Posted by Douglas Adams
    A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    and
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Refer to the definition of C's syntax; eg at http://www.cs.wmich.edu/~gupta/teach...aur%20form.htm. A declaration specifier is defined thus:
    Code:
    <translation-unit> ::= {<external-declaration>}*
    
    <external-declaration> ::= <function-definition>
                             | <declaration>
    
    <function-definition> ::= {<declaration-specifier>}* <declarator> {<declaration>}* <compound-statement>
    
    <declaration-specifier> ::= <storage-class-specifier>
                              | <type-specifier>
                              | <type-qualifier>
    
    <storage-class-specifier> ::= auto
                                | register
                                | static
                                | extern
                                | typedef
    
    <type-specifier> ::= void
                       | char
                       | short
                       | int
                       | long
                       | float
                       | double
                       | signed
                       | unsigned
                       | <struct-or-union-specifier>
                       | <enum-specifier>
                       | <typedef-name>
    
    <direct-declarator> ::= <identifier>
                          | ( <declarator> )
                          | <direct-declarator> [ {<constant-expression>}? ]
                          | <direct-declarator> ( <parameter-type-list> )
                          | <direct-declarator> ( {<identifier>}* )
    That is in Backus-Naur Form (BNF) and is part of the specification for C syntax. That tells you what the compiler was expecting when it instead encountered something unexpected. So the best that the compiler can say is something like (I was expecting one thing and got something different) and raise a syntax error.

    So the compiler takes the output of the preprocessor and scans and tokenizes it. Then it analyzes the syntax of the tokens as it builds a data structure for translating the source code into object code. That is all part of parsing. If the syntax is not correct, then the compiler can proceed no further, though most compilers will perform error recovery in order to analyze as much of the syntax as possible. That gives you a list of syntax errors to correct rather than just one and forcing you to recompile repeatedly to get each error.

    A down-side of error recovery is that one error can create many more. Eg, in one of my Pascal programs, it compiled correctly, but when I made one single change there were suddenly over 100 errors, all of them caused by one single error throwing the compiler off. Thus if you get a lot of errors, address the first ones first, which may clean up many of the subsequenct errors -- plus, the subsequent errors caused by an earlier on won't make any sense (eg, a type declaration failed, so all references to that type will also be wrong, so if you correct that first failure then those other errors will go away).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Executable code is only run from within functions, starting with main().
    Thanks, got that.

    That tells you what the compiler was expecting when it instead encountered something unexpected.
    So the compiler got a bare call to printf, which is unexpected, and it told me what was expected instead, a declaration specifier.

    Thus if you get a lot of errors, address the first ones first
    I'll remember that.
    I'll also read about parsing and tokenizing, mentioned in the paragraph before.

IMN logo majestic logo threadwatch logo seochat tools logo