<< < 164 165 166 167 168 169 170 171 172 173 174 > >>   Sort: Date

My programs prompts and intermediate output dont always show up on the screen ...
My program's prompts and intermediate output don't always show up on the screen, especially when I pipe the output through another program. It's best to use an explicit fflush(stdout) whenever output should definitely be visible (and especially if the text does not end with \n). Several mechanisms a...
2015-11-11, 1200👍, 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, 1199👍, 0💬

I have a simple little program that reads characters until EOF ...
I have a simple little program that reads characters until EOF, but how do I actually enter that ``EOF'' value from the keyboard? I see that EOF is defined by to be -1; am I supposed to enter -1? If you think about it, what you enter can't be -1, because ``-1'' is two characters, and getchar is read...
2015-11-16, 1197👍, 0💬

I thought that using large model meant that I could use more than 64K of data!
What does the error message ``DGROUP data allocation exceeds 64K'' mean, and what can I do about it? I thought that using large model meant that I could use more than 64K of data! Even in large memory models, MS-DOS compilers apparently toss certain data (strings, some initialized global or static v...
2015-03-20, 1197👍, 0💬

Why is this loop always executing once?
Why is this loop always executing once? for(i = start; i &lt; end; i++); { printf("%d\n", i); } A: The accidental extra semicolon hiding at the end of the line containing the for constitutes a null statement which is, as far as the compiler is concerned, the loop body. The following brace-enclos...
2015-06-03, 1195👍, 0💬

My ANSI compiler complains about a mismatch when it see
My ANSI compiler complains about a mismatch when it sees extern int func(float); int func(x) float x; { ... You have mixed the new-style prototype declaration ``extern int func(float);'' with the old-style definition ``int func(x) float x;''. It is usually possible to mix the two styles but not in t...
2016-01-13, 1193👍, 0💬

I have got some code that is trying to unpack external structures
I've got some code that's trying to unpack external structures, but it's crashing with a message about an ``unaligned access.'' What does this mean? The code looks like this: struct mystruct { char c; long int i32; int i16; } s; char buf[7], *p; fread(buf, 7, 1, fp); p = buf; s.c = *p++; s.i32 = *(l...
2015-05-20, 1193👍, 0💬

People keep saying that the behavior of ...
People keep saying that the behavior of i = i++ is undefined, but I just tried it on an ANSI-conforming compiler, and got the results I expected. A compiler may do anything it likes when faced with undefined behavior (and, within limits, with implementation-defined and unspecified behavior), includi...
2015-11-18, 1192👍, 0💬

How can I trap or ignore keyboard interrupts like control-C?
How can I trap or ignore keyboard interrupts like control-C? The basic step is to call signal, either as #include &lt;signal.h> signal(SIGINT, SIG_IGN); to ignore the interrupt signal, or as extern void func(int); signal(SIGINT, func); to cause control to transfer to function func on receipt of ...
2015-03-06, 1192👍, 0💬

Since array references decay into pointers ...
Since array references decay into pointers, if arr is an array, what's the difference between arr and &arr? The type. In Standard C, &arr yields a pointer, of type pointer-to-array-of-T, to the entire array. (In pre-ANSI C, the & in &arr generally elicited a warning, and was generall...
2015-12-09, 1191👍, 0💬

This program crashes before it even runs!
This program crashes before it even runs! (When single-stepping with a debugger, it dies before the first statement in main.) You probably have one or more very large (kilobyte or more) local arrays. Many systems have fixed-size stacks, and even those which perform dynamic stack allocation automatic...
2015-05-29, 1191👍, 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, 1190👍, 0💬

Is it acceptable for one header file to #include another?
Is it acceptable for one header file to #include another? It's a question of style, and thus receives considerable debate. Many people believe that ``nested #include files'' are to be avoided: the prestigious Indian Hill Style Guide disparages them; they can make it harder to find relevant definitio...
2016-02-16, 1189👍, 0💬

I inherited some code which contains far too many ...
I inherited some code which contains far too many #ifdef's for my taste. How can I preprocess the code to leave only one conditional compilation set, without running it through the preprocessor and expanding all of the #include's and #define's as well? There are programs floating around called unifd...
2016-01-29, 1188👍, 0💬

I have a pre-ANSI compiler, without stdarg.h What can I do?
I have a pre-ANSI compiler, without stdarg.h What can I do? There's an older header, &lt;varargs.h>, which offers about the same functionality. Here is the vstrcat function, rewritten to use &lt;varargs.h>: #include &lt;stdio.h> #include &lt;varargs.h> #include &lt;string.h> exte...
2015-06-10, 1188👍, 0💬

How can my program discover the complete pathname to the executable from which it was invoked?
How can my program discover the complete pathname to the executable from which it was invoked? argv[0] may contain all or part of the pathname, or it may contain nothing. You may be able to duplicate the command language interpreter's search path logic to locate the executable if the name in argv[0]...
2015-03-13, 1188👍, 0💬

What if a built-in logical or relational operator `returns something other than 1? ....
Isn't #defining TRUE to be 1 dangerous, since any nonzero value is considered ``true'' in C? What if a built-in logical or relational operator ``returns'' something other than 1? It is true (sic) that any nonzero value is considered true in C, but this applies only ``on input'', i.e. where a Boolean...
2016-02-29, 1187👍, 0💬

I can write C code that looks more like Pascal? ....
Here are some cute preprocessor macros: #define begin { #define end } With these, I can write C code that looks more like Pascal. What do y'all think? Use of macros like these, though perhaps superficially attractive, is generally discouraged; in severe cases the practice is called ``preprocessor ab...
2016-02-24, 1187👍, 0💬

I had a frustrating problem which turned out to be caused by the line
I had a frustrating problem which turned out to be caused by the line printf("%d", n); where n was actually a long int. I thought that ANSI function prototypes were supposed to guard against argument type mismatches like this. When a function accepts a variable number of arguments, its prototype doe...
2015-06-15, 1187👍, 0💬

I came across some code that puts a (void) cast before each call to printf. Why?
I came across some code that puts a (void) cast before each call to printf. Why? printf does return a value (the number of characters printed, or an error code), though few programs bother to check the return values from each call. Since some compilers (and lint) will warn about discarded return val...
2015-05-13, 1184👍, 0💬

I have some old code that tries to construct identifiers with a macro like ...
I have some old code that tries to construct identifiers with a macro like #define Paste(a, b) a/**/b but it doesn't work any more. It was an undocumented feature of some early preprocessor implementations (notably Reiser's) that comments disappeared entirely and could therefore be used for token pa...
2016-01-27, 1183👍, 0💬

What's the difference between using a typedef or a define for a user-defined type?
What's the difference between using a typedef or a define for a user-defined type? In general, typedefs are preferred, in part because they can correctly encode pointer types. For example, consider these declarations: typedef char *String_t; #define String_d char * String_t s1, s2; String_d s3, s4; ...
2016-02-22, 1182👍, 0💬

What is the difference between const char .....
What's the difference between const char *p, char const *p, and char * const p? The first two are interchangeable; they declare a pointer to a constant character (you can't change any pointed-to characters). char * const p declares a constant pointer to a (variable) character (i.e. you can't change ...
2016-01-06, 1182👍, 0💬

I am trying to take some square roots
I'm trying to take some square roots, and I've simplified the code down to main() { printf("%f\n", sqrt(144.)); } but I'm still getting crazy numbers. Make sure that you have #included &lt;math.h>, and correctly declared other functions returning double. (Another library function to be careful w...
2015-07-01, 1182👍, 0💬

<< < 164 165 166 167 168 169 170 171 172 173 174 > >>   Sort: Date