c89

Recursive declaration of function pointer in C

谁都会走 提交于 2019-11-26 08:27:24
问题 I\'d like to declare a function that returns a pointer to a function of the same type. I would like to use it to implement state machines like the one below: typedef event_handler_t (*event_handler_t)(event_t*); // compilation error event_handler_t state2(event_t* e); event_handler_t state1(event_t* e) { switch(e->type) { //... case SOME_EVENT: return state2; //... } } event_handler_t state2(event_t* e) { switch(e->type) { //... case OTHER_EVENT: return state1; //... } } //... event_handler_t

Using M_PI with C89 standard

落花浮王杯 提交于 2019-11-26 07:47:52
问题 I\'m using C and trying to get access to the constant M_PI (3.14159...). I have imported the math.h header file, but the M_PI constant was still undefined. Through some searching on StackOverflow I have found that I need to add #define _USE_MATH_DEFINES to my code (see example code below). This works fine when compiling normally, but I need to be able to compile with the std=c89 flag for the work that I\'m doing. How should I access M_PI from some C89 code? 回答1: A conforming standard library

What is the difference between C, C99, ANSI C and GNU C?

余生颓废 提交于 2019-11-26 06:04:04
问题 I have started programming practice on codechef and have been confused by the difference between C and C99. What does C mean here? Is it C89? Check the languages at the bottom of this submit. It contains both C and C99. I found on the internet something called GNU C. Is there a different C for linux/unix systems? Are these compliant to the C standards by ANSI? I have also read in some places \"C99 strict\". What is this? Are there any other different standards of C in use? Is there something

Does either ANSI C or ISO C specify what -5 % 10 should be?

微笑、不失礼 提交于 2019-11-26 04:52:55
问题 I seem to remember that ANSI C didn\'t specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly? 回答1: C89, not totally (§3.3.5/6). It can be either -5 or 5, because -5 / 10 can return 0 or -1 ( % is defined in terms of a linear equation involving / , * and + ): When integers are divided and the division is inexact, if both operands are

Enabling VLAs (variable length arrays) in MS Visual C++?

笑着哭i 提交于 2019-11-26 02:02:29
问题 How can I enable the use of VLAs, variable length arrays as defined in C99, in MS Visual C++ or that is not possible at all? Yes I know that the C++ standard is based on C89 and that VLAs are not available in C89 standard and thus aren\'t available in C++, but MSVC++ is supposed to be a C compiler also, a behavior that can be switched on using the /TC compiler parameter ( Compile as C Code (/TC) ). But doing so does not seem to enable VLAs and the compiling process fails with the same errors

Variable declaration placement in C

冷暖自知 提交于 2019-11-26 00:47:00
问题 I long thought that in C, all variables had to be declared at the beginning of the function. I know that in C99, the rules are the same as in C++, but what are the variable declaration placement rules for C89/ANSI C? The following code compiles successfully with gcc -std=c89 and gcc -ansi : #include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { char c = (i % 95) + 32; printf(\"%i: %c\\n\", i, c); char *s; s = \"some string\"; puts(s); } return 0; } Shouldn\'t the declarations of c

Is the “struct hack” technically undefined behavior?

别来无恙 提交于 2019-11-26 00:16:39
问题 What I am asking about is the well known \"last member of a struct has variable length\" trick. It goes something like this: struct T { int len; char s[1]; }; struct T *p = malloc(sizeof(struct T) + 100); p->len = 100; strcpy(p->s, \"hello world\"); Because of the way that the struct is laid out in memory, we are able to overlay the struct over a larger than necessary block and treat the last member as if it were larger than the 1 char specified. So the question is: Is this technique

Why should you use strncpy instead of strcpy?

别等时光非礼了梦想. 提交于 2019-11-26 00:08:22
问题 Edit: I\'ve added the source for the example. I came across this example: char source[MAX] = \"123456789\"; char source1[MAX] = \"123456789\"; char destination[MAX] = \"abcdefg\"; char destination1[MAX] = \"abcdefg\"; char *return_string; int index = 5; /* This is how strcpy works */ printf(\"destination is originally = \'%s\'\\n\", destination); return_string = strcpy(destination, source); printf(\"after strcpy, dest becomes \'%s\'\\n\\n\", destination); /* This is how strncpy works */

What is the behavior of integer division?

限于喜欢 提交于 2019-11-25 21:38:33
问题 For example, int result; result = 125/100; or result = 43/100; Will result always be the floor of the division? What is the defined behavior? 回答1: Will result always be the floor of the division? What is the defined behavior? Yes, integer quotient of the two operands. 6.5.5 Multiplicative operators 6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. 88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall

Variable declaration placement in C

北战南征 提交于 2019-11-25 18:53:12
I long thought that in C, all variables had to be declared at the beginning of the function. I know that in C99, the rules are the same as in C++, but what are the variable declaration placement rules for C89/ANSI C? The following code compiles successfully with gcc -std=c89 and gcc -ansi : #include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { char c = (i % 95) + 32; printf("%i: %c\n", i, c); char *s; s = "some string"; puts(s); } return 0; } Shouldn't the declarations of c and s cause an error in C89/ANSI mode? It compiles successfully because GCC allows it as a GNU extension,