<< < 162 163 164 165 166 167 168 169 170 171 172 > >>   Sort: Date

I am trying to compile this program
I'm trying to compile this program, but the compiler is complaining that ``union REGS'' is undefined, and the linker is complaining that int86 is undefined. Those have to do with MS-DOS interrupt programming. They don't exist on other systems.
2015-03-02, 1077👍, 0💬

How can I generate floating-point random numbers?
How can I generate floating-point random numbers? 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 is in the C Standard.) It's easy to write a low-pr...
2015-07-22, 1076👍, 0💬

Can I declare main as void, to shut off these annoying ....
Can I declare main as void, to shut off these annoying ``main returns no value'' messages? No. main must be declared as returning an int, and as taking either zero or two arguments, of the appropriate types. If you're calling exit() but still getting warnings, you may have to insert a redundant retu...
2015-12-20, 1074👍, 0💬

I have a varargs function which accepts a float parameter
I have a varargs function which accepts a float parameter. Why isn't va_arg(argp, float) working? In the variable-length part of variable-length argument lists, the old ``default argument promotions'' apply: arguments of type float are always promoted (widened) to type double, and types char and sho...
2015-06-08, 1074👍, 0💬

Does C have anything like the `substr (extract substring) routine present in other languages?
Does C have anything like the `substr (extract substring) 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, &amp;source[POS], LEN); dest[LEN] = '\0'; /* ensure \0 term...
2015-08-19, 1072👍, 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, 1072👍, 0💬

Why doesnt long int work?
Why doesn't long int n = 123456; printf("%d\n", n); work? Whenever you print long ints you must use the l (lower case letter ``ell'') modifier in the printf format (e.g. %ld). printf can't know the types of the arguments which you've passed to it, so you must let it know by using the correct format ...
2015-11-09, 1071👍, 0💬

I need some code to do regular expression and wildcard matching.
I need some code to do regular expression and wildcard matching. Make sure you recognize the difference between: * Classic regular expressions, variants of which are used in such Unix utilities as ed and grep. In regular expressions, a dot . usually matches any single character, and the sequence .* ...
2015-08-12, 1070👍, 0💬

Where can I get an ANSI-compatible lint?
Where can I get an ANSI-compatible lint? Products called PC-Lint and FlexeLint are available from Gimpel Software. The Unix System V release 4 lint is ANSI-compatible, and is available separately (bundled with other C tools) from UNIX Support Labs or from System V resellers. Another ANSI-compatible ...
2015-05-05, 1070👍, 0💬

How can a process change an environment variable in its caller?
How can a process change an environment variable in its caller? It may or may not be possible to do so at all. Different operating systems implement global name/value functionality similar to the Unix environment in different ways. Whether the ``environment'' can be usefully altered by a running pro...
2015-03-11, 1070👍, 0💬

How can I do serial comm port I O?
How can I do serial (``comm'') port I/O? It's system-dependent. Under Unix, you typically open, read, and write a device file in /dev, and use the facilities of the terminal driver to adjust its characteristics.Under MS-DOS, you can use the predefined stream stdaux, or a special file like COM1, or s...
2015-04-22, 1069👍, 0💬

I am trying to sort an array of strings with qsort, using strcmp as the comparison function, but it is not working.
I am trying to sort an array of strings with qsort, using strcmp as the comparison function, but it is not working. By ``array of strings'' you probably mean ``array of pointers to char.'' The arguments to qsort's comparison function are pointers to the objects being sorted, in this case, pointers t...
2015-08-12, 1068👍, 0💬

How can f be used for both float and double arguments in printf? Aren't they different types?
How can f be used for both float and double arguments in printf? Aren't they different types? In the variable-length part of a variable-length argument list, the ``default argument promotions'' apply: types char and short int are promoted to int, and float is promoted to double. (These are the same ...
2015-06-17, 1068👍, 0💬

I am appalled that the ANSI Standard leaves so many issues undefined ...
I'm appalled that the ANSI Standard leaves so many issues undefined. Isn't a Standard's whole job to standardize these things? It has always been a characteristic of C that certain constructs behaved in whatever way a particular compiler or a particular piece of hardware chose to implement them. Thi...
2015-11-18, 1067👍, 0💬

This program runs perfectly on one machine ...
This program runs perfectly on one machine, but I get weird results on another. Stranger still, adding or removing a debugging printout changes the symptoms... Lots of things could be going wrong; here are a few of the more common things to check: * uninitialized local variables integer overflow, es...
2015-05-27, 1067👍, 0💬

What is Hungarian Notation? Is it worthwhile?
What is Hungarian Notation? Is it worthwhile? Hungarian Notation is a naming convention, invented by Charles Simonyi, which encodes information about a variable's type (and perhaps its intended use) in its name. It is well-loved in some circles and roundly castigated in others. Its chief advantage i...
2015-05-08, 1067👍, 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, 1067👍, 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, 1067👍, 0💬

What are near and far pointers?
What are near and far pointers? These days, they're pretty much obsolete; they're definitely system-specific. They had to do with 16-bit programming under MS-DOS and perhaps some early versions of Windows. If you really need to know, see a DOS- or Windows-specific programming reference. If you're us...
2015-03-02, 1067👍, 0💬

Is there anything like an ifdef for typedefs?
Is there anything like an ifdef for typedefs? Unfortunately, no. (There can't be, because types and typedefs haven't been parsed at preprocessing time.) You may have to keep sets of preprocessor macros (e.g. MY_TYPE_DEFINED) recording whether certain typedefs have been declared.
2016-02-01, 1066👍, 0💬

How can I ensure that integer arithmetic doesnt overflow?
How can I ensure that integer arithmetic doesnt overflow? The usual approach is to test the operands against the limits in the header file &lt;limits.h> before doing the operation. For example, here is a ``careful'' addition function: int chkadd(int a, int b) { if(INT_MAX - b &lt; a) { fputs...
2015-03-04, 1064👍, 0💬

I am trying to use the ANSI stringizing preprocessing operator ....
I'm trying to use the ANSI ``stringizing'' preprocessing operator `#' to insert the value of a symbolic constant into a message, but it keeps stringizing the macro's name rather than its value. It turns out that the definition of # says that it's supposed to stringize a macro argument immediately, w...
2016-01-22, 1063👍, 0💬

Why does the declaration extern int .....
Why does the declaration extern int f(struct x *p); give me an obscure warning message about ``struct x declared inside parameter list''? In a quirk of C's normal block scoping rules, a structure declared (or even mentioned) for the first time within a prototype cannot be compatible with other struc...
2016-01-11, 1063👍, 0💬

Why does the simple line-copying loop while ...
Why does the simple line-copying loop while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); } copy the last line twice? p> In C, end-of-file is only indicated after an input routine has tried to read, and failed. (In other words, C's I/O is not like Pascal's.) Usually, you should just c...
2015-11-13, 1063👍, 0💬

<< < 162 163 164 165 166 167 168 169 170 171 172 > >>   Sort: Date