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

How can I write a function that takes a variable number of arguments?
How can I write a function that takes a variable number of arguments? Use the facilities of the &lt;stdarg.h> header. Here is a function which concatenates an arbitrary number of strings into malloc'ed memory: #include &lt;stdlib.h> /* for malloc, NULL, size_t */ #include &lt;stdarg.h> /...
2015-06-15, 1155👍, 0💬

Why is the macro giving me the warning
Why is the macro #define TRACE(n) printf("TRACE: %d\n", n) giving me the warning ``macro replacement within a string literal''? It seems to be expanding TRACE(count); as printf("TRACE: %d\count", count); Some pre-ANSI compilers/preprocessors interpreted macro definitions like #define TRACE(var, fmt)...
2016-01-25, 1154👍, 0💬

What are pragmas and what are they good for?
What are pragmas and what are they good for? The #pragma directive provides a single, well-defined ``escape hatch'' which can be used for all sorts of (nonportable) implementation-specific controls and extensions: source listing control, structure packing, warning suppression (like lint's old /* NOT...
2015-12-11, 1152👍, 0💬

Why doesnt that code work?
Why doesn't the code short int s; scanf("%d", &s); work? When converting %d, scanf expects a pointer to an int. To convert to a short int, use %hd .
2015-10-28, 1152👍, 0💬

I am trying to port this old program. Why do I get undefined external errors for some library functions?
I am trying to port this old program. Why do I get undefined external errors for some library functions? Some old or semistandard functions have been renamed or replaced over the years; if you need:/you should instead: index use strchr. rindex use strrchr. bcopy use memmove, after interchanging the ...
2015-07-16, 1151👍, 0💬

What is alloca and why is its use discouraged?
What is alloca and why is its use discouraged? alloca allocates memory which is automatically freed when the function which called alloca returns. That is, memory allocated with alloca is local to a particular function's ``stack frame'' or context. alloca cannot be written portably, and is difficult...
2016-03-14, 1150👍, 0💬

How can I get the current date or time of day in a C program?
How can I get the current date or time of day in a C program? Just use the time, ctime, localtime and/or strftime functions. Here is a simple example: #include &lt;stdio.h> #include &lt;time.h> int main() { time_t now; time(&amp;now); printf("It's %s", ctime(&amp;now)); return 0; } C...
2015-08-07, 1148👍, 0💬

I thought I would check errno after a long string of printf calls ...
I thought I'd check errno after a long string of printf calls, to see if any of them had failed: errno = 0; printf("This\n"); printf("is\n"); printf("a\n"); printf("test.\n"); if(errno != 0) fprintf(stderr, "printf failed: %s\n", strerror(errno)); Why is it printing something strange like ``printf f...
2015-10-14, 1143👍, 0💬

How can I flush pending input so that a user ...
How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work? fflush is defined only for output streams. Since its definition of ``flush'' is to complete the writing of buffered characters (not to discard them), discarding unread input would not be ...
2015-10-12, 1142👍, 0💬

How can I sort more data than will fit in memory?
How can I sort more data than will fit in memory? You want an ``external sort,'' which you can read about in Knuth, Volume 3. The basic idea is to sort the data in chunks (as much as will fit in memory at one time), write each sorted chunk to a temporary file, and then merge the files. Your operatin...
2015-08-07, 1142👍, 0💬

How do I convert a string to all upper or lower case?
How do I convert a string to all upper or lower case? Some libraries have routines strupr and strlwr or strupper and strlower, but these are not Standard or portable. It's a straightforward exercise to write upper/lower-case functions in terms of the toupper and tolower macros in &lt;ctype.h>;(T...
2015-08-19, 1140👍, 0💬

How can I read/write structures from/to data files?
How can I read/write structures from/to data files? It is relatively straightforward to write a structure out using fwrite: fwrite(&amp;somestruct, sizeof somestruct, 1, fp); and a corresponding fread invocation can read it back in. What happens here is that fwrite receives a pointer to the stru...
2015-09-18, 1135👍, 0💬

