<< < 158 159 160 161 162 163 164 165 166 167 168 > >>   Sort: Date

What is the right type to use for Boolean values in C? ....
What is the right type to use for Boolean values in C? Is there a standard type? Should I use #defines or enums for the true and false values? Traditionally, C did not provide a standard Boolean type, partly and partly to allow the programmer to make the appropriate space/time tradeoff. (Using an in...
2016-03-02, 1309👍, 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, 1305👍, 0💬

How do I set variables to, or test for IEEE NaN?
How do I set variables to, or test for IEEE NaN (``Not a Number'') and other special values? Many systems with high-quality IEEE floating-point implementations provide facilities (e.g. predefined constants, and functions like isnan(), either as nonstandard extensions in &lt;math.h> or perhaps in...
2015-06-21, 1305👍, 0💬

Suggesting that there can be 62 seconds in a minute?
Suggesting that there can be 62 seconds in a minute? Q: Why can tm_sec in the tm structure range from 0 to 61, suggesting that there can be 62 seconds in a minute? A: That's actually a buglet in the Standard. There can be 61 seconds in a minute during a leap second. It's possible for there to be two...
2015-01-05, 1305👍, 0💬

Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)?
Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)? No. The switch statement was originally designed to be quite simple for the compiler to translate, therefore case labels are limited to single, constant, integral expressions. You can attach several case labels t...
2015-02-02, 1299👍, 0💬

Why doesnt C have nested functions?
Why doesnt C have nested functions? It's not trivial to implement nested functions such that they have the proper access to local variables in the containing function(s), so they were deliberately left out of C as a simplification. (gcc does allow them, as an extension.) For many potential uses of n...
2015-01-14, 1299👍, 0💬

I am trying to use the ANSI stringizing preprocessing operator ...
I'm trying to use the ANSI ``stringizing'' preprocessing operator `#' to insert the value of a symbolic constant into a message, but it keeps stringizing the macro's name rather than its value. It turns out that the definition of # says that it's supposed to stringize a macro argument immediately, w...
2015-12-16, 1298👍, 0💬

How can I manipulate individual bits??
How can I manipulate individual bits?? Bit manipulation is straightforward in C, and commonly done. To extract (test) a bit, use the bitwise AND (&amp;) operator, along with a bit mask representing the bit(s) you're interested in: value &amp; 0x04 To set a bit, use the bitwise OR (| or |=) o...
2015-02-20, 1287👍, 0💬

Are pointers really faster than arrays?
Are pointers really faster than arrays? How much do function calls slow things down? Is ++i faster than i = i + 1? Precise answers to these and many similar questions depend of course on the processor and compiler in use. If you simply must know, you'll have to time test programs carefully. (Often t...
2015-02-09, 1283👍, 0💬

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, 1281👍, 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, 1280👍, 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, 1280👍, 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, 1278👍, 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, 1278👍, 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, 1278👍, 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, 1275👍, 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, 1275👍, 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, 1274👍, 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, 1270👍, 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, 1270👍, 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, 1268👍, 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, 1266👍, 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, 1266👍, 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, 1265👍, 0💬

<< < 158 159 160 161 162 163 164 165 166 167 168 > >>   Sort: Date