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

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

Must I free allocated memory before the program exits?
Must I free allocated memory before the program exits? You shouldn't have to. A real operating system definitively reclaims all memory and other resources when a program exits; the system cannot afford to have memory integrity depend on the whims of random programs. (Strictly speaking, it is not eve...
2016-03-28, 1203👍, 0💬

What do Segmentation violation, `Bus error, and General protection fault mean? What is a core dump?
What do Segmentation violation, `Bus error, and General protection fault mean? What is a core dump? These symptoms (and any similar messages having to do with memory access violations or protection faults) generally mean that your program tried to access memory it shouldn't have, invariably as a res...
2015-05-20, 1201👍, 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, 1201👍, 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, 1199👍, 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, 1198👍, 0💬

Does the sizeof operator work in preprocessor if directives?
Does the sizeof operator work in preprocessor if directives? No. Preprocessing happens during an earlier phase of compilation, before type names have been parsed. Instead of sizeof, consider using the predefined constants in ANSI's &lt;limits.h>, if applicable, or perhaps a ``configure'' script....
2016-02-03, 1197👍, 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, 1197👍, 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, 1196👍, 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, 1196👍, 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, 1196👍, 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, 1196👍, 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, 1196👍, 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, 1195👍, 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, 1194👍, 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, 1193👍, 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, 1191👍, 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, 1191👍, 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, 1191👍, 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, 1190👍, 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, 1190👍, 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, 1188👍, 0💬

Is exit(status) truly equivalent to returning the same status from main?
Is exit(status) truly equivalent to returning the same status from main? Yes and no. The Standard says that a return from the initial call to main is equivalent to calling exit. However, a return from main cannot be expected to work if data local to main might be needed during cleanup; A few very ol...
2015-03-09, 1187👍, 0💬

Why are some ANSI ISO Standard library functions showing you
Why are some ANSI/ISO Standard library functions showing up as undefined, even though I've got an ANSI compiler? It's possible to have a compiler available which accepts ANSI syntax, but not to have ANSI-compatible header files or run-time libraries installed. (In fact, this situation is rather comm...
2015-11-30, 1185👍, 0💬

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