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

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

Why is my simple program, which hardly does more than print
Why is my simple program, which hardly does more than print ``Hello, world!'' in a window, compiling to such a huge executable (several hundred K)? Should I #include fewer header files? What you're seeing is the current (poor) state of the ``art'' in library design. As run-time libraries accrete mor...
2015-07-06, 1122👍, 0💬

How can I find the modification date and time of a file?
How can I find the modification date and time of a file? The Unix and POSIX function is stat, which several other systems supply as well.
2015-04-13, 1122👍, 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, 1122👍, 0💬

I am splitting up a program into multiple source files for the first time ...
I'm splitting up a program into multiple source files for the first time, and I'm wondering what to put in .c files and what to put in .h files. (What does ``.h'' mean, anyway?) As a general rule, you should put these things in header (.h) files: macro definitions (preprocessor #defines) structure, ...
2016-02-18, 1120👍, 0💬

I have got this tricky preprocessing I want to do and I cant figure out a way to do it.
I have got this tricky preprocessing I want to do and I cant figure out a way to do it. C's preprocessor is not intended as a general-purpose tool. (Note also that it is not guaranteed to be available as a separate program.) Rather than forcing it to do something inappropriate, you might want to wri...
2016-01-19, 1119👍, 0💬

How can I do graphics?
How can I do graphics? Once upon a time, Unix had a fairly nice little set of device-independent plot functions described in plot(3) and plot(5). The GNU libplot library, written by Robert Maier, maintains the same spirit and supports many modern plot devices; see http://www.gnu.org/software/pl otuti...
2015-04-17, 1119👍, 0💬

I heard that you have to include ......
I heard that you have to #include before calling printf. Why? So that a proper prototype for printf will be in scope. A compiler may use a different calling sequence for functions which accept variable-length argument lists. (It might do so if calls using variable-length argument lists were less eff...
2016-01-08, 1118👍, 0💬

How can I return a sequence of random numbers which dont repeat at all?
How can I return a sequence of random numbers which dont repeat at all? What you're looking for is often called a ``random permutation'' or ``shuffle.'' One way is to initialize an array with the values to be shuffled, then randomly interchange each of the cells with another one later in the array: ...
2015-07-24, 1118👍, 0💬

What is the best style for code layout in C?
What is the best style for code layout in C? While providing the example most often copied, also supply a good excuse for disregarding it: The position of braces is less important, although people hold passionate beliefs. We have chosen one of several popular styles. Pick a style that suits you, the...
2015-05-18, 1118👍, 0💬

How can I get random integers in a certain range?
How can I get random integers in a certain range? The obvious way, rand() % N /* POOR */ (which tries to return numbers from 0 to N-1) is poor, because the low-order bits of many random number generators are distressingly non-random. A better method is something like (int)((double)rand() / ((double)...
2015-07-29, 1117👍, 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, 1116👍, 0💬

Why do some people write if(0 == x) instead of if(x == 0)?
Why do some people write if(0 == x) instead of if(x == 0)? It's a trick to guard against the common error of writing if(x = 0) If you're in the habit of writing the constant before the ==, the compiler will complain if you accidentally type if(0 = x) Evidently it can be easier for some people to rem...
2015-05-15, 1116👍, 0💬

I heard that you have to include stdio.h before calling printf. Why?
I heard that you have to include stdio.h before calling printf. Why? So that a proper prototype for printf will be in scope. A compiler may use a different calling sequence for functions which accept variable-length argument lists. (It might do so if calls using variable-length argument lists were l...
2015-06-17, 1115👍, 0💬

Why isnt my procedure call working? The compiler seems to skip right over it
Why isnt my procedure call working? The compiler seems to skip right over it Does the code look like this? myprocedure; C has only functions, and function calls always require parenthesized argument lists, even if empty. Use myprocedure(); Without the parentheses, the reference to the function name ...
2015-06-01, 1115👍, 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, 1114👍, 0💬

Why do all the lines end up containing copies of the last line?
I'm using fgets to read lines from a file into an array of pointers. Why do all the lines end up containing copies of the last line? You have only allocated memory for one line, linebuf. Each time you call fgets, the previous line is overwritten. fgets doesn't do any memory allocation: unless it rea...
2015-11-13, 1113👍, 0💬

Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer?
Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer? sizeof tells you the size of the pointer. There is no portable way to find out the size of a malloc'ed block. (Remember, too, that sizeof operates at compile time
2016-03-21, 1112👍, 0💬

Did C have any Year 2000 problems?
Did C have any Year 2000 problems? No, although poorly-written C programs might have. The tm_year field of struct tm holds the value of the year minus 1900; this field therefore contains the value 100 for the year 2000. Code that uses tm_year correctly (by adding or subtracting 1900 when converting ...
2015-08-03, 1112👍, 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, 1111👍, 0💬

Why doesnt this code work?
Why doesn't this code: double d; scanf("%f", &amp;d); work? Unlike printf, scanf uses %lf for values of type double, and %f for float.%f tells scanf to expect a pointer-to-float, not the pointer-to-double you gave it. Either use %lf, or declare the receiving variable as a float.
2015-10-30, 1110👍, 0💬

How can I find out the size of a file, prior to reading it in?
How can I find out the size of a file, prior to reading it in? If the ``size of a file'' is the number of characters you'll be able to read from it in C (or which were written to it by a previous program), it can be difficult or impossible to determine this number exactly (other than by reading the ...
2015-04-15, 1110👍, 0💬

Does C have anything like the `substr extract substrin routine present in other languages?
Does C have anything like the `substr extract substrin routine present in other languages? Not as such. To extract a substring of length LEN starting at index POS in a source string, use something like char dest[LEN+1]; strncpy(dest, &source[POS], LEN); dest[LEN] = '\0'; /* ensure \0 termination...
2016-03-07, 1109👍, 0💬

What are the complete rules for header file searching?
What are the complete rules for header file searching? The exact behavior is implementation-defined (which means that it is supposed to be documented; Typically, headers named with &lt;> syntax are searched for in one or more standard places. Header files named with "" syntax are first searched ...
2016-02-12, 1109👍, 0💬

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