+ Post New Thread
Results 1 to 7 of 7
  1. #1
    Junior Member level 2
    Points: 645, Level: 5

    Join Date
    Feb 2013
    Location
    India
    Posts
    20
    Helped
    0 / 0
    Points
    645
    Level
    5

    Generating compile time error in C

    How can I evaluate any expression during compile time and generate compile time error in C?
    Say I have a function
    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    
    void foo(int number){
        if(number < 10){
            //Do this;
        }
        else
            #error "Number is greater than 9 and this is compiler generated error";
    }

    •   AltAdvertisment

        
       

  2. #2
    Advanced Member level 3
    Points: 6,497, Level: 19
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    834
    Helped
    342 / 342
    Points
    6,497
    Level
    19

    Re: Generating compile time error in C

    "number" is a function argument, and the compiler doesn't know the value at compile time.
    Values known at compile time can be tested like this:

    Code:
    #include <stdio.h>
    
    #define MY_NUMBER 11
    
    #if MY_NUMBER >= 10
    #error MY_NUMBER is out of range
    #endif
    
    int main(int argc, char *argv[]){
      printf("hello, world!\n");
    }


    1 members found this post helpful.

  3. #3
    Super Moderator
    Points: 74,991, Level: 66
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,289
    Helped
    4077 / 4077
    Points
    74,991
    Level
    66

    Re: Generating compile time error in C

    It depends on the compiler, which you have not told us.

    Things to try:
    #pragma message ("print this while compiling"); -also try without parentheses.
    #warning ("print this while compiling");
    #error This is a compiler error message

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  4. #4
    Junior Member level 2
    Points: 645, Level: 5

    Join Date
    Feb 2013
    Location
    India
    Posts
    20
    Helped
    0 / 0
    Points
    645
    Level
    5

    Re: Generating compile time error in C

    I am using AVR Studio 5, don't exactly know which compiler is used in it.Actually I am passing a value to a function which I want to put it in a header file later. So for the reliability, I just wanted to put some error message there so that I could get any error or warning during compile time for any unwanted value.



  5. #5
    Advanced Member level 3
    Points: 7,037, Level: 19

    Join Date
    Jan 2015
    Posts
    985
    Helped
    312 / 312
    Points
    7,037
    Level
    19

    Re: Generating compile time error in C

    You are confusing compile-time and run-time error checking.
    If you want to make sure that the parameter passed to a function is within a certain range then that can only be done at run-time.
    However, if you pass a constant (for preprocessor value) as the parameter in the call to the function, then that might be checked at compile time.
    The test comes as: at what time to you know what the actual value is given the context of the check. In the code in your first post, you don't know the value within the function at compile time - that is why you have the 'if' statement and the code that is generated by it that will be run when the function is called - but you do at run-time. Therefore this is an example of run-time error checking. If you want to tell the user that a bad value has been passed then you need to use something like a print statement (if the user can fix the problem) or a stack dump (if it is an internal coding error).
    Susan


    1 members found this post helpful.

  6. #6
    Advanced Member level 5
    Points: 15,047, Level: 29

    Join Date
    Nov 2012
    Posts
    2,783
    Helped
    652 / 652
    Points
    15,047
    Level
    29

    Re: Generating compile time error in C

    You are confusing compile-time and run-time error checking.
    Absolutely correct. Further, the error message will only add confusion.

    Compile time errors are produced during compilation and the user will be confused to get a error during run time that says compiler error!!

    This error cannot be generated by the compiler.


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  7. #7
    Super Moderator
    Points: 28,088, Level: 40
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,390
    Helped
    1058 / 1058
    Points
    28,088
    Level
    40
    Blog Entries
    6

    Re: Generating compile time error in C

    What you want is not properly error generation, but rather something better known as debug messages during runtime, (as Aussie remarked) which can be either logged to a local file or sent via a serial console monitor, from whose outcomes you often classify them in various levels, such as INFO, WANRING and ERROR, taking different actions depending on each case, as for example to abort the program, or perform some specific treatment routine.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



--[[ ]]--