<< < 168 169 170 171 172 173 174 >   Sort: Date

I am using scanf c to read a Y/N response
I'm using scanf %c to read a Y/N response, but later input gets skipped. You wanted scanf %c to read a single character, and it tried to, but when you tried to type that single character at it, before the rest of the input system would accept it, you had to hit the RETURN key, too. scanf read only t...
2015-10-21, 1059👍, 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, 1059👍, 0💬

I am trying to port this old program. Why do I get undefined external errors for some library functions?
I am trying to port this old program. Why do I get undefined external errors for some library functions? Some old or semistandard functions have been renamed or replaced over the years; if you need:/you should instead: index use strchr. rindex use strrchr. bcopy use memmove, after interchanging the ...
2015-07-16, 1058👍, 0💬

What is the difference between ...
What's the difference between const MAXSIZE = 100; and #define MAXSIZE 100 A preprocessor #define gives you a true compile-time constant. In C, const gives you a run-time object which you're not supposed to try to modify; ``const'' really means ``readonly''.
2016-02-18, 1055👍, 0💬

What's the difference between...?
What's the difference between #include &lt;> and #include "" ? The syntax is typically used with Standard or system-supplied headers, while "" is typically used for a program's own header files.
2016-02-16, 1054👍, 0💬

When I read numbers from the keyboard with scanf ...
When I read numbers from the keyboard with scanf and a "%d\n" format, like this: int n; scanf("%d\n", &amp;n); printf("you typed %d\n", n); it seems to hang until I type one extra line of input. Perhaps surprisingly, \n in a scanf format string does not mean to expect a newline, but rather to re...
2015-10-23, 1051👍, 0💬

Why does everyone say not to use gets?
Why does everyone say not to use gets? Unlike fgets(), gets() cannot be told the size of the buffer it's to read into, so it cannot be prevented from overflowing that buffer if an input line is longer than expected--and Murphy's Law says that, sooner or later, a larger-than-expected input line will ...
2015-10-16, 1050👍, 0💬

How can I add N days to a date? How can I find the difference between two dates?
How can I add N days to a date? How can I find the difference between two dates? The ANSI/ISO Standard C mktime and difftime functions provide some (limited) support for both problems. mktime accepts non-normalized dates, so it is straightforward to take a filled-in struct tm, add or subtract from t...
2015-08-05, 1048👍, 0💬

Why doesnt that code work?
Why doesn't the code short int s; scanf("%d", &s); work? When converting %d, scanf expects a pointer to an int. To convert to a short int, use %hd .
2015-10-28, 1044👍, 0💬

How do I convert a string to all upper or lower case?
How do I convert a string to all upper or lower case? Some libraries have routines strupr and strlwr or strupper and strlower, but these are not Standard or portable. It's a straightforward exercise to write upper/lower-case functions in terms of the toupper and tolower macros in &lt;ctype.h>;(T...
2015-08-19, 1043👍, 0💬

If fflush wont work, what can I use to flush input?
If fflush wont work, what can I use to flush input? It depends on what you're trying to do. If you're trying to get rid of an unread newline or other unexpected input after calling scanf you really need to rewrite or replace the call to scanf. Alternatively, you can consume the rest of a partially-r...
2015-10-12, 1041👍, 0💬

How can I get the current date or time of day in a C program?
How can I get the current date or time of day in a C program? Just use the time, ctime, localtime and/or strftime functions. Here is a simple example: #include &lt;stdio.h> #include &lt;time.h> int main() { time_t now; time(&amp;now); printf("It's %s", ctime(&amp;now)); return 0; } C...
2015-08-07, 1040👍, 0💬

How can I change their mode to binary?
I'm writing a ``filter'' for binary files, but stdin and stdout are preopened as text streams. How can I change their mode to binary? There is no standard way to do this. On Unix-like systems, there is no text/binary distinction, so there is no need to change the mode. Some MS-DOS compilers supply a...
2015-09-21, 1039👍, 0💬

How can I delete a file?
How can I delete a file? The Standard C Library function is remove. (This is therefore one of the few questions in this section for which the answer is not ``It's system-dependent.'') On older, pre-ANSI Unix systems, remove may not exist, in which case you can try unlink.
2015-04-10, 1037👍, 0💬

I need code to parse and evaluate expressions.
I need code to parse and evaluate expressions. Two available packages are ``defunc,'' posted to comp.sources.misc in December, 1993 (V41 i32,33), to alt.sources in January, 1994, and available from sunsite.unc.edu in pub/packages/development/libra ries/defunc-1.3.tar.Z,and ``parse,'' at lamont.ldgo....
2015-05-01, 1035👍, 0💬

How can I read/write structures from/to data files?
How can I read/write structures from/to data files? It is relatively straightforward to write a structure out using fwrite: fwrite(&amp;somestruct, sizeof somestruct, 1, fp); and a corresponding fread invocation can read it back in. What happens here is that fwrite receives a pointer to the stru...
2015-09-18, 1032👍, 0💬

Why is p turning out const, instead of the characters pointed to?
I've got the declarations typedef char *charp; const charp p; Why is p turning out const, instead of the characters pointed to? typedef substitutions are not purely textual.In the declaration const charp p; p is const for the same reason that const int i declares i as const. The typedef'ed declarati...
2016-01-04, 1027👍, 0💬

I thought I would check errno after a long string of printf calls ...
I thought I'd check errno after a long string of printf calls, to see if any of them had failed: errno = 0; printf("This\n"); printf("is\n"); printf("a\n"); printf("test.\n"); if(errno != 0) fprintf(stderr, "printf failed: %s\n", strerror(errno)); Why is it printing something strange like ``printf f...
2015-10-14, 1026👍, 0💬

I need a random true/false value ...
I need a random true/false value, so I'm just taking rand() % 2, but it's alternating 0, 1, 0, 1, 0... Poor pseudorandom number generators (such as the ones unfortunately supplied with some systems) are not very random in the low-order bits. (In fact, for a pure linear congruential random number gen...
2015-07-24, 1020👍, 0💬

I am trying to update a file in place ...
I'm trying to update a file in place, by using fopen mode "r+", reading a certain string, and writing back a modified string, but it's not working. Be sure to call fseek before you write, both to seek back to the beginning of the string you're trying to overwrite, and because an fseek or fflush is a...
2015-10-07, 1014👍, 0💬

I know that the library function localtime will convert ...
I know that the library function localtime will convert a time_t into a broken-down struct tm, and that ctime will convert a time_t to a printable string. How can I perform the inverse operations of converting a struct tm or a string into a time_t? ANSI C specifies a library function, mktime, which ...
2015-08-05, 1011👍, 0💬

How can I get back to the interactive keyboard if stdin is redirected?
I'm trying to write a program like ``more.'' How can I get back to the interactive keyboard if stdin is redirected? There is no portable way of doing this. Under Unix, you can open the special file /dev/tty. Under MS-DOS, you can try opening the ``file'' CON, or use routines or BIOS calls such as ge...
2015-09-29, 1003👍, 0💬

How can I flush pending input so that a user ...
How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work? fflush is defined only for output streams. Since its definition of ``flush'' is to complete the writing of buffered characters (not to discard them), discarding unread input would not be ...
2015-10-12, 1001👍, 0💬

How can I tell if standard input or output is redirected
How can I tell if standard input or output is redirected (i.e. whether ``&lt;'' or ``&gt;'' was used on the invocation command line)? You can't tell directly, but you can usually look at a few other things to make whatever decision you need to. If you want your program to take input from std...
2015-10-01, 1001👍, 0💬

<< < 168 169 170 171 172 173 174 >   Sort: Date