c89

Compatibility of C89/C90, C99 and C11

对着背影说爱祢 提交于 2019-12-21 09:18:23
问题 I just read: C Wikipedia entry. As far as I know there are 3 different versions of C that are widely used: C89, C99 and C11. My question concerns the compatibility of source code of different versions. Suppose I am going to write a program (in C11 since it is the latest version) and import a library written in C89. Are these two versions going to work together properly when compiling all files according to the C11 specification? Question 1 : Are the newer versions of C i.e. C99, C11 supersets

clear/truncate file in C when already open in “r+” mode

自古美人都是妖i 提交于 2019-12-19 19:48:09
问题 My code currently looks something like this (these steps splitted into multiple functions): /* open file */ FILE *file = fopen(filename, "r+"); if(!file) { /* read the file */ /* modify the data */ /* truncate file (how does this work?)*/ /* write new data into file */ /* close file */ fclose(file); } I know I could open the file with in "w" mode, but I don't want to do this in this case. I know there is a function ftruncate in unistd.h / sys/types.h , but I don't want to use these functions

mixed declarations and codes

自作多情 提交于 2019-12-19 05:47:11
问题 When I compile function with "gcc -o dene -Wall -ansi -pedantic-errors dene.c" ,gcc emits no error.(can you look a line which starts with char ....,in if loop,) 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);

mixed declarations and codes

瘦欲@ 提交于 2019-12-19 05:46:15
问题 When I compile function with "gcc -o dene -Wall -ansi -pedantic-errors dene.c" ,gcc emits no error.(can you look a line which starts with char ....,in if loop,) 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);

How to “simulate” C99 in Visual Studio for variables declaration

本秂侑毒 提交于 2019-12-18 19:38:21
问题 I'm using Visual Studio 2012 to develop simple Win32 C programs. I know that the VS compiler only supports C89, but I'd like to know if there is a way to override this limitation. In particular I'd like to declare variables anywhere in my code, instead of only at the beginning of scope blocks (as C89 requires). Thanks in advance. 回答1: The choices I see: stick with MSVC and switch to C++ stick with MSVC and use a precompiler that translates C99 to C90 (Comeau, c99-to-c89) switch to a toolchain

Are there any differences between ANSI C and ISO C?

≡放荡痞女 提交于 2019-12-18 13:12:05
问题 I understand that there is both an ANSI standard and an ISO standard for C. Are there any differences between these two standards? If so, what are they? And if there is not a difference then what's the point of having two standards? 回答1: In 1990, the ANSI C standard (with a few minor modifications) was adopted by the International Organization for Standardization as ISO/IEC 9899:1990. This version is sometimes called C90. Therefore, the terms "C89" and "C90" refer to essentially the same

GNU89, mixed declarations and loop initial declarations

孤人 提交于 2019-12-18 07:04:33
问题 The default C dialect for GCC and ICC is GNU89. GNU89 allows mixed declarations e.g. int i; i = 0; int j; I inferred (incorrectly) from a number of other posts on SO e.g C: for loop int initial declaration, that this meant I could do for(int i=0; i<n; i++) with GNU89 but when I do this I get error: 'for' loop initial declarations are only allowed in C99 mode Apparently, mixed declarations and loop initial declarations are not the same thing (i.e. one does not imply the other). If I could only

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

Deadly 提交于 2019-12-18 06:13:48
问题 This question already has answers here : Reliably determine the number of elements in an array (2 answers) Closed 2 years ago . 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

Variable-length arrays in C89?

冷暖自知 提交于 2019-12-18 05:47:11
问题 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? :-) 回答1: GCC always supported variable length arrays AFAIK. Setting -std to C89 doesn't turn off GCC extensions ... Edit: In fact if you

Is implicit function declaration legal in C89?

大兔子大兔子 提交于 2019-12-18 04:24:27
问题 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