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

Why does this code crash?
Why does this code: char *p = "hello, world!"; p[0] = 'H'; crash? String constants are in fact constant. The compiler may place them in nonwritable storage, and it is therefore not safe to modify them. When you need writable strings, you must allocate writable memory for them, either by declaring an...
2015-05-27, 1128👍, 0💬

How can I send mail from within a C program?
How can I send mail from within a C program? Under Unix, open a pipe to the mail program, or perhaps /usr/lib/sendmail.
2015-04-17, 1128👍, 0💬

How can I access an I O board directly?
How can I access an I O board directly? At one level, at least, it's quite simple: you have a device register which is actually wired up so that the bits written to it get coverted to actual voltage levels in the real world that you can do interesting things with. In general, there are two ways to g...
2015-04-20, 1127👍, 0💬

How can I check whether a file exists? I want to warn the user if a requested input file is missing.
How can I check whether a file exists? I want to warn the user if a requested input file is missing. It's surprisingly difficult to make this determination reliably and portably. Any test you make can be invalidated if the file is created or deleted (i.e. by some other process) between the time you ...
2015-04-15, 1125👍, 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, 1125👍, 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, 1124👍, 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, 1124👍, 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, 1123👍, 0💬

How can I generate random numbers with a normal or Gaussian distribution?
How can I generate random numbers with a normal or Gaussian distribution? There are a number of ways of doing this. 1. Exploit the Central Limit Theorem (``law of large numbers'') and add up several uniformly-distributed random numbers: #include &lt;stdlib.h> #include &lt;math.h> #define NSU...
2015-07-22, 1123👍, 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, 1121👍, 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, 1120👍, 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, 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💬

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💬

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, 1117👍, 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💬

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💬

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, 1115👍, 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💬

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