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

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, 1176👍, 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, 1175👍, 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, 1175👍, 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, 1172👍, 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, 1172👍, 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, 1172👍, 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, 1172👍, 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, 1171👍, 0💬

How can I implement a delay, or time a users response, with sub-second resolution?
How can I implement a delay, or time a users response, with sub-second resolution? Unfortunately, there is no portable way. Routines you might look for on your system include clock, delay, ftime, gettimeofday, msleep, nap, napms, nanosleep, setitimer, sleep, Sleep, times, and usleep. (A function cal...
2015-03-06, 1171👍, 0💬

Why does the call char scanf work?
Why does the call char s[30]; scanf("%s", s); work? I thought you always needed an & on each variable passed to scanf. You always need a pointer; you don't necessarily need an explicit &amp;. When you pass an array to scanf, you do not need the &, because arrays are always passed to func...
2015-10-30, 1168👍, 0💬

Can you mix old-style and new-style function syntax?
Can you mix old-style and new-style function syntax? Doing so is legal (and can be useful for backwards compatibility), but requires a certain amount of care . Modern practice, however, is to use the prototyped form in both declarations and definitions. (The old-style syntax is marked as obsolescent...
2016-01-13, 1167👍, 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, 1167👍, 0💬

Why isnt it being handled properly?
I'm reading strings typed by the user into an array, and then printing them out later. When the user types a sequence like \n, why isn't it being handled properly? Character sequences like \n are interpreted at compile time. When a backslash and an adjacent n appear in a character constant or string...
2015-08-24, 1165👍, 0💬

Should I use symbolic names like TRUE and FALSE for Boolean constants, or plain 1 and 0?
Should I use symbolic names like TRUE and FALSE for Boolean constants, or plain 1 and 0? It's your choice. Preprocessor macros like TRUE and FALSE (and, of course, NULL) are used for code readability, not because the underlying values might ever change. It's a matter of style, not correctness, wheth...
2015-05-11, 1165👍, 0💬

How can I read a binary data file properly?
How can I read a binary data file properly? I'm occasionally seeing 0x0a and 0x0d values getting garbled, and I seem to hit EOF prematurely if the data contains the value 0x1a. When you're reading a binary data file, you should specify "rb" mode when calling fopen, to make sure that text file transl...
2015-09-24, 1164👍, 0💬

I am getting baffling syntax errors which make no sense at all
I'm getting baffling syntax errors which make no sense at all, and it seems like large chunks of my program aren't being compiled. Check for unclosed comments, mismatched #if/#ifdef/#ifndef/#else/#endi fdirectives, and perhaps unclosed quotes; remember to check header files, too.
2015-06-01, 1164👍, 0💬

Why cant I perform arithmetic on a void pointer?
Why cant I perform arithmetic on a void pointer? The compiler doesn't know the size of the pointed-to objects. (Remember that pointer arithmetic is always in terms of the pointed-to size; Therefore, arithmetic on void *'s is disallowed (though some compilers allow it as an extension). Before perform...
2015-12-07, 1163👍, 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, 1161👍, 0💬

I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it?
I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it? drand48 is a Unix System V routine which returns floating point random numbers (presumably with 48 bits of precision) in the half-open interval [0, 1). (Its companion seed routine is srand48; neither...
2015-07-20, 1158👍, 0💬

Dont ANSI function prototypes render lint obsolete?
Dont ANSI function prototypes render lint obsolete? Not really. First of all, prototypes work only if they are present and correct; an inadvertently incorrect prototype is worse than useless. Secondly, lint checks consistency across multiple source files, and checks data declarations as well as func...
2015-05-01, 1158👍, 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, 1156👍, 0💬

When I read numbers from the keyboard with scanf ...
When I read numbers from the keyboard with scanf and a "%d\n" format, like this: int n; scanf("%d\n", &amp;n); printf("you typed %d\n", n); it seems to hang until I type one extra line of input. Perhaps surprisingly, \n in a scanf format string does not mean to expect a newline, but rather to re...
2015-10-23, 1156👍, 0💬

People always say that good style is important
People always say that good style is important, but when they go out of their way to use clear techniques and make their programs readable, they seem to end up with less efficient programs. Since efficiency is so important, isn't it necessary to sacrifice some style and readability? It's true that g...
2015-05-06, 1156👍, 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-07-20, 1155👍, 0💬

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