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

I have a simple little program that reads characters until EOF ...
I have a simple little program that reads characters until EOF, but how do I actually enter that ``EOF'' value from the keyboard? I see that EOF is defined by to be -1; am I supposed to enter -1? If you think about it, what you enter can't be -1, because ``-1'' is two characters, and getchar is read...
2015-11-16, 1146👍, 0💬

What are the differences between C and CPP?
What are the differences between C and CPP? Q: Is C++ a superset of C? What are the differences between C and C++? Can I use a C++ compiler to compile C code? A: C++ was derived from C, and is largely based on it, but there are some legal C constructs which are not legal C++. Conversely, ANSI C inhe...
2015-01-10, 1146👍, 0💬

I have got some code that is trying to unpack external structures
I've got some code that's trying to unpack external structures, but it's crashing with a message about an ``unaligned access.'' What does this mean? The code looks like this: struct mystruct { char c; long int i32; int i16; } s; char buf[7], *p; fread(buf, 7, 1, fp); p = buf; s.c = *p++; s.i32 = *(l...
2015-05-20, 1144👍, 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, 1144👍, 0💬

How can I write a macro which takes a variable number of arguments ....
How can I write a macro which takes a variable number of arguments, or use the preprocessor to ``turn off'' a function call with a variable number of arguments? One popular trick is to define and invoke the macro with a single, parenthesized ``argument'' which in the macro expansion becomes the enti...
2016-01-19, 1143👍, 0💬

Why is this loop always executing once?
Why is this loop always executing once? for(i = start; i &lt; end; i++); { printf("%d\n", i); } A: The accidental extra semicolon hiding at the end of the line containing the for constitutes a null statement which is, as far as the compiler is concerned, the loop body. The following brace-enclos...
2015-06-03, 1143👍, 0💬

I came across some code that puts a (void) cast before each call to printf. Why?
I came across some code that puts a (void) cast before each call to printf. Why? printf does return a value (the number of characters printed, or an error code), though few programs bother to check the return values from each call. Since some compilers (and lint) will warn about discarded return val...
2015-05-13, 1142👍, 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, 1142👍, 0💬

Why cant I open a file by its explicit path?
Why can't I open a file by its explicit path? The call fopen("c:\newdir\file.dat", "r") is failing. The file you actually requested--with the characters \n and \f in its name--probably doesn't exist, and isn't what you thought you were trying to open. In character constants and string literals, the ...
2015-04-08, 1142👍, 0💬

What if a built-in logical or relational operator `returns something other than 1? ....
Isn't #defining TRUE to be 1 dangerous, since any nonzero value is considered ``true'' in C? What if a built-in logical or relational operator ``returns'' something other than 1? It is true (sic) that any nonzero value is considered true in C, but this applies only ``on input'', i.e. where a Boolean...
2016-02-29, 1140👍, 0💬

Is it acceptable for one header file to #include another?
Is it acceptable for one header file to #include another? It's a question of style, and thus receives considerable debate. Many people believe that ``nested #include files'' are to be avoided: the prestigious Indian Hill Style Guide disparages them; they can make it harder to find relevant definitio...
2016-02-16, 1140👍, 0💬

How can I write a function that takes a format string and a variable number of arguments
How can I write a function that takes a format string and a variable number of arguments, like printf, and passes them to printf to do most of the work? Use vprintf, vfprintf, or vsprintf. These routines are like their counterparts printf, fprintf, and sprintf, except that instead of a variable-leng...
2015-06-12, 1140👍, 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, 1140👍, 0💬

People keep saying that the behavior of ...
People keep saying that the behavior of i = i++ is undefined, but I just tried it on an ANSI-conforming compiler, and got the results I expected. A compiler may do anything it likes when faced with undefined behavior (and, within limits, with implementation-defined and unspecified behavior), includi...
2015-11-18, 1139👍, 0💬

What does the message Automatic aggregate intialization is an ANSI feature mean?
What does the message Automatic aggregate intialization is an ANSI feature'' mean? My compiler is complaining about valid ANSI code. Messages like these are typically emitted by pre-ANSI compilers which have been upgraded just enough to detect (but not properly translate) new C features which were i...
2015-11-30, 1138👍, 0💬

I inherited some code which contains far too many ...
I inherited some code which contains far too many #ifdef's for my taste. How can I preprocess the code to leave only one conditional compilation set, without running it through the preprocessor and expanding all of the #include's and #define's as well? There are programs floating around called unifd...
2016-01-29, 1137👍, 0💬

I have a pre-ANSI compiler, without stdarg.h What can I do?
I have a pre-ANSI compiler, without stdarg.h What can I do? There's an older header, &lt;varargs.h>, which offers about the same functionality. Here is the vstrcat function, rewritten to use &lt;varargs.h>: #include &lt;stdio.h> #include &lt;varargs.h> #include &lt;string.h> exte...
2015-06-10, 1136👍, 0💬

I had a frustrating problem which turned out to be caused by the line ....
I had a frustrating problem which turned out to be caused by the line printf("%d", n); where n was actually a long int. I thought that ANSI function prototypes were supposed to guard against argument type mismatches like this. A:When a function accepts a variable number of arguments, its prototype d...
2016-01-11, 1134👍, 0💬

I had a frustrating problem which turned out to be caused by the line
I had a frustrating problem which turned out to be caused by the line printf("%d", n); where n was actually a long int. I thought that ANSI function prototypes were supposed to guard against argument type mismatches like this. When a function accepts a variable number of arguments, its prototype doe...
2015-06-15, 1133👍, 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, 1132👍, 0💬

My compiler is complaining that printf is undefined ...
My compiler is complaining that printf is undefined! How can this be? It's the world's most popular C function... Allegedly, there are C compilers for Microsoft Windows which do n ot support printf, on the argument that printf is for printing to old-fashioned terminals, while under Windows the right...
2015-07-03, 1131👍, 0💬

How can I make it pause before closing the program output window?
I'm compiling some test programs on a windows-based system, and the windows containing my program's output are closing so quickly after my program calls exit that I can't see the output. How can I make it pause before closing? After wondering why the author of your compiler's run-time system didn't ...
2015-04-24, 1131👍, 0💬

I keep getting errors due to library functions being undefined, but I am including all the right header files.
I keep getting errors due to library functions being undefined, but I am including all the right header files. In the general case of calling code in an external library, using #include to pull in the right header file(s) is only half of the story; you also have to tell the linker to search the exte...
2015-07-14, 1130👍, 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, 1130👍, 0💬

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