<< < 10 11 12 13 14 15 16 17 18 19 20 > >>   Sort: Rank

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, 1225👍, 0💬

What is the most efficient way to count the number of bits which are set in an integer?
What is the most efficient way to count the number of bits which are set in an integer? Many ``bit-fiddling'' problems like this one can be sped up and streamlined using lookup tables (but see question 20.13). Here is a little function which computes the number of bits in a value, 4 bits at a time: ...
2015-02-11, 1120👍, 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, 1381👍, 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, 1253👍, 0💬

I have been replacing multiplications and divisions with shift operators, because shifting is more efficient.
I have been replacing multiplications and divisions with shift operators, because shifting is more efficient. This is an excellent example of a potentially risky and usually unnecessary optimization. Any compiler worthy of the name can replace a constant, power-of-two multiplication with a left shif...
2015-02-09, 1208👍, 0💬

People claim that optimizing compilers are good and that we no longer have to write things in assembler for speed
People claim that optimizing compilers are good and that we no longer have to write things in assembler for speed, but my compiler can't even replace i/=2 with a shift. A: Was i signed or unsigned? If it was signed, a shift is not equivalent (hint: think about the result if i is negative and odd), s...
2015-02-06, 1174👍, 0💬

How can I swap two values without using a temporary?
How can I swap two values without using a temporary? The standard hoary old assembly language programmer's trick is: a ^= b; b ^= a; a ^= b; But this sort of code has little place in modern, HLL programming. Temporary variables are essentially free, and the idiomatic code using three assignments, na...
2015-02-06, 1143👍, 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, 1165👍, 0💬

Is there a way to switch on strings?
Is there a way to switch on strings? Not directly. Sometimes, it's appropriate to use a separate function to map strings to integer codes, and then switch on those: #define CODE_APPLE 1 #define CODE_ORANGE 2 #define CODE_NONE 0 switch(classifyfunc(string)) { case CODE_APPLE: ... case CODE_ORANGE: .....
2015-02-04, 1205👍, 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, 1256👍, 0💬

Are the outer parentheses in return statements really optional?
Are the outer parentheses in return statements really optional? Yes. Long ago, in the early days of C, they were required, and just enough people learned C then, and wrote code which is still in circulation, that the notion that they might still be required is widespread. (As it happens, parentheses...
2015-02-02, 1188👍, 0💬

Why dont C comments nest?
Why don't C comments nest? How am I supposed to comment out code containing comments? Are comments legal inside quoted strings? A: C comments don't nest mostly because PL/I's comments, which C's are borrowed from, don't either. Therefore, it is usually better to ``comment out'' large sections of cod...
2015-01-30, 1199👍, 0💬

Why isnt there a numbered, multi-level break statement to break out
Why isn't there a numbered, multi-level break statement to break out of several loops at once? What am I supposed to use instead, a goto? A:First, remember why it is that break and continue exist at all--they are, in effect, ``structured gotos'' used in preference to goto (and accepted as alternativ...
2015-01-30, 1181👍, 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, 1162👍, 0💬

Does C have circular shift operators?
Does C have circular shift operators? No. (Part of the reason why is that the sizes of C's types aren't precisely defined----but a circular shift makes most sense when applied to a word of a particular known size.) You can implement a circular shift using two regular shifts and a bitwise OR: (x &...
2015-01-28, 1196👍, 0💬

Is C a great language, or what?
Is C a great language, or what? Where else could you write something like a+++++b ? A: Well, you can't meaningfully write it in C, either. The rule for lexical analysis is that at each point during a straightforward left-to-right scan, the longest possible token is determined, without regard to whet...
2015-01-26, 1216👍, 0💬

If the assignment operator were ...
If the assignment operator were :=, wouldn't it then be harder to accidentally write things like if(a = b) ? A: Yes, but it would also be just a little bit more cumbersome to type all of the assignment statements which a typical program contains. In any case, it's really too late to be worrying abou...
2015-01-26, 1194👍, 0💬

Does C have an equivalent to Pascals with statement?
Does C have an equivalent to Pascals with statement? No. The way in C to get quick and easy access to the fields of a structure is to declare a little local structure pointer variable (which, it must be admitted, is not quite as notationally convenient as a with statement and doesn't save quite as m...
2015-01-14, 1212👍, 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, 1262👍, 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, 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, 1236👍, 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, 1142👍, 0💬

I need a sort of an approximate strcmp routine ...
I need a sort of an approximate strcmp routine ... Q: I need a sort of an ``approximate'' strcmp routine, for comparing two strings for close, but not necessarily exact, equality. A:Some nice information and algorithms having to do with approximate string matching, as well as a useful bibliography, ...
2015-01-09, 1208👍, 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, 1238👍, 0💬

<< < 10 11 12 13 14 15 16 17 18 19 20 > >>   Sort: Rank