c89

Is negating INT_MIN undefined behaviour?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 09:31:55
问题 Let's say I have a variable i that comes from external sources: int i = get_i(); Assuming i is INT_MIN and two's complement representation, is -i undefined? 回答1: It depends on the platform. C supports three representations for negative numbers (see section 6.2.6.2 of the C99 standard): Two's complement. One's complement. Sign and magnitude. With one's complement and sign and magnitude, -INT_MIN is defined (and equal to INT_MAX ). With two's complement, it depends on whether the value with

ANSI C: If a function pointer points to executable code does that mean less execution overhead than simply invoking the function? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-06 08:28:48
This question already has answers here : Does Function pointer make the program slow? (8 answers) Closed 2 years ago . We know that using function pointers in C can be quite helpful when used in the proper scenarios (calling a function at runtime vs compile time, making the code more readable, etc.), but there isn't much literature around simple function invocation vs using a function pointer. void foo(void) { printf("hello\n"); } int testFcn(void) { // simple invokation foo(); return 0; } // Or, declare function pointer and assign void (*myFunc)(void) = foo; int testFcn(myFunc) { // Function

c89: Convert an int to void* and back

你离开我真会死。 提交于 2019-12-05 21:15:44
问题 First off, this is not a dupe of: Is it safe to cast an int to void pointer and back to int again? The difference in the questions is this: I'm only using the void* to store the int, but I never actually use it as a void*. So the question really comes down to this: Is a void * guaranteed to be at least as wide as an int I can't use intptr_t because I'm using c89 / ANSI C. EDIT In stdint.h from C99 ( gcc version ) I see the following: /* Types for `void *' pointers. */ #if __WORDSIZE == 64 #

Should I use “-ansi” or explicit “-std=…” as compiler flags?

99封情书 提交于 2019-12-05 13:58:15
问题 I've read that ANSI C is not exactly the same as ISO C and compilers may differ in interpretation of what "-ansi" is about. (gcc maps it to C90, clang maps it to C89) At the moment I would tend to use "-std=..." over "-ansi" as then it is explicitly shown which standard is used. As I am specifically interested in compiling on Linux, Windows and MAC, I fear some compilers could not understand "-std=..." but "-ansi". So are there any pros and cons for using the one over the other? 回答1: If you

What parts of C are most portable?

拈花ヽ惹草 提交于 2019-12-05 11:37:19
问题 I recently read an interview with Lua co-creators Luiz H. de Figueredo and Roberto Ierusalimschy, where they discussed the design, and implementation of Lua. It was very intriguing to say the least. However, one part of the discussion brought something up in my mind. Roberto spoke of Lua as a " freestanding application " (that is, it's pure ANSI C that uses nothing from the OS.) He said, that the core of Lua was completely portable, and because of its purity has been able to be ported much

C89: signed/unsigned mismatch

丶灬走出姿态 提交于 2019-12-05 11:12:21
Are signed/unsigned mismatches necessarily bad? Here is my program: int main(int argc, char *argv[]) { unsigned int i; for (i = 1; i < argc; i++) { // signed/unsigned mismatch here } } argc is signed, i is not. Is this a problem? "signed/unsigned mismatches" can be bad. In your question, you are asking about comparisons. When comparing two values of the same base type, but one signed and one unsigned, the signed value is converted to unsigned. So, int i = -1; unsigned int j = 10; if (i < j) printf("1\n"); else printf("2\n"); prints 2, not 1. This is because in i < j , i is converted to an

Pass matrix as argument

做~自己de王妃 提交于 2019-12-05 09:33:25
I want to pass two matrices as argument. These matrices have different size and i don't understand how i have to do this work: #include <stdio.h> #include <stdlib.h> void f(int m[3][], int n); int main() { int A[3][3]={{1,2,3},{4,5, 6},{7,8,9}}; int B[3][2]={{1,2},{3, 4}, {5, 6}}; f(A, 3); f(B, 2); return 0; } void f(int m[3][], int n) { int i,j; for(i=0;i<3;i++) { for(j=0;j<n;j++) printf("%5d", m[i][j]); } return; } How can I do this? The only safe way that I know of to do this is to include the matrix dimensions in the parameters, or make some kind of matrix struct Option A) dimensions as

A legal array assignment. Is it possible?

[亡魂溺海] 提交于 2019-12-05 05:42:59
After reading the chapter about structures in the K&R book I decided to make some tests to understand them better, so I wrote this piece of code: #include <stdio.h> #include <string.h> struct test func(char *c); struct test { int i ; int j ; char x[20]; }; main(void) { char c[20]; struct {int i ; int j ; char x[20];} a = {5 , 7 , "someString"} , b; c = func("Another string").x; printf("%s\n" , c); } struct test func(char *c) { struct test temp; strcpy(temp.x , c); return temp; } My question is: why is c = func("Another string").x; working (I know that it's illegal, but why is it working)? At

Compatibility of C89/C90, C99 and C11

本秂侑毒 提交于 2019-12-05 03:41:54
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 of older C versions? By superset I mean, that old code will compile without errors and the same

Implementing a FIFO queue in C

∥☆過路亽.° 提交于 2019-12-05 01:52:10
问题 For an embedded application, I am trying to implement a first-in, first-out (FIFO) queue of structs using ANSI C. The most straightforward way to do this seems to be by implementing a linked-list, so that each structure contains a pointer to the next in the queue. Hence I define the struct itself as: typedef enum { LED_on, LED_off, etc } Action; typedef struct Queued_Action QueuedAction; struct Queued_Action { Action action; int value; QueuedAction *nextAction; }; So far so good. If I define