<< < 1 2 3 4 5 6 7 8 9 > >>   Sort: Date

How can I print numbers with commas separating the thousands? What about currency formatted numbers?
How can I print numbers with commas separating the thousands? What about currency formatted numbers? The functions in &lt;locale.h> begin to provide some support for these operations, but there is no standard C function for doing either task. (In Standard C, the only thing printf does in respons...
2015-11-02, 1884👍, 0💬

malloc is returning crazy pointer values ...
malloc is returning crazy pointer values, I have included the line extern void *malloc(); before I call it. malloc accepts an argument of type size_t, and size_t may be defined as unsigned long. If you are passing ints (or even unsigned ints), malloc may be receiving garbage (or similarly if you are...
2016-04-15, 1880👍, 0💬

Return a null pointer or a pointer to 0 bytes? .....
What should malloc(0) 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 ortable code must either take care not to call malloc(0), or be prepared for the possibility of a null return.
2016-04-21, 1840👍, 0💬

What's the best way to write a multi-statement macro?
What's the best way to write a multi-statement macro? The usual goal is to be able to invoke the macro as if it were an expression statement consisting of a function call: MACRO(arg1, arg2); This means that the ``caller'' will be supplying the final semicolon, so the macro body should not. The macro...
2016-02-22, 1749👍, 0💬

I need a random number generator.
I need a random number generator. The Standard C library has one: rand. The implementation on your system may not be perfect, but writing a better one isn't necessarily easy, either. Here is a portable C implementation of the ``minimal standard'' generator : #define a 16807 #define m 2147483647 #def...
2015-08-03, 1735👍, 0💬

How can I read a single character from the keyboard without waiting for the RETURN key?
How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they're typed? Alas, there is no standard or portable way to do these things in C. Concepts such as screens and keyboards are not even mentioned in the...
2015-04-29, 1719👍, 0💬

What is the difference between ......
Q: What's the difference between const MAXSIZE = 100; and #define MAXSIZE 100 A preprocessor #define gives you a true compile-time constant. In C, const gives you a run-time object which you're not supposed to try to modify; ``const'' really means ``readonly''.
2015-12-22, 1695👍, 0💬

My application depends heavily on dynamic allocation of nodes for data structures ......
My application depends heavily on dynamic allocation of nodes for data structures, and malloc/free overhead is becoming a bottleneck. What can I do ? One improvement, which is particularly attractive if all nodes are the same size, is to place unused nodes on your own free list, rather than actually...
2016-04-12, 1654👍, 0💬

Does anyone have a tool for converting old-style C programs to ANSI C?
Does anyone have a tool for converting old-style C programs to ANSI C, or vice versa, or for automatically generating prototypes? Two programs, protoize and unprotoize, convert back and forth between prototyped and ``old style'' function definitions and declarations. (These programs do not handle fu...
2015-11-25, 1620👍, 0💬

What is the correct declaration of main?
What is the correct declaration of main? There are two valid declarations: int main(void) int main(int argc, char **argv) although they can be written in a variety of ways. The second parameter may be declared char *argv[] , you can use any names for the two parameters, and you can use old-style syn...
2015-12-22, 1600👍, 0💬

Why can I only seem to malloc 640K or so?. .....
I've got 8 meg of memory in my PC. Why can I only seem to malloc 640K or so?. Under the segmented architecture of PC compatibles, it can be difficult to use more than 640K with any degree of transparency, especially under MS-DOS.
2016-04-12, 1520👍, 0💬

I have seen function declarations that look like this
I've seen function declarations that look like this: extern int func __((int, int)); What are those extra parentheses and underscores for? They're part of a trick which allows the prototype part of the function declaration to be turned off for a pre-ANSI compiler. Somewhere else is a conditional def...
2015-05-23, 1508👍, 0💬

I am allocating a large array for some numeric work ...
I'm allocating a large array for some numeric work, using the line double *array = malloc(300 * 300 * sizeof(double)); malloc isn't returning null, but the program is acting strangely, as if it's overwriting memory, or malloc isn't allocating as much as I asked for, or something.. Notice that 300 x ...
2016-04-15, 1505👍, 0💬

What is Duff's Device?
What is Duff's Device? It's a devastatingly devious way of unrolling a loop, devised by Tom Duff while he was at Lucasfilm. In its ``classic'' form, it was used to copy bytes, and looked like this: register n = (count + 7) / 8; /* count > 0 assumed */ switch (count % 8) { case 0: do { *to = *from++;...
2015-01-02, 1504👍, 0💬

I im dynamically allocating an array, like this ...
I'm dynamically allocating an array, like this: int *iarray = (int *)malloc(nints); malloc isn't returning NULL, but the code isn't working. A:malloc is a low-level, typeless allocator. It doesn't know how you're going to use the memory; all it does is to allocate as many bytes of memory as you ask ...
2016-04-06, 1493👍, 0💬

How do I swap bytes?
How do I swap bytes? V7 Unix had a swab function, but it seems to have been forgotten. A problem with explicit byte-swapping code is that you have to decide whether to call it or not, based on the byte order of the data and the byte order of the machine in use. A better solution is to define functio...
2015-02-16, 1490👍, 0💬

How can I read one character at a time, without waiting for the RETURN key
How can I read one character at a time, without waiting for the RETURN key Alas, there is no standard or portable way to do these things in C. Concepts such as screens and keyboards are not even mentioned in the Standard, which deals only with simple I/O ``streams'' of characters. Input to a compute...
2015-11-11, 1471👍, 0💬

What is the best way of making my program efficient?
What is the best way of making my program efficient? By picking good algorithms, implementing them carefully, and making sure that your program isn't doing any extra work. For example, the most microoptimized character-copying loop in the world will be beat by code which avoids having to copy charac...
2015-02-11, 1468👍, 0💬

What is a good way to implement complex numbers in C?
What is a good way to implement complex numbers in C? It is straightforward to define a simple structure and some arithmetic functions to manipulate them. C99 supports complex as a standard type. Here is a tiny example, to give you a feel for it: typedef struct { double real; double imag; } complex;...
2015-06-19, 1465👍, 0💬

What is the difference between memcpy and memmove?
What is the difference between memcpy and memmove? memmove offers guaranteed behavior if the memory regions pointed to by the source and destination arguments overlap. memcpy makes no such guarantee, and may therefore be more efficiently implementable. When in doubt, it's safer to use memmove. It se...
2015-07-16, 1460👍, 0💬

You cant use dynamically-allocated memory after you free it?
You cant use dynamically-allocated memory after you free it? No. Some early documentation for malloc stated that the contents of freed memory were ``left undisturbed,'' but this ill-advised guarantee was never universal and is not required by the C Standard. Few programmers would use the contents of...
2016-04-04, 1450👍, 0💬

I am allocating structures which contain pointers to other dynamically ....
I'm allocating structures which contain pointers to other dynamically-allocated objects. When I free a structure, do I also have to free each subsidiary pointer? Yes. malloc knows nothing about structure declarations or about the contents of allocated memory; it especially does not know whether allo...
2016-03-31, 1441👍, 0💬

y program is crashing, apparently somewhere down inside malloc ....
My program is crashing, apparently somewhere down inside malloc, but I can't see anything wrong with it. Is there a bug in malloc? It is unfortunately very easy to corrupt malloc's internal data structures, and the resulting problems can be stubborn. The most common source of problems is writing mor...
2016-04-06, 1436👍, 0💬

How do I round numbers?
How do I round numbers? The simplest and most straightforward way is with code like (int)(x + 0.5) C's floating to integer conversion truncates (discards) the fractional part, so adding 0.5 before truncating arranges that fractions &gt;= 0.5 will be rounded up. (This technique won't work properl...
2015-06-26, 1436👍, 0💬

<< < 1 2 3 4 5 6 7 8 9 > >>   Sort: Date