<< < 5 6 7 8 9 10 11 12 13 14 15 > >>   Sort: Rank

How can I handle floating-point exceptions gracefully?
How can I handle floating-point exceptions gracefully? On many systems, you can define a function matherr which will be called when there are certain floating-point errors, such as errors in the math routines in . You may also be able to use signal to catch SIGFPE.
2015-06-21, 599👍, 0💬

What is a good way to implement complex numbers in C?
What is a good way to implement complex numbers in C? It is straightforward to define a simple structure and some arithmetic functions to manipulate them. C99 supports complex as a standard type. Here is a tiny example, to give you a feel for it: typedef struct { double real; double imag; } complex;...
2015-06-19, 767👍, 0💬

I am having trouble with a Turbo C program which crashes
I'm having trouble with a Turbo C program which crashes and says something like ``floating point formats not linked.'' Some compilers for small machines, including Turbo C (and Ritchie's original PDP-11 compiler), leave out certain floating point support if it looks like it will not be needed. In pa...
2015-06-19, 608👍, 0💬

I heard that you have to include stdio.h before calling printf. Why?
I heard that you have to include stdio.h before calling printf. Why? So that a proper prototype for printf will be in scope. A compiler may use a different calling sequence for functions which accept variable-length argument lists. (It might do so if calls using variable-length argument lists were l...
2015-06-17, 598👍, 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, 603👍, 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, 580👍, 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 <stdarg.h> header. Here is a function which concatenates an arbitrary number of strings into malloc'ed memory: #include <stdlib.h> /* for malloc, NULL, size_t */ #include <stdarg.h> /* for va_ st...
2015-06-15, 586👍, 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, 640👍, 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, 587👍, 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, <varargs.h>, which offers about the same functionality. Here is the vstrcat function, rewritten to use <varargs.h>: #include <stdio.h> #include <varargs.h> #include <string.h> extern char *malloc(); c...
2015-06-10, 625👍, 0💬

How can I discover how many arguments a function was actually called with?
How can I discover how many arguments a function was actually called with? This information is not available to a portable program. Some old systems provided a nonstandard nargs function, but its use was always questionable, since it typically returned the number of words passed, not the number of a...
2015-06-10, 667👍, 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, 601👍, 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, 629👍, 0💬

I cant get va_arg to pull in an argument of type pointer-to-function.
I cant get va_arg to pull in an argument of type pointer-to-function. Try using a typedef for the function pointer type. The type-rewriting games which the va_arg macro typically plays are stymied by overly-complicated types such as pointer-to-function. To illustrate, a simplified implementation of ...
2015-06-05, 658👍, 0💬

How can I write a function which takes a variable number of arguments
How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)? In general, you cannot. Ideally, you should provide a version of that other function which accepts a va_list pointer. Suppose you want to write a...
2015-06-05, 638👍, 0💬

How can I call a function with an argument list built up at run time?
How can I call a function with an argument list built up at run time? There is no guaranteed or portable way to do this. Instead of an actual argument list, you might consider passing an array of generic (void *) pointers. The called function can then step through the array, much like main() might s...
2015-06-03, 681👍, 0💬

Why is this loop always executing once?
Why is this loop always executing once? for(i = start; i < 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-enclosed b...
2015-06-03, 653👍, 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, 624👍, 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, 613👍, 0💬

This program crashes before it even runs!
This program crashes before it even runs! (When single-stepping with a debugger, it dies before the first statement in main.) You probably have one or more very large (kilobyte or more) local arrays. Many systems have fixed-size stacks, and even those which perform dynamic stack allocation automatic...
2015-05-29, 638👍, 0💬

I have a program that seems to run correctly
I have a program that seems to run correctly, but it crashes as it's exiting, after the last statement in main(). What could be causing this? A:There are at least three things to look for: 1. If a semicolon in a previous declaration is missing, main might be inadvertently declared as returning a str...
2015-05-29, 638👍, 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, 643👍, 0💬

Why does this code crash?
Why does this code: char *p = "hello, world!"; p[0] = 'H'; crash? String constants are in fact constant. The compiler may place them in nonwritable storage, and it is therefore not safe to modify them. When you need writable strings, you must allocate writable memory for them, either by declaring an...
2015-05-27, 629👍, 0💬

I have seen function declarations that look like this
I've seen function declarations that look like this: extern int func __((int, int)); What are those extra parentheses and underscores for? They're part of a trick which allows the prototype part of the function declaration to be turned off for a pre-ANSI compiler. Somewhere else is a conditional def...
2015-05-23, 793👍, 0💬

<< < 5 6 7 8 9 10 11 12 13 14 15 > >>   Sort: Rank