<< < 4 5 6 7 8 9 10 11 12 13 14 > >>   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, 1172👍, 0💬

Can I declare main as void, to shut off these annoying ....
Can I declare main as void, to shut off these annoying ``main returns no value'' messages? No. main must be declared as returning an int, and as taking either zero or two arguments, of the appropriate types. If you're calling exit() but still getting warnings, you may have to insert a redundant retu...
2015-12-20, 1170👍, 0💬

Why are some ANSI ISO Standard library functions showing you
Why are some ANSI/ISO Standard library functions showing up as undefined, even though I've got an ANSI compiler? It's possible to have a compiler available which accepts ANSI syntax, but not to have ANSI-compatible header files or run-time libraries installed. (In fact, this situation is rather comm...
2015-11-30, 1170👍, 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, 1170👍, 0💬

Why doesnt strcat work? ....
Why doesn't strcat(string, '!'); work? There is a very real difference between characters and strings, and strcat concatenates strings. A character constant like '!' represents a single character. A string literal between double quotes usually represents multiple characters. A string literal like "!...
2016-03-14, 1169👍, 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, 1168👍, 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, 1168👍, 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, 1167👍, 0💬

How can I invoke another program or command and trap its output?
How can I invoke another program or command and trap its output? Unix and some other systems provide a popen function, which sets up a stdio stream on a pipe connected to the process running a command, so that the calling program can read the output (or alternatively supply the input). Using popen, ...
2015-03-16, 1167👍, 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, 1167👍, 0💬

How can I generate floating-point random numbers?
How can I generate floating-point random numbers? drand48 is a Unix System V routine which returns floating point random numbers (presumably with 48 bits of precision) in the half-open interval [0, 1). (Its companion seed routine is srand48; neither is in the C Standard.) It's easy to write a low-pr...
2015-07-22, 1166👍, 0💬

How can I read a directory in a C program?
How can I read a directory in a C program? See if you can use the opendir and readdir functions, which are part of the POSIX standard and are available on most Unix variants. Implementations also exist for MS-DOS, VMS, and other systems. (MS-DOS also has FINDFIRST and FINDNEXT routines which do esse...
2015-04-01, 1166👍, 0💬

Is there anything like an ifdef for typedefs?
Is there anything like an ifdef for typedefs? Unfortunately, no. (There can't be, because types and typedefs haven't been parsed at preprocessing time.) You may have to keep sets of preprocessor macros (e.g. MY_TYPE_DEFINED) recording whether certain typedefs have been declared.
2016-02-01, 1165👍, 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, 1165👍, 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, 1164👍, 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, 1164👍, 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, 1163👍, 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, 1162👍, 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, 1161👍, 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, 1160👍, 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, 1159👍, 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, 1159👍, 0💬

How can I specify a variable width in a scanf format string?
How can I specify a variable width in a scanf format string? You can't; an asterisk in a scanf format string means to suppress assignment. You may be able to use ANSI stringizing and string concatenation to construct a constant format specifier based on a preprocessor macro containing the desired wi...
2015-10-26, 1158👍, 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, 1158👍, 0💬

<< < 4 5 6 7 8 9 10 11 12 13 14 > >>   Sort: Date