Question:
Is it possible to add our own errors and warnings in C just like user-defined functions?
1970-01-01 00:00:00 UTC
Is it possible to add our own errors and warnings in C just like user-defined functions?
Five answers:
?
2016-10-31 12:10:42 UTC
In a typical runtime environment, while a C/C++ application gets invoked, the runtime library is initially began. it is the place international and static variables get initialized. After that, the runtime library will call the perfect() function of the C/C++ executable. The runtime library expects significant() to be there and if no longer, will errors out. So a extra precise answer is that the programmer needs to stipulate significant() by writing a significant() function (person-defined), besides the shown fact that it must be defined in a undeniable way (with argc and argv arguments) so the runtime library can call it (pre-defined). It in basic terms relies upon on the context of the place your question is coming from. Sorry for being imprecise, however the respond relies upon on the area from the place the question is being asked of.
Cronin
2013-03-19 22:02:42 UTC
This is where C++ comes in where you can make your own exceptions.

You can also replace the standard array with an ADT that handles errors, resizing, etc. whatever you can imagine.
husoski
2013-03-19 21:39:16 UTC
You can perform argument checking at run time, and you have a very limited way to do compile time checking using preprocessor #if and #error directives. For this sort of problem, in the C language, only runtime checking is practical, since the sizeof operator isn't legal in a #if expression.



One easy way to add internal checks is by using the assert() macro. Include at the top and then your loop could be something like:

for (i=0; i<=100; ++i)

{

assert(i>=0 && i < (sizeof num)/(sizeof *num));

num[i] = i;

}



The macro ordinarily generates inline code to generate a runtime error with a console message if the condition is false. However if the name NDEBUG has been defined then no code is generated. This is useful taking out all the debug overhead when compiling for a release.



Checks that should be done even "in production" should not be coded with assert, though, but with explicit if statements or your own macro defintions. Take a look at the assert.h source file to get an idea how to create such a macro. (You can't directly copy that code...copyright rules and all...but you can learn from it!)
2013-03-19 21:36:12 UTC
You can't add to the C language unless you have source code for a compiler and then you could very likely only make changes that affect how your own code is compiled.



Of course you can change your own code to address error checking, but that falls under user defined routines, functions, coding whatever.





You should already have given me best answer about your pointer question.



To continue stupid analogies.



C gives you a cat but no kitty litter.
Unca Alby
2013-03-19 21:26:50 UTC
Sounds like a great idea. What you want, at least in this particular example, is a run-time bounds check, such as what is found in languages like Pascal and Java.



Maybe later, after you become an expert yourself, you can work on such a project. Publish it to the Open Source community.



But no, right now, nothing of the sort exists. C is notorious for giving you enough rope to shoot yourself in the foot.



You'll just have to learn what not to do like the rest of us.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...