<< < 161 162 163 164 165 166 167 168 169 170 171 > >>   Sort: Date

How can I handle floating-point exceptions gracefully?
How can I handle floating-point exceptions gracefully? On many systems, you can define a function matherr which will be called when there are certain floating-point errors, such as errors in the math routines in &lt;math.h>. You may also be able to use signal to catch SIGFPE
2015-03-04, 1105👍, 0💬

How can I convert integers to binary or hexadecimal?
How can I convert integers to binary or hexadecimal? Make sure you really know what you're asking. Integers are stored internally in binary, although for most purposes it is not incorrect to think of them as being in octal, decimal, or hexadecimal, whichever is convenient. The base in which a number...
2015-02-13, 1105👍, 0💬

I am sure I've got the trig functions declared correctly, but they are still giving me wrong answers.
I am sure I've got the trig functions declared correctly, but they are still giving me wrong answers. You weren't handing them angles in degrees, were you? C's trig functions (like FORTRAN's and most other languages) accept angles in radians. The conversion from degrees to radians is simple enough: ...
2015-06-29, 1104👍, 0💬

How can I write a function which takes a variable number of arguments
How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)? In general, you cannot. Ideally, you should provide a version of that other function which accepts a va_list pointer. Suppose you want to write a...
2015-06-05, 1104👍, 0💬

How can I write a generic macro to swap two values?
How can I write a generic macro to swap two values? There is no good answer to this question. If the values are integers, a well-known trick using exclusive-OR could perhaps be used, but it will not work for floating-point values or pointers, or if the two values are the same variable. If the macro ...
2016-02-24, 1103👍, 0💬

I a'm getting strange syntax errors inside lines I have ifdeffed out.
I a'm getting strange syntax errors inside lines I have ifdeffed out. Under ANSI C, the text inside a ``turned off'' #if, #ifdef, or #ifndef must still consist of ``valid preprocessing tokens.'' This means that the characters " and ' must each be paired just as in real C code, and the pairs mustn't ...
2016-01-29, 1103👍, 0💬

Here is a neat trick for checking whether two strings are equal
Q: Here's a neat trick for checking whether two strings are equal: if(!strcmp(s1, s2)) Is this good style? It is not particularly good style, although it is a popular idiom. The test succeeds if the two strings are equal, but the use of ! (``not'') suggests that it tests for inequality. Another opti...
2015-05-15, 1103👍, 0💬

