c99

Convert C99 code to C89

百般思念 提交于 2019-11-30 02:46:07
问题 How can I convert c99 source code automatically to c89? I want to compile c99 libraries with Visual C++, but MSVC only supports c89. Many changes are only syntactic, such as struct initializers, and you could write a tool to "de-c99" code automatically. Does this preprocessor exist? 回答1: Clang-based source-to-source translator: https://github.com/libav/c99-to-c89/ 回答2: The commercial Comeau C/C++ compiler can do this. Alternatively, use a proper C compiler (eg GCC or Clang via MinGW, Pelles C

What techniques/strategies do people use for building objects in C (not C++)?

纵然是瞬间 提交于 2019-11-30 00:57:36
I am especially interested in objects meant to be used from within C, as opposed to implementations of objects that form the core of interpreted languages such as python. I tend to do something like this: struct foo_ops { void (*blah)(struct foo *, ...); void (*plugh)(struct foo *, ...); }; struct foo { struct foo_ops *ops; /* data fields for foo go here */ }; With these structure definitions, the code implementing foo looks something like this: static void plugh(struct foo *, ...) { ... } static void blah(struct foo *, ...) { ... } static struct foo_ops foo_ops = { blah, plugh }; struct foo

Are all pointers derived from pointers to structure types the same?

情到浓时终转凉″ 提交于 2019-11-29 23:59:19
The Question The question of whether all pointers derived from pointers to structure types are the same, is not easy to answer. I find it to be a significant question for the following two primary reasons. A. The lack of a pointer to pointer to 'any' incomplete or object type, imposes a limitation on convenient function interfaces, such as: int allocate(ANY_TYPE **p, size_t s); int main(void) { int *p; int r = allocate(&p, sizeof *p); } [ Complete code sample ] The existing pointer to 'any' incomplete or object type is explicitly described as: C99 / C11 §6.3.2.3 p1 : A pointer to void may be

How does the compiler allocate memory without knowing the size at compile time?

孤街醉人 提交于 2019-11-29 23:27:25
I wrote a C program that accepts integer input from the user, that is used as the size of an integer array, and using that value it declares an array of given size, and I am confirming it by checking the size of the array. Code: #include <stdio.h> int main(int argc, char const *argv[]) { int n; scanf("%d",&n); int k[n]; printf("%ld",sizeof(k)); return 0; } and surprisingly it is correct! The program is able to create the array of required size. But all static memory allocation is done at compile time, and during compile time the value of n is not known, so how come the compiler is able to

What are the most useful new features in C99? [closed]

梦想的初衷 提交于 2019-11-29 18:42:27
C99 has been around for over 10 years, but support for it has been slow coming, so most developers have stuck with C89. Even today, I'm sometimes mildly surprised when I come across C99 features in C code. Now that most major compilers support C99 (MSVC being a notable exception, and some embedded compilers also lagging behind), I feel that developers who work with C probably ought to know about what C99 features are available to them. Some of the features are just common features that were never standardized before ( snprintf , for instance), or are familiar from C++ (flexible variable

What does &(int) { 1 } mean in C++?

旧街凉风 提交于 2019-11-29 16:02:11
问题 I saw this here and I don't know what it means: &(int) { 1 } I thought it was weird because it seems like invalid syntax. It's casting a block scope(?) with a random 1 in the middle (without a semi-colon) and taking the address. Doesn't make a lot of sense to me. Could you guys enlighten me? Tried it out w/ C++11, so it compiles: auto a = &(int) { 1 }; But I have no idea what to do with the variable. 回答1: As far as I can tell this is a compound literal, it is C99 feature, it is not standard C

Unsigned to signed conversion in C

让人想犯罪 __ 提交于 2019-11-29 15:22:36
Is the following guaranteed to work or implementation defined? unsigned int a = 4294967294; signed int b = a; The value of b is -2 on gcc. From C99 (§6.3.1.3/3) Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised. The conversion of a value to signed int is implementation-defined (as you correctly mentioned because of 6.3.1.3p3) . On some systems for example it can be INT_MAX (saturating conversion). For gcc the implementation behavior is defined here: The result of, or the signal

-Wmissing-field-initializer when using designated initializers

时光毁灭记忆、已成空白 提交于 2019-11-29 14:46:41
I'm using GCC 4.6.2 (Mingw) and compiling with -Wextra . I'm getting strange warnings whenever I use designated initializers. For the following code typedef struct { int x; int y; } struct1; typedef struct { int x; int y; } struct2; typedef struct { struct1 s1; struct2 s2[4]; } bug_struct; bug_struct bug_struct1 = { .s1.x = 1, .s1.y = 2, .s2[0].x = 1, .s2[0].y = 2, .s2[1].x = 1, .s2[1].y = 2, .s2[2].x = 1, .s2[2].y = 2, .s2[3].x = 1, .s2[3].y = 2, }; I get warnings bug.c:24:3: warning: missing initializer [-Wmissing-field-initializers] bug.c:24:3: warning: (near initialization for 'bug_struct1

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