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

But I cant use all these nonstandard, system-dependent functions, because my program has to be ANSI compatible!
But I cant use all these nonstandard, system-dependent functions, because my program has to be ANSI compatible! You're out of luck. Either you misunderstood your requirement, or it's an impossible one to meet. ANSI/ISO Standard C simply does not define ways of doing these things; it is a language st...
2015-02-27, 1211👍, 0💬

Why isnt there a numbered, multi-level break statement to break out
Why isn't there a numbered, multi-level break statement to break out of several loops at once? What am I supposed to use instead, a goto? A:First, remember why it is that break and continue exist at all--they are, in effect, ``structured gotos'' used in preference to goto (and accepted as alternativ...
2015-01-30, 1211👍, 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, 1210👍, 0💬

How do I read the arrow keys? What about function keys?
How do I read the arrow keys? What about function keys? Terminfo, some versions of termcap, and some versions of curses have support for these non-ASCII keys. Typically, a special key sends a multicharacter sequence (usually beginning with ESC, '\033'); parsing these can be tricky. (curses will do t...
2015-04-24, 1210👍, 0💬

How can I ensure that integer arithmetic doesnt overflow?
How can I ensure that integer arithmetic doesnt overflow? The usual approach is to test the operands against the limits in the header file &lt;limits.h> before doing the operation. For example, here is a ``careful'' addition function: int chkadd(int a, int b) { if(INT_MAX - b &lt; a) { fputs...
2015-03-04, 1210👍, 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, 1208👍, 0💬

How can I automatically locate a programs configuration files in the same directory as the executable?
How can I automatically locate a programs configuration files in the same directory as the executable? It's hard, in general; Even if you can figure out a workable way to do it, you might want to consider making the program's auxiliary (library) directory configurable, perhaps with an environment va...
2015-03-13, 1208👍, 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, 1207👍, 0💬

How do I copy files?
How do I copy files? Either use system() to invoke your operating system's copy utility, or open the source and destination files (using fopen or some lower-level file-opening system call), read characters or blocks of characters from the source file, and write them to the destination file. Here is ...
2015-04-08, 1207👍, 0💬

How can I call system when parameters (filenames, etc.) of the executed command arent known until run time?
How can I call system when parameters (filenames, etc.) of the executed command arent known until run time? Just use sprintf (or perhaps strcpy and strcat) to build the command string in a buffer, then call system with that buffer. (Make sure the buffer is allocated with enough space; Here is a cont...
2015-03-18, 1207👍, 0💬

People claim that optimizing compilers are good and that we no longer have to write things in assembler for speed
People claim that optimizing compilers are good and that we no longer have to write things in assembler for speed, but my compiler can't even replace i/=2 with a shift. A: Was i signed or unsigned? If it was signed, a shift is not equivalent (hint: think about the result if i is negative and odd), s...
2015-02-06, 1206👍, 0💬

What is the ANSI C Standard?
What is the ANSI C Standard? In 1983, the American National Standards Institute (ANSI) commissioned a committee, X3J11, to standardize the C language. After a long, arduous process, including several widespread public reviews, the committee's work was finally ratified as ANS X3.159-1989 on December ...
2016-01-15, 1205👍, 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, 1205👍, 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, 1203👍, 0💬

What printf format should I use for a typedef like size_t when I don't know whether it is long or some other type?
What printf format should I use for a typedef like size_t when I don't know whether it is long or some other type? Use a cast to convert the value to a known, conservatively-sized type, then use the printf format matching that type. For example, to print the size of a type, you might use printf("%lu...
2015-11-04, 1203👍, 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, 1203👍, 0💬

How can I find the day of the week given the date?
How can I find the day of the week given the date? Here are three methods: 1. Use mktime or localtime # . Here is a code fragment which computes the day of the week for February 29, 2000: #include &lt;stdio.h> #include &lt;time.h> char *wday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "...
2015-01-07, 1202👍, 0💬

I am trying to do some simple trig...
I'm trying to do some simple trig, and I am #including &lt;math.h>, but the linker keeps complaining that functions like sin and cos are undefined. Make sure you're actually linking with the math library. For instance, due to a longstanding bug in Unix and Linux systems, you usually need to use ...
2015-07-01, 1201👍, 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, 1201👍, 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, 1200👍, 0💬

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

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