c89

Unbuffered I/O in ANSI C

对着背影说爱祢 提交于 2019-11-28 01:55:10
For the sake of education, and programming practice, I'd like to write a simple library that can handle raw keyboard input, and output to the terminal in 'real time'. I'd like to stick with ansi C as much as possible, I just have no idea where to start something like this. I've done several google searches, and 99% of the results use libraries, or are for C++. I'd really like to get it working in windows, then port it to OSX when I have the time. Sticking with Standard C as much as possible is a good idea, but you are not going to get very far with your adopted task using just Standard C. The

C: convert double to float, preserving decimal point precision

蓝咒 提交于 2019-11-27 23:08:17
问题 i wanted to convert double to float in C, but wanted to preserve the decimal point exactly as possible without any changes... for example, let's say i have double d = 0.1108; double dd = 639728.170000; double ddd = 345.2345678 now correct me if i am wrong, i know that floating point precision is about 5 numbers after the dot. can i get those five numbers after the dot exactly as the double had it? so that above results as follows: float f = x(d); float ff = x(dd); float fff = x(ddd); printf("

Conforming variant of the old “struct hack” (?)

五迷三道 提交于 2019-11-27 21:43:06
问题 I believe I've found a way to achieve something like the well-known "struct hack" in portable C89. I'm curious if this really strictly conforms to C89. The main idea is: I allocate memory large enough to hold an initial struct and the elements of the array. The exact size is (K + N) * sizeof(array_base_type) , where K is chosen so that K * sizeof(array_base_type) >= sizeof(the_struct) and N is the number of array elements. First, I dereference the pointer that malloc() returned to store the

What's the difference between “int” and “int_fast16_t”?

大城市里の小女人 提交于 2019-11-27 20:23:24
As I understand it, the C specification says that type int is supposed to be the most efficient type on target platform that contains at least 16 bits. Isn't that exactly what the C99 definition of int_fast16_t is too? Maybe they put it in there just for consistency, since the other int_fastXX_t are needed? Update To summarize discussion below: My question was wrong in many ways. The C standard does not specify bitness for int . It gives a range [-32767,32767] that it must contain. I realize at first most people would say, "but that range implies at least 16-bits!" But C doesn't require two's

Is the behavior of subtracting two NULL pointers defined?

两盒软妹~` 提交于 2019-11-27 20:20:39
Is the difference of two non-void pointer variables defined (per C99 and/or C++98) if they are both NULL valued? For instance, say I have a buffer structure that looks like this: struct buf { char *buf; char *pwrite; char *pread; } ex; Say, ex.buf points to an array or some malloc'ed memory. If my code always ensures that pwrite and pread point within that array or one past it, then I am fairly confident that ex.pwrite - ex.pread will always be defined. However, what if pwrite and pread are both NULL. Can I just expect subtracting the two is defined as (ptrdiff_t)0 or does strictly compliant

Warning: this decimal constant is unsigned only in ISO C90

六月ゝ 毕业季﹏ 提交于 2019-11-27 19:49:46
Piece of code : long rangeVar = 0; rangeVar = atol(p_value); if (rangeVar >= -2147483648 && rangeVar <= 2147483647) On compiling I get: warning: this decimal constant is unsigned only in ISO C90 Thanks in Advance The rules for the types of decimal integer constants changed between the 1990 and 1999 editions of the ISO C standard. In the 1990 version, an unsuffixed decimal integer constant's type is the first of int , long int , or unsigned long int in which its value can be represented. (C90 had no long long or unsigned long long type). In the 1999 and 2011 versions, its type is one of int ,

C check if file exists

和自甴很熟 提交于 2019-11-27 19:22:23
问题 In a project I have to do in C89 standard I have to check if a file exists. How do I do this? I thought of using FILE *file; if ((file = fopen(fname, "r")) == NULL) { printf("file doesn't exists"); } return 0; but I think there can be more cases then file doesn't exists that will do fopen == NULL. How do I do this? I prefer not using includes rather then . 回答1: If you can't use stat() in your environment (which is definitely the better approach), just evaluate errno. Don't forget to include

Where can I find the C89/C90 standards in PDF format? [closed]

元气小坏坏 提交于 2019-11-27 16:45:57
I'm looking for a free copy version of the C89/C90 standard , but I can't find it anywhere! Why is so hard to find it? C99 and C11 standards are very easy to get a copy of on Internet. Even in Stack Overflow question Where do I find the current C or C++ standard documents? and in The C Standard, Obtaining the Standard don't contain what I'm looking for. Web searches didn't helped either, nor Open Standards . R.. You can find nice HTML versions of C89, C99, and C11, as well as some of the official draft PDF files they're generated from, here: http://port70.net/~nsz/c/ Some other useful direct

Problem trying to use the C qsort function

妖精的绣舞 提交于 2019-11-27 16:28:44
问题 #include <stdio.h> #include <stdlib.h> float values[] = { 4, 1, 10, 9, 2, 5, -1, -9, -2,10000,-0.05,-3,-1.1 }; int compare (const void * a, const void * b) { return ( (int) (*(float*)a - *(float*)b) ); } int main () { int i; qsort (values, 13, sizeof(float), compare); for (i = 0; i < 13; i++) { printf ("%f ",values[ i ]); } putchar('\n'); return 0; } The result is: -9.000000 -3.000000 -2.000000 -1.000000 -1.100000 -0.050000 1.000000 2.000000 4.000000 5.000000 9.000000 10.000000 10000.000000

Why can't gcc find the random() interface when -std=c99 is set?

不想你离开。 提交于 2019-11-27 14:38:19
问题 I do "#include <stdlib.h>" at the top of the source. Example compilation: /usr/bin/colorgcc -std=c99 -fgnu89-inline -g -Wall -I/usr/include -I./ -I../ -I../../ -I../../../ -I../../../../ -O3 -o f8 f8.c In file included from f8.c:7: ctype-cmp.c: In function ‘randomized’: ctype-cmp.c:48: warning: implicit declaration of function ‘random’ ctype-cmp.c: In function ‘main’: ctype-cmp.c:153: warning: implicit declaration of function ‘srandom’ ais@xcalibur:t$ When I turn off -std=c99, the function