Thread: How to use the #error

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95

    How to use the #error

    Hello,
    I'm trying to use #error such that when a function computes something I don't want it 'throws' an error. I've read about this directive but didn't find how to make it work.

    Correct me:
    Code:
    #include <stdio.h>
    #error Array Out of bounds
    
    int foo(){
     #error Array out of Bounds
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That doesn't work. #error is a preprocessor directive. So when the preprocessor sees it, it errors, no matter where in the text it is.
    The compiler does not even have a chance to run.

    It seems to me that what you are looking for is the C++ feature exceptions. They can be thrown to raise an error if something goes wrong runtime.
    C++ also features a compile-time assert which triggers an error during compile time if something goes wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    hmm..but I'm to use c and want to use #error to the extent it's permissible. For some reason however when I try:
    Code:
     			#if qNo < 1 || qNo > 4
    				#error  notFound
    			#endif /* bug with finding the hole */
    I get a compilation error. If I use #warning instead I don't.
    (I've tried, #error, #error "hello", but all with no success).

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Please read what Elysia is saying. You can only use #error during pre-processor logistics for helping in creating portable code. If you are needing to check for assertions during run time, then I would recommend that you read up on assert().

    Have you even google'd around for how to effectively use #error? There are many good links out there on this.

    Something on the use of assert()
    http://www.cprogramming.com/tips/sho...ount=30&page=0

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note also that assert will prompt something like "assertion failed", pretty much halting the program and making recovery impossible. But that is the intended feature of assert - to help catch debug errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe you're looking for the Boost Static Assertions?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by cpjust View Post
    Maybe you're looking for the Boost Static Assertions?
    Not if he's using C

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by bithub View Post
    Not if he's using C
    Doh! Forgot which forum I was on.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM