c89

C89 vs c99 GCC compiler

 ̄綄美尐妖づ 提交于 2019-11-29 13:28:56
问题 Is there a difference if I compile the following program using c89 vs c99? I get the same output. Is there really a difference between the two? #include <stdio.h> int main () { // Print string to screen. printf ("Hello World\n"); } gcc -o helloworld -std=c99 helloworld.c vs gcc -o helloworld -std=c89 helloworld.c 回答1: // comments are not a part of C89 but are OK in C99, falling off of main() without returning any value is equivalent to return 0; in C99, but not so in C89. From N1256 (pdf), 5

Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

…衆ロ難τιáo~ 提交于 2019-11-29 12:06:54
问题 Consider following example: #include <stdio.h> int main(void) { unsigned char a = 15; /* one byte */ unsigned short b = 15; /* two bytes */ unsigned int c = 15; /* four bytes */ long x = -a; /* eight bytes */ printf("%ld\n", x); x = -b; printf("%ld\n", x); x = -c; printf("%ld\n", x); return 0; } To compile I am using GCC 4.4.7 (and it gave me no warnings): gcc -g -std=c99 -pedantic-errors -Wall -W check.c My result is: -15 -15 4294967281 The question is why both unsigned char and unsigned

K&R Exercise 1.16 - Limitation on line length

断了今生、忘了曾经 提交于 2019-11-29 11:58:19
问题 I'm learning C from K&R's "The C Programming Language" book. I'm doing the exercises specified in the book. I'm on exercise number 1.16, but I don't understand it. Exercise 1.16: Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text. My questions: "...as much as possible of the text..." - is there some limitation on string length? Maybe in standard headers there's a variable with the max

Is there a type-safe way of getting an element count for arrays in C? [duplicate]

ε祈祈猫儿з 提交于 2019-11-29 11:11:31
This question already has an answer here: Reliably determine the number of elements in an array 2 answers The usual approach to getting an array's element count in C in something like this: #define COUNTOF(arr) (sizeof(arr) / sizeof(arr[0])) This results in an integral-constant expression, which is a very nice plus as well. The problem is that it isn't type-safe: int* i; COUNTOF(i); /* compiles :( */ . In practice, this should come up rarely, but for the sake of correctness it would be nice to make this type-safe. In C++03 this is easy (and in C++11 it's even easier, left as an exercise for

C90: How do I globally initialize this struct in C without C99 extensions

ぐ巨炮叔叔 提交于 2019-11-29 10:24:26
问题 I was wondering what the best way to initialize this struct is with C90, while still keeping it neat. In my header file, call it test.h, I have the following struct defined: struct s_test_cfg{ char *a[3]; char *b[3]; char *c[3]; } Then I have it declared as an extern struct so that I can initialize it globally in the .c file: extern struct s_test_cfg test_cfg; Now in my .c file, I want to be able to declare something like this globally (obviously what I'm about to write is unsupported in C90)

Variable-length arrays in C89?

牧云@^-^@ 提交于 2019-11-29 09:57:34
I've read that C89 does not support variable-length arrays, but the following experiment seems to disprove that: #include <stdio.h> int main() { int x; printf("Enter a number: "); scanf("%d", &x); int a[x]; a[0] = 1; // ... return 0; } When I compile as such (assuming filename is va_test.c ): gcc va_test.c -std=c89 -o va_test It works... What am I missing? :-) GCC always supported variable length arrays AFAIK. Setting -std to C89 doesn't turn off GCC extensions ... Edit: In fact if you check here: http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#C-Dialect-Options Under -std= you will

C: convert double to float, preserving decimal point precision

浪子不回头ぞ 提交于 2019-11-29 05:35:39
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("%f\n%f\n%f\n", f, ff, fff); it should print 0.1108 639728.17000 345.23456 all digits after the

Is implicit function declaration legal in C89?

早过忘川 提交于 2019-11-29 05:14:33
Consider this C program: int main() { puts("Hello world!"); return 0; } This compiles and runs fine and as far as I understand, is legal C89. However, I'm not 100% sure about that. Compiling in C99 mode with clang informs me that implicit declaration of function 'puts' is invalid in C99 (which makes me think that the C standard must have changed in C99 to make implicit function declaration illegal, which is what I'm trying to confirm). Is implicit function declaration legal in C89? (even if it's a bad idea to do it (unless your in an obfuscated C code challenge)) Is implicit function

C89, Mixing Variable Declarations and Code

十年热恋 提交于 2019-11-29 04:44:18
I'm very curious to know why exactly C89 compilers will dump on you when you try to mix variable declarations and code, like this for example: rutski@imac:~$ cat test.c #include <stdio.h> 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:~$ Yes, you can avoid this sort of thing by staying away from -pedantic. But then your code is no longer standards compliant. And as anybody capable of answering this post

Problem trying to use the C qsort function

你离开我真会死。 提交于 2019-11-29 01:54:05
#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 It's wrong because the order of -1 and -1.1 is changed. I believe it is happening because my "compare"