<< < 160 161 162 163 164 165 166 167 168 169 170 > >>   Sort: Date

What does it really mean for a program to be legal or `valid or ``conforming?
What does it really mean for a program to be legal or `valid or ``conforming? Simply stated, the Standard talks about three kinds of conformance: conforming programs, strictly conforming programs, and conforming implementations. A conforming program is one that is accepted by a conforming implementa...
2015-11-23, 1243👍, 0💬

Wy compiler is rejecting the simplest possible test programs ...
My compiler is rejecting the simplest possible test programs, with all kinds of syntax errors. It's complaining about the first line of main(int argc, char **argv) { return 0; } Perhaps it is a pre-ANSI compiler, unable to accept function prototypes and the like. If you don't have access to an ANSI ...
2015-12-02, 1242👍, 0💬

How can I handle floating-point exceptions gracefully?
How can I handle floating-point exceptions gracefully? On many systems, you can define a function matherr which will be called when there are certain floating-point errors, such as errors in the math routines in . You may also be able to use signal to catch SIGFPE.
2015-06-21, 1241👍, 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, 1241👍, 0💬

Why do some versions of toupper act strangely if given an upper-case letter?
Why do some versions of toupper act strangely if given an upper-case letter? Why does some code call islower before toupper? In earlier times, toupper was a function-like preprocessor macro and was defined to work only on lower-case letters; it misbehaved if applied to digits, punctuation, or letter...
2015-08-17, 1239👍, 0💬

Here is a neat trick for checking whether two strings are equal
Q: Here's a neat trick for checking whether two strings are equal: if(!strcmp(s1, s2)) Is this good style? It is not particularly good style, although it is a popular idiom. The test succeeds if the two strings are equal, but the use of ! (``not'') suggests that it tests for inequality. Another opti...
2015-05-15, 1239👍, 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, 1239👍, 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, 1237👍, 0💬

Why doesnt the call scanf work?
Why doesn't the call scanf("%d", i) work? The arguments you pass to scanf must always be pointers: for each value converted, scanf ``returns'' it by filling in one of the locations you've passed pointers to. To fix the fragment above, change it to scanf("%d", &i) .
2015-11-02, 1235👍, 0💬

How can I specify a variable width in a scanf format string?
How can I specify a variable width in a scanf format string? You can't; an asterisk in a scanf format string means to suppress assignment. You may be able to use ANSI stringizing and string concatenation to construct a constant format specifier based on a preprocessor macro containing the desired wi...
2015-10-26, 1235👍, 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, 1234👍, 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-12-07, 1232👍, 0💬

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

I am sure I've got the trig functions declared correctly, but they are still giving me wrong answers.
I am sure I've got the trig functions declared correctly, but they are still giving me wrong answers. You weren't handing them angles in degrees, were you? C's trig functions (like FORTRAN's and most other languages) accept angles in radians. The conversion from degrees to radians is simple enough: ...
2015-06-29, 1232👍, 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, 1232👍, 0💬

Is it legal to pass a null pointer as the first argument to realloc? Why would you want to?
Is it legal to pass a null pointer as the first argument to realloc? Why would you want to? ANSI C sanctions this usage (and the related realloc(..., 0), which frees), although several earlier implementations do not support it, so it may not be fully portable. Passing an initially-null pointer to re...
2016-03-16, 1231👍, 0💬

People seem to make a point of distinguishing between ...
People seem to make a point of distinguishing between implementation-defined, unspecified, and undefined behavior. What do these mean? First of all, all three of these represent areas in which the C Standard does not specify exactly what a particular construct, or a program which uses it, must do. T...
2015-11-23, 1230👍, 0💬

Now I am trying to sort an array of structures with qsort ...
Now I'm trying to sort an array of structures with qsort. My comparison function takes pointers to structures, but the compiler complains that the function is of the wrong type for qsort. How can I cast the function pointer to shut off the warning? The conversions must be in the comparison function,...
2015-08-10, 1230👍, 0💬

Can I use an ifdef in a #define line ...
Can I use an #ifdef in a #define line, to define something two different ways, like this? #define a b \ #ifdef whatever c d #else e f g #endif No. You can't ``run the preprocessor on itself,'' so to speak. What you can do is use one of two completely separate #define lines, depending on the #ifdef s...
2016-02-03, 1229👍, 0💬

How can I read in an object file and jump to locations in it?
How can I read in an object file and jump to locations in it? You want a dynamic linker or loader. It may be possible to malloc some space and read in object files, but you have to know an awful lot about object file formats, relocation, etc., and this approach can't work if code and data reside in ...
2015-03-09, 1229👍, 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, 1229👍, 0💬

How can I write a function which takes a variable number of arguments
How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)? In general, you cannot. Ideally, you should provide a version of that other function which accepts a va_list pointer. Suppose you want to write a...
2015-06-05, 1227👍, 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, 1227👍, 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, 1225👍, 0💬

<< < 160 161 162 163 164 165 166 167 168 169 170 > >>   Sort: Date