<< < 1 2 3 4 5 6 7 8 9 10 > >>   Sort: Date

If I have a char * variable pointing to the name of a function ...
If I have a char * variable pointing to the name of a function, how can I call that function? Code like extern int func(int, int); char *funcname = "func"; int r = (*funcname)(1, 2); or r = (*(int (*)(int, int))funcname)(1, 2); doesn't seem to work. By the time a program is running, information abou...
2015-02-20, 1434👍, 0💬

How does free know how many bytes to free?
How does free know how many bytes to free? The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. (Typically, the size is stored adjacent to the allocated block, which is why things usually break badly if the ...
2016-03-24, 1429👍, 0💬

How can I print a character in a printf format string?
How can I print a '%' character in a printf format string? I tried \%, but it didn't work. Simply double the percent sign: %% . The reason it's tricky to print % signs with printf is that % is essentially printf's escape character. Whenever printf sees a %, it expects it to be followed by a characte...
2015-11-09, 1428👍, 0💬

I have a program which mallocs and later frees a lot of memory ...
I have a program which mallocs and later frees a lot of memory, but I can see from the operating system that memory usage doesn't actually go back down. Most implementations of malloc/free do not return freed memory to the operating system, but merely make it available for future malloc calls within...
2016-03-28, 1420👍, 0💬

How can I determine whether a machines byte order is big-endian or little-endian?
How can I determine whether a machines byte order is big-endian or little-endian? The usual techniques are to use a pointer: int x = 1; if(*(char *)&amp;x == 1) printf("little-endian\n"); else printf("big-endian\n"); or a union: union { int i; char c[sizeof(int)]; } x; x.i = 1; if(x.c[0] == 1) p...
2015-02-16, 1419👍, 0💬

What is C language?
What is C language? The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is one of the most widely used programming languages. C...
2014-12-31, 1409👍, 0💬

What is a good way to check for ``close enough'' floating-point equality?
What is a good way to check for ``close enough'' floating-point equality? Since the absolute accuracy of floating point values varies, by definition, with their magnitude, the best way of comparing two floating point values is to use an accuracy threshold which is relative to the magnitude of the nu...
2015-06-26, 1408👍, 0💬

How do I send escape sequences to control a terminal or other device?
How do I send escape sequences to control a terminal or other device? If you can figure out how to send characters to the device at all , it's easy enough to send escape sequences. In ASCII, the ESC code is 033 (27 decimal), so code like fprintf(ofd, "\033[J"); sends the sequence ESC [ J . Some prog...
2015-04-20, 1407👍, 0💬

Why isn't a pointer null after calling free? ...
Why isn't a pointer null after calling free? How unsafe is it to use (assign, compare) a pointer value after it's been freed? When you call free, the memory pointed to by the passed pointer is freed, but the value of the pointer in the caller probably remains unchanged, because C's pass-by-value sem...
2016-04-04, 1405👍, 0💬

Why wont the Frobozz Magic C Compiler ...
Why won't the Frobozz Magic C Compiler, which claims to be ANSI compliant, accept this code? I know that the code is ANSI, because gcc accepts it. Many compilers support a few non-Standard extensions, gcc more so than most. Are you sure that the code being rejected doesn't rely on such an extension?...
2015-11-25, 1405👍, 0💬

When I call malloc to allocate memory for a pointer which is local to a function ....
When I call malloc to allocate memory for a pointer which is local to a function, do I have to explicitly free it? Yes. Remember that a pointer is different from what it points to. Local variablesare deallocated when the function returns, but in the case of a pointer variable, this means that the po...
2016-03-31, 1400👍, 0💬

How can printf use f for type double, if scanf requires lf?
Someone told me it was wrong to use %lf with printf. How can printf use %f for type double, if scanf requires %lf? It's true that printf's %f specifier works with both float and double arguments Due to the ``default argument promotions'' (which apply in variable-length argument lists values of type ...
2015-11-06, 1396👍, 0💬

How can I split up a string into whitespace-separated fields?
How can I split up a string into whitespace-separated fields? How can I duplicate the process by which main() is handed argc and argv? The only Standard function available for this kind of ``tokenizing'' is strtok, although it can be tricky to use and it may not do everything you want it to. (For in...
2015-08-17, 1394👍, 0💬

I figured I could use scanf more safely if I checked its ...
I figured I could use scanf more safely if I checked its return value to make sure that the user typed the numeric values I expect: int n; while(1) { printf("enter a number: "); if(scanf("%d", &amp;n) == 1) break; printf("try again: "); } printf("you typed %d\n", n); but sometimes it seems to go...
2015-10-19, 1393👍, 0💬

Here is a good puzzle: how do you write a program which produces its own source code as output?
Here is a good puzzle: how do you write a program which produces its own source code as output? It is actually quite difficult to write a self-reproducing program that is truly portable, due particularly to quoting and character set difficulties. Here is a classic example (which ought to be presente...
2015-01-02, 1393👍, 0💬

What does the message warning macro replacement within a string literal mean?
What does the message warning macro replacement within a string literal mean? 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 ...
2015-12-14, 1381👍, 0💬

Why does the ANSI Standard place limits on the length and case-significance of external identifiers?
Why does the ANSI Standard place limits on the length and case-significance of external identifiers? The problem is linkers which are under control of neither the ANSI/ISO Standard nor the C compiler developers on the systems which have them. The limitation is only that identifiers be significant in...
2015-12-04, 1370👍, 0💬

Suggesting that there can be 62 seconds in a minute?
Suggesting that there can be 62 seconds in a minute? Q: Why can tm_sec in the tm structure range from 0 to 61, suggesting that there can be 62 seconds in a minute? A: That's actually a buglet in the Standard. There can be 61 seconds in a minute during a leap second. It's possible for there to be two...
2015-01-05, 1370👍, 0💬

How do I set variables to, or test for IEEE NaN?
How do I set variables to, or test for IEEE NaN (``Not a Number'') and other special values? Many systems with high-quality IEEE floating-point implementations provide facilities (e.g. predefined constants, and functions like isnan(), either as nonstandard extensions in &lt;math.h> or perhaps in...
2015-06-21, 1369👍, 0💬

What is the right type to use for Boolean values in C? ....
What is the right type to use for Boolean values in C? Is there a standard type? Should I use #defines or enums for the true and false values? Traditionally, C did not provide a standard Boolean type, partly and partly to allow the programmer to make the appropriate space/time tradeoff. (Using an in...
2016-03-02, 1366👍, 0💬

Why doesnt C have nested functions?
Why doesnt C have nested functions? It's not trivial to implement nested functions such that they have the proper access to local variables in the containing function(s), so they were deliberately left out of C as a simplification. (gcc does allow them, as an extension.) For many potential uses of n...
2015-01-14, 1365👍, 0💬

Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)?
Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)? No. The switch statement was originally designed to be quite simple for the compiler to translate, therefore case labels are limited to single, constant, integral expressions. You can attach several case labels t...
2015-02-02, 1355👍, 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, 1351👍, 0💬

What is the difference between these initializations? ....
What is the difference between these initializations? char a[] = "string literal"; char *p = "string literal"; My program crashes if I try to assign a new value to p[i]. A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways: 1. As the in...
2016-03-09, 1348👍, 0💬

<< < 1 2 3 4 5 6 7 8 9 10 > >>   Sort: Date