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

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 &lt;math.h>. You may also be able to use signal to catch SIGFPE
2015-03-04, 1171👍, 0💬

When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999? Most computers use base 2 for floating-point numbers as well as for integers, and just as for base 10, not all fractions are representable exactly in base 2. It's well-known that in base 10, a fraction like 1/3 = 0.333...
2015-07-03, 1170👍, 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, 1168👍, 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, 1168👍, 0💬

Should I use symbolic names like TRUE and FALSE for Boolean constants, or plain 1 and 0?
Should I use symbolic names like TRUE and FALSE for Boolean constants, or plain 1 and 0? It's your choice. Preprocessor macros like TRUE and FALSE (and, of course, NULL) are used for code readability, not because the underlying values might ever change. It's a matter of style, not correctness, wheth...
2016-02-29, 1167👍, 0💬

What is the right way to use errno?
What is the right way to use errno? In general, you should detect errors by checking return values, and use errno only to distinguish among the various causes of an error, such as ``File not found'' or ``Permission denied''. (Typically, you use perror or strerror to print these discriminating error ...
2015-02-23, 1167👍, 0💬

I am reading strings typed by the user into an array ...
I'm reading strings typed by the user into an array, and then printing them out later. When the user types a sequence like \n, why isn't it being handled properly? Character sequences like \n are interpreted at compile time. When a backslash and an adjacent n appear in a character constant or string...
2016-03-04, 1166👍, 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, 1166👍, 0💬

Which is more efficient, a switch statement or an if else chain?
Which is more efficient, a switch statement or an if else chain? The differences, if any, are likely to be slight. The switch statement was designed to be efficiently implementable, though the compiler may choose to use the equivalent of an if/else chain (as opposed to a compact jump table) if the c...
2015-02-04, 1165👍, 0💬

Why cant I pass a char to a function which expects a const char ?
Why cant I pass a char to a function which expects a const char ? You can use a pointer-to-T (for any type T) where a pointer-to-const-T is expected. However, the rule (an explicit exception) which permits slight mismatches in qualified pointer types is not applied recursively, but only at the top l...
2016-01-04, 1164👍, 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, 1163👍, 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, 1162👍, 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, 1162👍, 0💬

How can I convert integers to binary or hexadecimal?
How can I convert integers to binary or hexadecimal? Make sure you really know what you're asking. Integers are stored internally in binary, although for most purposes it is not incorrect to think of them as being in octal, decimal, or hexadecimal, whichever is convenient. The base in which a number...
2015-02-13, 1162👍, 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, 1161👍, 0💬

People seem to make a point of distinguishing between ...
People seem to make a point of distinguishing between implementation-defined, unspecified, and undefined behavior. What do these mean? First of all, all three of these represent areas in which the C Standard does not specify exactly what a particular construct, or a program which uses it, must do. T...
2015-11-23, 1161👍, 0💬

There seem to be a few missing operators ..
There seem to be a few missing operators, like ^^, &amp;&amp;=, and -&gt;=. A:A logical exclusive-or operator (hypothetically ``^^'') would be nice, but it couldn't possibly have short-circuiting behavior analogous to &amp;&amp; and || Similarly, it's not clear how short-circuiti...
2015-01-28, 1161👍, 0💬

How can I list all of the predefined identifiers?
How can I list all of the predefined identifiers? There's no standard way, although it is a common need. gcc provides a -dM option which works with -E, and other compilers may provide something similar. If the compiler documentation is unhelpful, the most expedient way is probably to extract printab...
2016-01-27, 1160👍, 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, 1160👍, 0💬

How can I use a preprocessorif expression to ? ....
How can I use a preprocessor #if expression to tell whether a machine's byte order is big-endian or little-endian? You probably can't. The usual techniques for detecting endianness involve pointers or arrays of char, or maybe unions, but preprocessor arithmetic uses only long integers, and there is ...
2016-02-01, 1159👍, 0💬

What does pragma once mean? I found it in some header files.
What does pragma once mean? I found it in some header files. It is an extension implemented by some preprocessors to help make header files idempotent; that is, to make sure that their contents are processed exactly once even if they are #included multiple times.Some people claim that #pragma once c...
2015-12-11, 1159👍, 0💬

Why does everyone say not to use scanf? What should I use instead?
Why does everyone say not to use scanf? What should I use instead? scanf has a number of problems, its %s format has the same problem that gets() has --it's hard to guarantee that the receiving buffer won't overflow. More generally, scanf is designed for relatively structured, formatted input (its n...
2015-10-21, 1159👍, 0💬

If NULL and 0 are equivalent as null pointer constants, which should I use?
If NULL and 0 are equivalent as null pointer constants, which should I use? Many programmers believe that NULL should be used in all pointer contexts, as a reminder that the value is to be thought of as a pointer. Others feel that the confusion surrounding NULL and 0 is only compounded by hiding 0 b...
2015-05-11, 1159👍, 0💬

How can I use a macro argument inside a string literal in the macro expansion?
How can I use a macro argument inside a string literal in the macro expansion? Some pre-ANSI compilers/preprocessors interpreted macro definitions like #define TRACE(var, fmt) printf("TRACE: var = fmt\n", var) such that invocations like TRACE(i, %d); were expanded as printf("TRACE: i = %d\n", i); In...
2016-01-22, 1158👍, 0💬

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