How can I change their mode to binary?
I'm writing a ``filter'' for binary files, but stdin and stdout are preopened as text streams. How can I change their mode to binary? There is no standard way to do this. On Unix-like systems, there is no text/binary distinction, so there is no need to change the mode. Some MS-DOS compilers supply a...
2015-09-21, 1134👍, 0💬

If fflush wont work, what can I use to flush input?
If fflush wont work, what can I use to flush input? It depends on what you're trying to do. If you're trying to get rid of an unread newline or other unexpected input after calling scanf you really need to rewrite or replace the call to scanf. Alternatively, you can consume the rest of a partially-r...
2015-10-12, 1130👍, 0💬

I need a random true/false value ...
I need a random true/false value, so I'm just taking rand() % 2, but it's alternating 0, 1, 0, 1, 0... Poor pseudorandom number generators (such as the ones unfortunately supplied with some systems) are not very random in the low-order bits. (In fact, for a pure linear congruential random number gen...
2015-07-24, 1130👍, 0💬

A third-party header file I just started using is defining its own TRUE and FALSE values incompatibly ...
A third-party header file I just started using is defining its own TRUE and FALSE values incompatibly with the code I've already developed. What can I do? This is indeed an annoying situation. It's a classic namespace problem;Ideally, third-party vendors would be conscientious when defining symbols ...
2016-02-26, 1129👍, 0💬

How can I write a function analogous to scanf
How can I write a function analogous to scanf, i.e. that accepts similar arguments, and calls scanf to do most of the work? C99 (but not any earlier C Standard) supports vscanf, vfscanf, and vsscanf.
2015-06-12, 1123👍, 0💬

How should functions be apportioned among source files?
How should functions be apportioned among source files? Usually, related functions are put together in one file. Sometimes (as when developing libraries) it is appropriate to have exactly one source file (and, consequently, one object module) per independent function. Other times, and especially for...
2015-05-18, 1121👍, 0💬

Why is p turning out const, instead of the characters pointed to?
I've got the declarations typedef char *charp; const charp p; Why is p turning out const, instead of the characters pointed to? typedef substitutions are not purely textual.In the declaration const charp p; p is const for the same reason that const int i declares i as const. The typedef'ed declarati...
2016-01-04, 1118👍, 0💬

How can I get back to the interactive keyboard if stdin is redirected?
I'm trying to write a program like ``more.'' How can I get back to the interactive keyboard if stdin is redirected? There is no portable way of doing this. Under Unix, you can open the special file /dev/tty. Under MS-DOS, you can try opening the ``file'' CON, or use routines or BIOS calls such as ge...
2015-09-29, 1111👍, 0💬

How can I delete a file?
How can I delete a file? The Standard C Library function is remove. (This is therefore one of the few questions in this section for which the answer is not ``It's system-dependent.'') On older, pre-ANSI Unix systems, remove may not exist, in which case you can try unlink.
2015-04-10, 1105👍, 0💬

I am trying to update a file in place ...
I'm trying to update a file in place, by using fopen mode "r+", reading a certain string, and writing back a modified string, but it's not working. Be sure to call fseek before you write, both to seek back to the beginning of the string you're trying to overwrite, and because an fseek or fflush is a...
2015-10-07, 1102👍, 0💬

I need code to parse and evaluate expressions.
I need code to parse and evaluate expressions. Two available packages are ``defunc,'' posted to comp.sources.misc in December, 1993 (V41 i32,33), to alt.sources in January, 1994, and available from sunsite.unc.edu in pub/packages/development/libra ries/defunc-1.3.tar.Z,and ``parse,'' at lamont.ldgo....
2015-05-01, 1102👍, 0💬

How can I tell if standard input or output is redirected
How can I tell if standard input or output is redirected (i.e. whether ``&lt;'' or ``&gt;'' was used on the invocation command line)? You can't tell directly, but you can usually look at a few other things to make whatever decision you need to. If you want your program to take input from std...
2015-10-01, 1094👍, 0💬

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