Tag: c89

C89中的可变长度数组?

我已经读过C89不支持可变长度数组,但以下实验似乎反驳了: #include int main() { int x; printf(“Enter a number: “); scanf(“%d”, &x); int a[x]; a[0] = 1; // … return 0; } 当我这样编译时(假设filename是va_test.c ): gcc va_test.c -std=c89 -o va_test 有用… 我错过了什么? 🙂

C:将double转换为float,保留小数点精度

我想在C中将double转换为float,但希望尽可能保留小数点而不做任何更改… 例如,让我说我有 double d = 0.1108; double dd = 639728.170000; double ddd = 345.2345678 现在纠正我,如果我错了,我知道点后面的浮点精度大约是5个数字。 我可以在点之后得到那五个数字吗? 以上结果如下: float f = x(d); float ff = x(dd); float fff = x(ddd); printf(“%f\n%f\n%f\n”, f, ff, fff); 它应该打印 0.1108 639728.17000 345.23456 精度限制之后的所有数字(我假设为5)将被截断。

C89,混合变量声明和代码

我很好奇,当你尝试混合变量声明和代码时,为什么C89编译器会转储给你,例如: rutski@imac:~$ cat test.c #include int main(void) { printf(“Hello World!\n”); int x = 7; printf(“%d!\n”, x); return 0; } rutski@imac:~$ gcc -std=c89 -pedantic test.c test.c: In function ‘main’: test.c:7: warning: ISO C90 forbids mixed declarations and code rutski@imac:~$ 是的,你可以避免这种事情,远离-pedantic。 但是,您的代码不再符合标准。 正如任何能够回答这篇文章的人可能已经知道的那样,这不仅仅是一个理论上的问题。 像Microsoft的C编译器这样的平台在任何和所有情况下都可以在标准中快速执行。 鉴于古老的C是多么的,我认为这个特征是由于一些历史问题可以追溯到70年代的非凡硬件限制,但我不知道细节。 或者我完全错了吗?

如何将C-struct指定的初始化程序重写为C89(resp MSVC C编译器)

伙计们,我有这个问题: 通常在C99 GCC(cygwin / MinGW / linux)中,C struct中的初始化器有点符号语法。 像这样: //HELP ME HOW TO REWRITE THIS (in most compact way) to MSVC static struct my_member_t my_global_three[] = { {.type = NULL, .name = “one”}, {.type = NULL, .name = “two”}, {.type = NULL, .name = “three”}, }; 将my_memeber_t定义在头文件中: struct my_member_t { struct complex_type * type; char […]

混合声明和代码

当我使用“gcc -o dene -Wall -ansi -pedantic-errors dene.c”编译函数时,gcc不会发出任何错误。(你可以看一下以char开头的行,在if循环中) static void remove_negation(char *s,char *s1) { char **cmainp=malloc(sizeof(char*)*1); int len=0;int d=0; int i=0; cmainp[0]=malloc(sizeof(char)*300); len=strlen(s); for(i=0;i<len;++i) { if(s[i]=='-') if(i==0 || s[i-1]==',') /*look*/ {char *p=malloc(sizeof(char)*3); /*look*/ ++i; p[0]=s[i]; p[1]='\0'; strcat(s1,","); strcat(s1,p); free(p); continue; } cmainp[0][d]=s[i]; ++d; } cmainp[0][d+1]='\0'; strcpy(cmainp[0],s); free(cmainp[0]); } 但是,当编译上面的函数用gcc重新格式化时,gcc会发出该错误; “dene.c:10:错误:ISO C90禁止混合声明和代码” static void remove_negation(char *s,char *s1) […]

符合旧“struct hack”(?)的变体

我相信我已经找到了一种方法来实现便携式C89中众所周知的“struct hack”。 我很好奇这是否真的严格符合C89。 主要思想是:我分配足够大的内存来保存初始结构和数组元素。 确切的大小是(K + N) * sizeof(array_base_type) ,其中选择K * sizeof(array_base_type) >= sizeof(the_struct)使得K * sizeof(array_base_type) >= sizeof(the_struct)并且N是数组元素的数量。 首先,我取消引用malloc()返回以存储the_struct指针,然后使用指针算法获取指向结构后面的数组开头的指针。 一行代码值超过一千字,所以这里是一个最小的实现: typedef struct Header { size_t length; /* other members follow */ } Header; typedef struct Value { int type; union { int intval; double fltval; } v; } Value; /* round up to nearest multiple […]

为什么在设置-std = c99时gcc不能找到random()接口?

我在源代码的顶部做”#include ” 。 示例编译: /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$ 当我关闭-std = c99时,无法找到函数isfinite()。 所以我确实想要使用-std = c99这个和其他原因。 有什么技巧我不见了?

在while循环中使用scanf函数

我正在尝试为编程分配格式化以空格分隔的用户输入。 本质上,输入由任意数量的表达式组成 L integer integer integer integer和C integer integer integer 。 例如: L 1 1 5 7 C 4 5 3 。 到目前为止,我已经设法根据初始字符提取整数,并可以使用scanf函数迭代字符串: char a; while(scanf(“%c”, &a) == 1){ if(a == ‘C’){ int inputX, inputY, inputR; scanf(“%d %d %d”, &inputX, &inputY, &inputR); printf(“%d %d %d\n”, inputX, inputY, inputR); } else if(a == ‘L’){ int x1, […]

ANSI C中的无缓冲I / O.

为了教育和编程实践,我想编写一个简单的库,可以处理原始键盘输入,并“实时”输出到终端。 我想尽可能地坚持使用ansi C,我只是不知道从哪里开始这样的事情。 我已经完成了几次谷歌搜索,99%的结果使用了库,或者是用于C ++。 我真的很想让它在Windows中运行,然后在我有空的时候将它移植到OSX。

无法摆脱“此十进制常量仅在ISO C90中无符号”警告

我在我的哈希表实现上使用FNV哈希作为哈希算法,但我在这行的问题标题中收到警告: unsigned hash = 2166136261; 我不明白为什么会这样,因为当我这样做时: printf(“%u\n”, UINT_MAX); printf(“2166136261\n”); 我明白了: 4294967295 2166136261 这似乎是在我的机器的限制下…… 为什么我会收到警告,有什么方法可以摆脱它?