How do I read the arrow keys? What about function keys?
How do I read the arrow keys? What about function keys? Terminfo, some versions of termcap, and some versions of curses have support for these non-ASCII keys. Typically, a special key sends a multicharacter sequence (usually beginning with ESC, '\033'); parsing these can be tricky. (curses will do t...
2015-04-24, 1102👍, 0💬

What should malloc0 do? Return a null pointer or a pointer to 0 bytes?
What should malloc0 do? Return a null pointer or a pointer to 0 bytes? The ANSI/ISO Standard says that it may do either; the behavior is implementation-defined Portable code must either take care not to call malloc(0), or be prepared for the possibility of a null return.
2015-12-04, 1100👍, 0💬

Which is more efficient, a switch statement or an if else chain?
Which is more efficient, a switch statement or an if else chain? The differences, if any, are likely to be slight. The switch statement was designed to be efficiently implementable, though the compiler may choose to use the equivalent of an if/else chain (as opposed to a compact jump table) if the c...
2015-02-04, 1100👍, 0💬

Having dynamically allocated an array , can I change its size?
Having dynamically allocated an array , can I change its size? Yes. This is exactly what realloc is for. Given a region of malloced memory its size can be changed using code like: dynarray = realloc(dynarray, 20 * sizeof(int)); Note that realloc may not always be able to enlarge memory regions in-pl...
2016-03-21, 1098👍, 0💬

So what could go wrong? Are there really any systems where void main doesnt work?
So what could go wrong? Are there really any systems where void main doesnt work? It has been reported that programs using void main() and compiled using BC++ 4.5 can crash. Some compilers (including DEC C V4.1 and gcc with certain warnings enabled) will complain about void main().
2015-12-18, 1098👍, 0💬

There seem to be a few missing operators ..
There seem to be a few missing operators, like ^^, &amp;&amp;=, and -&gt;=. A:A logical exclusive-or operator (hypothetically ``^^'') would be nice, but it couldn't possibly have short-circuiting behavior analogous to &amp;&amp; and || Similarly, it's not clear how short-circuiti...
2015-01-28, 1098👍, 0💬

I am including the right header file for the library function I am using, but the linker keeps saying its undefined.
I am including the right header file for the library function I am using, but the linker keeps saying its undefined. In the general case of calling code in an external library, using #include to pull in the right header file(s) is only half of the story; you also have to tell the linker to search th...
2016-02-10, 1097👍, 0💬

How can I allocate arrays or structures bigger than 64K?
How can I allocate arrays or structures bigger than 64K? A reasonable computer ought to give you transparent access to all available memory. If you're not so lucky, you'll either have to rethink your program's use of memory, or use various system-specific techniques. 64K is (still) a pretty big chun...
2015-03-30, 1097👍, 0💬

How can I return multiple values from a function?
How can I return multiple values from a function? There are several ways of doing this. (These examples show hypothetical polar-to-rectangular coordinate conversion functions, which must return both an x and a y coordinate.) 1. Pass pointers to several locations which the function can fill in: #incl...
2015-02-25, 1095👍, 0💬

When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999? Most computers use base 2 for floating-point numbers as well as for integers, and just as for base 10, not all fractions are representable exactly in base 2. It's well-known that in base 10, a fraction like 1/3 = 0.333...
2015-07-03, 1094👍, 0💬

How can my program discover the complete pathname to the executable from which it was invoked?
How can my program discover the complete pathname to the executable from which it was invoked? argv[0] may contain all or part of the pathname, or it may contain nothing. You may be able to duplicate the command language interpreter's search path logic to locate the executable if the name in argv[0]...
2015-03-13, 1094👍, 0💬

How can I automatically locate a programs configuration files in the same directory as the executable?
How can I automatically locate a programs configuration files in the same directory as the executable? It's hard, in general; Even if you can figure out a workable way to do it, you might want to consider making the program's auxiliary (library) directory configurable, perhaps with an environment va...
2015-03-13, 1094👍, 0💬

What are the differences between C and CPP?
What are the differences between C and CPP? Q: Is C++ a superset of C? What are the differences between C and C++? Can I use a C++ compiler to compile C code? A: C++ was derived from C, and is largely based on it, but there are some legal C constructs which are not legal C++. Conversely, ANSI C inhe...
2015-01-10, 1093👍, 0💬

Can I declare main as void, to shut off these annoying ....
Can I declare main as void, to shut off these annoying ``main returns no value'' messages? No. main must be declared as returning an int, and as taking either zero or two arguments, of the appropriate types. If you're calling exit() but still getting warnings, you may have to insert a redundant retu...
2015-12-20, 1092👍, 0💬

I cant get va_arg to pull in an argument of type pointer-to-function.
I cant get va_arg to pull in an argument of type pointer-to-function. Try using a typedef for the function pointer type. The type-rewriting games which the va_arg macro typically plays are stymied by overly-complicated types such as pointer-to-function. To illustrate, a simplified implementation of ...
2015-06-05, 1092👍, 0💬

If NULL and 0 are equivalent as null pointer constants, which should I use?
If NULL and 0 are equivalent as null pointer constants, which should I use? Many programmers believe that NULL should be used in all pointer contexts, as a reminder that the value is to be thought of as a pointer. Others feel that the confusion surrounding NULL and 0 is only compounded by hiding 0 b...
2015-05-11, 1092👍, 0💬

How do I copy files?
How do I copy files? Either use system() to invoke your operating system's copy utility, or open the source and destination files (using fopen or some lower-level file-opening system call), read characters or blocks of characters from the source file, and write them to the destination file. Here is ...
2015-04-08, 1092👍, 0💬

<< < 161 162 163 164 165 166 167 168 169 170 171 > >>   Sort: Date