<< < 8 9 10 11 12 13 14 15 16 17 > >>   Sort: Rank

But I cant use all these nonstandard, system-dependent functions, because my program has to be ANSI compatible!
But I cant use all these nonstandard, system-dependent functions, because my program has to be ANSI compatible! You're out of luck. Either you misunderstood your requirement, or it's an impossible one to meet. ANSI/ISO Standard C simply does not define ways of doing these things; it is a language st...
2015-02-27, 1182👍, 0💬

Why isnt any of this standardized in C? Any real program has to do some of these things.
Why isnt any of this standardized in C? Any real program has to do some of these things. Actually, some standardization has occurred along the way. In the beginning, C did not have a standard library at all; programmers always had to ``roll their own'' utility routines. After several abortive attemp...
2015-02-27, 1197👍, 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, 1152👍, 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, 1245👍, 0💬

What is the right way to use errno?
What is the right way to use errno? In general, you should detect errors by checking return values, and use errno only to distinguish among the various causes of an error, such as ``File not found'' or ``Permission denied''. (Typically, you use perror or strerror to print these discriminating error ...
2015-02-23, 1169👍, 0💬

How can I write data files which can be read on other machines with different word size, byte order, or floating point formats?
How can I write data files which can be read on other machines with different word size, byte order, or floating point formats? The most portable solution is to use text files (usually ASCII), written with fprintf and read with fscanf or the like. (Similar advice also applies to network protocols.) ...
2015-02-23, 1197👍, 0💬

If I have a char * variable pointing to the name of a function ...
If I have a char * variable pointing to the name of a function, how can I call that function? Code like extern int func(int, int); char *funcname = "func"; int r = (*funcname)(1, 2); or r = (*(int (*)(int, int))funcname)(1, 2); doesn't seem to work. By the time a program is running, information abou...
2015-02-20, 1276👍, 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, 1255👍, 0💬

How can I determine whether a machines byte order is big-endian or little-endian?
How can I determine whether a machines byte order is big-endian or little-endian? The usual techniques are to use a pointer: int x = 1; if(*(char *)&amp;x == 1) printf("little-endian\n"); else printf("big-endian\n"); or a union: union { int i; char c[sizeof(int)]; } x; x.i = 1; if(x.c[0] == 1) p...
2015-02-16, 1339👍, 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, 1403👍, 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, 1164👍, 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, 1228👍, 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, 1126👍, 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, 1384👍, 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, 1257👍, 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, 1212👍, 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, 1176👍, 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, 1145👍, 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, 1167👍, 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, 1209👍, 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, 1262👍, 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, 1192👍, 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, 1203👍, 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, 1183👍, 0💬

<< < 8 9 10 11 12 13 14 15 16 17 > >>   Sort: Rank