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

I am trying to define a few simple little function-like macros ...
I'm trying to define a few simple little function-like macros such as #define square(x) x * x but they're not always working. There are three important rules to remember when defining function-like macros: 1. The macro expansion must always be parenthesized to protect any lower-precedence operators ...
2016-02-26, 1093👍, 0💬

If I can say... why can't I say...
If I can say char a[] = "Hello, world!"; why can't I say char a[14]; Strings are arrays, and you can't assign arrays directly. Use strcpy instead: strcpy(a, "Hello, world!");
2016-03-11, 1092👍, 0💬

How can I insert or delete a line (or record) in the middle of a file?
How can I insert or delete a line (or record) in the middle of a file? In general, there is no way to do this. The usual solution is simply to rewrite the file. When you find yourself needing to insert data into an existing file, here are a few alternatives you can try: * Rearrange the data file so ...
2015-10-05, 1092👍, 0💬

Once I have used freopen, how can I get the original stdout (or stdin) back?
Once I have used freopen, how can I get the original stdout (or stdin) back? There isn't a good way. If you need to switch back, the best solution is not to have used freopen in the first place. Try using your own explicit output (or input) stream variable, which you can reassign at will, while leav...
2015-10-01, 1092👍, 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, 1091👍, 0💬

Some people say that gotos are evil and that I should never use them. Isnt that a bit extreme
Some people say that gotos are evil and that I should never use them. Isnt that a bit extreme rogramming style, like writing style, is somewhat of an art and cannot be codified by inflexible rules, although discussions about style often seem to center exclusively around such rules. In the case of th...
2015-05-08, 1090👍, 0💬

My compiler isnt letting me declare a function
My compiler isnt letting me declare a function A; My compiler isn't letting me declare a function int f(...) { } i.e. accepting a variable number of arguments, but with no fixed arguments at all. A: Standard C requires at least one fixed argument, in part so that you can hand it to va_start. (In any...
2015-06-08, 1088👍, 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, 1087👍, 0💬

How can I find out if there are characters available for reading?
How can I find out if there are characters available for reading (and if so, how many)? Alternatively, how can I do a read that will not block if there are no characters available? These, too, are entirely operating-system-specific. Some versions of curses have a nodelay function. Depending on your ...
2015-04-29, 1085👍, 0💬

I am starting to think about multinational character sets ....
I'm starting to think about multinational character sets, and I'm worried about the implications of making sizeof(char) be 2 so that 16-bit character sets can be represented. If type char were made 16 bits, sizeof(char) would still be 1, and CHAR_BIT in &lt;limits.h> would be 16, and it would si...
2016-03-02, 1083👍, 0💬

I have an old macro that doesn't seem to work any more....
I have an old macro #define CTRL(c) ('c' &amp; 037) that doesn't seem to work any more. The intended use of this macro is in code like tchars.t_eofc = CTRL(D); which is expected to expand to tchars.t_eofc = ('D' &amp; 037); based on the assumption that the actual value of the parameter c wil...
2016-01-25, 1082👍, 0💬

I just typed in this program, and it is acting strangely. Can you see anything wrong with it?
I just typed in this program, and it is acting strangely. Can you see anything wrong with it? See if you can run lint first (perhaps with the -a, -c, -h, -p or other options ). Many C compilers are really only half-compilers, taking the attitude that it's not their problem if you didn't say what you...
2015-05-06, 1080👍, 0💬

How can I insert or delete a line (or record) in the middle of a file?
How can I insert or delete a line (or record) in the middle of a file? In general, there is no way to do this. The usual solution is simply to rewrite the file. When you find yourself needing to insert data into an existing file, here are a few alternatives you can try: * Rearrange the data file so ...
2015-04-13, 1078👍, 0💬

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

I cant get strcat to work. I tried ...
I can't get strcat to work. I tried char *s1 = "Hello, ";br> char *s2 = "world!";br> char *s3 = strcat(s1, s2);br> but I got strange results. the main problem here is that space for the concatenated result is not properly allocated. C does not provide an automatically-managed string type. C compiler...
2016-03-09, 1073👍, 0💬

My floating-point calculations are acting strangely and giving me different answers on different machines.
My floating-point calculations are acting strangely and giving me different answers on different machines. If the problem isn't that simple, recall that digital computers usually use floating-point formats which provide a close but by no means exact simulation of real number arithmetic. Among other ...
2015-06-29, 1072👍, 0💬

How can I find out how much free space is available on disk?
How can I find out how much free space is available on disk? There is no portable way. Under some versions of Unix you can call statfs. Under MS-DOS, use interrupt 0x21 subfunction 0x36, or perhaps a routine such as diskfree. Another possibility is to use popen to invoke and read the output of a ``d...
2015-04-03, 1072👍, 0💬

How can I open files with names like ...
How can I open files with names like ``file1'', ``file2'', ``file3'', etc., where the numeric part is controlled by a variable? Basically I want ``file%d'', like printf.. You want printf's close cousin sprintf, which ``prints'' to a string: char filename[FILENAME_MAX]; sprintf(filename, "file%d", i)...
2015-10-07, 1071👍, 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, 1067👍, 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, 1065👍, 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, 1064👍, 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, 1064👍, 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, 1061👍, 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, 1059👍, 0💬

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