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

Why doesn't C have an exponentiation operator?
Why doesn't C have an exponentiation operator? One reason is probably that few processors have a built-in exponentiation instruction. C has a pow function (declared in &lt;math.h>) for performing exponentiation, although explicit multiplication is usually better for small positive integral expon...
2015-06-24, 1242👍, 0💬

What is a good data structure to use for storing lines of text?
What's a good data structure to use for storing lines of text? I started to use fixed-size arrays of arrays of char, but they're just too restrictive. One good way of doing this is with a pointer (simulating an array) to a set of pointers (each simulating an array) of char. This data structure is so...
2015-02-25, 1242👍, 0💬

How can printf use f for type double, if scanf requires lf?
Someone told me it was wrong to use %lf with printf. How can printf use %f for type double, if scanf requires %lf? It's true that printf's %f specifier works with both float and double arguments Due to the ``default argument promotions'' (which apply in variable-length argument lists values of type ...
2015-11-06, 1240👍, 0💬

How can I open files mentioned on the command line, and parse option flags?
How can I open files mentioned on the command line, and parse option flags? Here is a skeleton which implements a traditional Unix-style argv parse, handling option flags beginning with -, and optional filenames. (The two flags accepted by this example are -a and -b; -b takes an argument.) #include ...
2015-03-11, 1239👍, 0💬

What is assert and when would I use it?
What is assert and when would I use it? It is a macro, defined in &lt;assert.h>, for testing ``assertions''. An assertion essentially documents an assumption being made by the programmer, an assumption which, if violated, would indicate a serious programming error. For example, a function which ...
2015-01-12, 1239👍, 0💬

What is hashing?
What is hashing? Hashing is the process of mapping strings to integers, usually in a relatively small range. A ``hash function'' maps a string (or some other data structure) to a bounded number (the ``hash bucket'') which can more easily be used as an index in an array, or for performing repeated co...
2015-01-07, 1239👍, 0💬

So can I query the malloc package to find out how big an allocated block is?
So can I query the malloc package to find out how big an allocated block is? Unfortunately, there is no standard or portable way. (Some compilers provide nonstandard extensions.) If you need to know, you'll have to keep track of it yourself.
2016-03-24, 1238👍, 0💬

How can I get the numeric value corresponding to a character? ....
How can I get the numeric value (i.e. ASCII or other character set code) corresponding to a character, or vice versa? In C, characters are represented by small integers corresponding to their values in the machine's character set. Therefore, you don't need a conversion function: if you have the char...
2016-03-07, 1238👍, 0💬

The predefined constant M_PI seems to be missing from my machines copy of math.h.
The predefined constant M_PI seems to be missing from my machines copy of math.h. That constant (which is apparently supposed to be the value of pi, accurate to the machine's precision), is not standard; in fact a standard-conforming copy of should not #define a symbol M_PI. If you need pi, you'll h...
2015-06-24, 1238👍, 0💬

How can I discover how many arguments a function was actually called with?
How can I discover how many arguments a function was actually called with? This information is not available to a portable program. Some old systems provided a nonstandard nargs function, but its use was always questionable, since it typically returned the number of words passed, not the number of a...
2015-06-10, 1238👍, 0💬

How can I call FORTRAN?
How can I call FORTRAN? Q: How can I call FORTRAN (C++, BASIC, Pascal, Ada, LISP) functions from C? (And vice versa?) A: The answer is entirely dependent on the machine and the specific calling sequences of the various compilers in use, and may not be possible at all. Read your compiler documentatio...
2015-01-12, 1237👍, 0💬

How can I recover the file name given an open stream or file descriptor?
How can I recover the file name given an open stream or file descriptor? This problem is, in general, insoluble. Under Unix, for instance, a scan of the entire disk (perhaps involving special permissions) would theoretically be required, and would fail if the descriptor were connected to a pipe or r...
2015-04-10, 1236👍, 0💬

But what about main's third argument, envp?
But what about main's third argument, envp? It's a non-standard (though common) extension. If you really need to access the environment in ways beyond what the standard getenv function provides, though, the global variable environ is probably a better avenue (though it's equally non-standard).
2015-12-20, 1233👍, 0💬

How can I include expansions of the macros .....
How can I include expansions of the __FILE__ and __LINE__ macros in a general-purpose debugging macro? One solution involves writing your debug macro in terms of a varargs function , and an auxiliary function which stashes the values of __FILE__ and __LINE__ away in static variables, as in: #include...
2016-01-15, 1232👍, 0💬

How can I display a percentage-done indication that updates itself in place, or show one of those twirling baton progress indica
How can I display a percentage-done indication that updates itself in place, or show one of those twirling baton progress indicators? These simple things, at least, you can do fairly portably. Printing the character '\r' will usually give you a carriage return without a line feed, so that you can ov...
2015-04-27, 1231👍, 0💬

How can I direct output to the printer?
How can I direct output to the printer? Under Unix, either use popen to write to the lp or lpr program, or perhaps open a special file like /dev/lp. Under MS-DOS, write to the (nonstandard) predefined stdio stream stdprn, or open the special files PRN or LPT1. Under some circumstances, another (and ...
2015-04-22, 1228👍, 0💬

What is the difference between these initializations? ....
What is the difference between these initializations? char a[] = "string literal"; char *p = "string literal"; My program crashes if I try to assign a new value to p[i]. A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways: 1. As the in...
2016-03-09, 1227👍, 0💬

fopen isnt letting me open files like "$HOME/.profile" and "~/.myrcfile".
fopen isn't letting me open files like "$HOME/.profile" and "~/.myrcfile". Under Unix, at least, environment variables like $HOME, along with the home-directory notation involving the ~ character, are expanded by the shell, and there's no mechanism to perform these expansions automatically when you ...
2015-04-06, 1227👍, 0💬

Can I use base-2 constants (something like 0b101010)? Is there a printf format for binary?
Can I use base-2 constants (something like 0b101010)? Is there a printf format for binary? No, on both counts, although there are various preprocessor tricks you can try. You can convert base-2 string representations to integers with strtol. If you need to print numbers out in base 2, .
2015-02-13, 1226👍, 0💬

This program runs perfectly on one machine ...
This program runs perfectly on one machine, but I get weird results on another. Stranger still, adding or removing a debugging printout changes the symptoms... Lots of things could be going wrong; here are a few of the more common things to check: * uninitialized local variables integer overflow, es...
2015-05-27, 1225👍, 0💬

I am still getting errors due to library functions being undefined
I'm still getting errors due to library functions being undefined, even though I'm explicitly requesting the right libraries while linking. Many linkers make one pass over the list of object files and libraries you specify, and extract from libraries only those modules which satisfy references which...
2015-07-14, 1223👍, 0💬

What is the difference between calloc and malloc? ....
What's the difference between calloc and malloc? Which should I use? Is it safe to take advantage of calloc's zero-filling? Does free work on memory allocated with calloc, or do you need a cfree? calloc(m, n) is essentially equivalent to p = malloc(m * n); memset(p, 0, m * n); There is no important ...
2016-03-16, 1220👍, 0💬

How can I find out how much memory is available?
How can I find out how much memory is available? Your operating system may provide a routine which returns this information, but it's quite system-dependent. (Also, the number may vary over time.) If you're trying to predict whether you'll be able to allocate a certain amount of memory, just try it-...
2015-03-30, 1220👍, 0💬

I believe that declaring void main) cant fail ...
I believe that declaring void main() can't fail, since I'm calling exit instead of returning, and anyway my operating system ignores a program's exit/return status. It doesn't matter whether main returns or not, or whether anyone looks at the status; the problem is that when main is misdeclared, its...
2015-12-18, 1218👍, 0💬

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